INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.
http://spoon.gforge.inria.fr/
Other
1.74k stars 346 forks source link

static import not written out by Spoon #3214

Open shoeoffhead opened 4 years ago

shoeoffhead commented 4 years ago

I just did a little test reading in and writing out commons-io-2.6-sources.jar with Spoon.

IOUtils defines: public static final int EOF = -1;

This is statically imported by org.apache.commons.io.output.ByteArrayOutputStream and org.apache.commons.io.EndianUtils

Spoon just adds this static import to ByteArrayOutputStream but NOT EndianUtils

monperrus commented 4 years ago

Thanks a lot for the bug report.

Could you share a link to the source file you're analyzing?

shoeoffhead commented 4 years ago

Just the latest commons-io version: 2.6 https://mvnrepository.com/artifact/commons-io/commons-io/2.6

You can also download it manually: https://repo1.maven.org/maven2/commons-io/commons-io/2.6 The file is called: commons-io-2.6-sources.jar

I also use the latest Spoon.

monperrus commented 4 years ago

Would you be able to isolate the bug in a failing test case in a pull request? That would be super useful. Thanks!

shoeoffhead commented 4 years ago

I did now several other tests read/writing sources from big libraries. I always have issues with static imports not written out.

I will prepare a minimal project for you showing the bug. I am not very familiar with Github ;)

[UPDATE]: I forgot that this problems happens when using launcher.getEnvironment().setAutoImports(true);

With this setting Spoon removes the static import in the written out source code: for example: import static java.lang.Character.isLetter;

If I use setAutoImports(false), Spoon removes the static import BUT also writes the full qualified name for EVERY import in the source code: for example: java.lang.Character.isLetter(...)

I would prefer using setAutoImports(true) because otherwise the source code is cluttered with qualified names.

monperrus commented 4 years ago

excellent, looking forward to it!

shoeoffhead commented 4 years ago
  1. extract the attached zip file
  2. execute on the cli: gradle run
  3. the files are written to folder 'output'. Add 'output' as a source folder to your IDE to see the described error in my first post.

commons-collections.zip

shoeoffhead commented 4 years ago

Did you have time looking into this?

monperrus commented 4 years ago

Not yet. If you could reproduce the bug in a failing test case in a pull request, that would be super useful. Thanks!

shoeoffhead commented 4 years ago

Sorry. I am not really familiar with Git. The zip project is super simple though. You can reproduce the problem in seconds.