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.75k stars 351 forks source link

[Bug]: Attempting to build model of Dubbo (3.1.1) #4952

Open QifanWang opened 2 years ago

QifanWang commented 2 years ago

Describe the bug

I try to analyze Dubbo source code, but Spoon throws a ModelBuildingException. Shall I use Spoon to analyze such multi-pom.xml projects? Or I need to set some properties of maven laucher?

Source code you are trying to analyze/transform

Source code are downloaded from https://github.com/apache/dubbo/archive/refs/tags/dubbo-3.1.1.zip

Source code for your Spoon processing

MavenLauncher launcher = new MavenLauncher(
               "/home/cstar/Downloads/dubbo-dubbo-3.1.1",
                MavenLauncher.SOURCE_TYPE.APP_SOURCE);
launcher.buildModel();

Actual output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" spoon.compiler.ModelBuildingException: The type Application is already defined
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.reportProblem(JDTBasedSpoonCompiler.java:598)
    at spoon.support.compiler.jdt.TreeBuilderRequestor.acceptResult(TreeBuilderRequestor.java:29)
    at spoon.support.compiler.jdt.TreeBuilderCompiler.buildUnits(TreeBuilderCompiler.java:110)
    at spoon.support.compiler.jdt.JDTBatchCompiler.getUnits(JDTBatchCompiler.java:283)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnits(JDTBasedSpoonCompiler.java:416)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:368)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:334)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:115)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:98)
    at spoon.Launcher.buildModel(Launcher.java:782)
    at Main.main(Main.java:9)

Process finished with exit code 1

Expected output

build model successfully.

Spoon Version

10.1.1

JVM Version

11

What operating system are you using?

Ubuntu 20.04.3 LTS

SirYwell commented 2 years ago

This is indeed a problem because Spoon tries to build the model from all files at once. Spoon has no concept of multi-modules in its hierarchy, so having two classes with the same FQN will cause issues.

As an example, both https://github.com/apache/dubbo/blob/a66f5126745858f393c67c15d9a84f9e60405aba/dubbo-demo/dubbo-demo-annotation/dubbo-demo-annotation-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java and https://github.com/apache/dubbo/blob/a66f5126745858f393c67c15d9a84f9e60405aba/dubbo-demo/dubbo-demo-api/dubbo-demo-api-consumer/src/main/java/org/apache/dubbo/demo/consumer/Application.java are in the same package and have the same simple name. Therefore, whenever asking for org.apache.dubbo.demo.consumer.Application it is unclear which class is actually meant.

For this to work, you would currently need mutliple Launchers. Or probably it's enough to exclude the demo modules in your case?

Note there is also https://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/compiler/Environment.html#setIgnoreDuplicateDeclarations(boolean). This might make the error go away, but it will probably also break other things.

dhmoclex commented 2 years ago

Hello,

I do not understand as the documentation states:

The Spoon MavenLauncher (JavaDoc) is used to create the AST model of a Maven project. It automatically infers the list of source folders and the dependencies from the pom.xml file. This launcher handles multi-module Maven projects.

(https://spoon.gforge.inria.fr/launcher.html)

Sincerely,

dhmoclex commented 2 years ago

I may be mistaken, the documentation talks about Maven modules, not Jigsaw modules?

This bug seems related to #4751.

SirYwell commented 2 years ago

I do not understand as the documentation states:

The Spoon MavenLauncher (JavaDoc) is used to create the AST model of a Maven project. It automatically infers the list of source folders and the dependencies from the pom.xml file. This launcher handles multi-module Maven projects.

Yes, the issue here is the vague usage of "handles multi-module Maven projects". What that (currently) actually means it can find all the modules, it can find the dependencies of all the modules, but all the modules are on the source classpath for JDT, so JDT will try to compile all at once. I assume this can't be simply fixed in a non-breaking way in the current MavenLauncher,

I may be mistaken, the documentation talks about Maven modules, not Jigsaw modules?

This bug seems related to #4751.

Yes, the documentation of the MavenLauncher talks about Maven modules. The given project also doesn't seem to be based on Jigsaw modules, so that shouldn't be an issue here.