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 352 forks source link

[Bug]: package-info incorrect processing #5357

Open embraceddd opened 1 year ago

embraceddd commented 1 year ago

Describe the bug

Salut! Attaching demo-project https://github.com/embraceddd/spoonpckinfobug Quickstart for it: spoonpckinfobug/src/main/resources/buggedProject/src/main/java/demo

There are two packages notwork and work that contains package-info files. Package-info file in package work proccesing fine in all ways. For package-info file in package notwork we can get different result depends on app dependencies and application startup method

1) If we HAVE jakarta-xml-bind dependency in our project and we will start app with shorten-command-line JARManifest method, app will fall down with JLSViolation.

2) If we DON'T have jakarta-xml-bind dependency in our project and we will start app with shorten-command-line JARManifest method, app will fall down with JLSViolation.

3) If we HAVE jakarta-xml-bind dependency in our project and we will start app with shorten-command-line ARGFILE method, app will successflyy process package-info file.

4) If we DON'T have jakarta-xml-bind dependency in our project and we will start app with shorten-command-line ARGFILE method, app will fall down with JLSViolation.

Difference at those two package-info files is how their importing XmlNsForm class. In not working package-info file we are importing it like import javax.xml.bind.annotations.XmlNsForm, in working package-info file we are inlineing import

How to start it on your machihe: 1) Download repo https://github.com/embraceddd/spoonpckinfobug 2) Download dependencies via gradle 3) In classpath directory we need to change ${PROJECT_PATH} and ${HOME} variables Example for Windows: ${HOME}\${PROJECT_PATH}\spoonpckinfobug\bin\main ${HOME} = C:\Users\user ${PROJECT_PATH} = ${HOME}\IdeaProjects 4) At root directory there are 4 files named like start{Type}WithJakarta or start{Type}NoJakarta. Those file are command lines to start application from console, I used gitbash for example.

Source code you are trying to analyze/transform

@javax.xml.bind.annotation.XmlSchema(namespace = "n", elementFormDefault = XmlNsForm.QUALIFIED)
package demo.notwork;

import javax.xml.bind.annotation.XmlNsForm;

Source code for your Spoon processing

Launcher launcher = new Launcher();
            launcher.addInputResource(sourcePath);
            launcher.setSourceOutputDirectory(localPath.getAbsolutePath());
            launcher.getEnvironment().setNoClasspath(true);
            launcher.getEnvironment().setCopyResources(false);
            launcher.run();

Actual output

Exception in thread "main" spoon.JLSViolation: Not allowed javaletter or keyword in identifier found. See JLS for correct identifier. Identifier: package-info
    at spoon.JLSViolation.throwIfSyntaxErrorsAreNotIgnored(JLSViolation.java:38)
    at spoon.support.reflect.reference.CtReferenceImpl.checkIdentifierForJLSCorrectness(CtReferenceImpl.java:114)
    at spoon.support.reflect.reference.CtReferenceImpl.setSimpleName(CtReferenceImpl.java:57)
    at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReferenceFromSourceTypeBinding(ReferenceBuilder.java:1127)
    at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:860)
    at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:831)
    at spoon.support.compiler.jdt.ReferenceBuilder.getVariableReference(ReferenceBuilder.java:1261)
    at spoon.support.compiler.jdt.JDTTreeBuilderHelper.createFieldAccessNoClasspath(JDTTreeBuilderHelper.java:463)
    at spoon.support.compiler.jdt.JDTTreeBuilder.visit(JDTTreeBuilder.java:1529)
    at org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference.traverse(QualifiedNameReference.java:1172)
    at org.eclipse.jdt.internal.compiler.ast.MemberValuePair.traverse(MemberValuePair.java:260)
    at org.eclipse.jdt.internal.compiler.ast.NormalAnnotation.traverse(NormalAnnotation.java:76)
    at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:810)
    at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:788)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.traverseUnitDeclaration(JDTBasedSpoonCompiler.java:481)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$0(JDTBasedSpoonCompiler.java:438)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:465)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:436)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:373)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:336)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:117)
    at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:100)
    at spoon.Launcher.buildModel(Launcher.java:782)
    at spoon.Launcher.run(Launcher.java:733)
    at ru.embraceddx.spoonpckinfobug.SpoonPreparator.buildModel(SpoonPreparator.java:21)
    at ru.embraceddx.spoonpckinfobug.SpoonpckinfobugApplication.main(SpoonpckinfobugApplication.java:13)

Expected output

Success in all ways

Spoon Version

10.4.0

JVM Version

17.0.6

What operating system are you using?

Windows 10 Also tried at MacOs Ventura and Linux Ubuntu and get same results

hongxuchen commented 8 months ago

We have recently encountered quite a few similar issues when using spoon. We sincerely hope this can be fixed as package-info is so widely used.