Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: Parameter annotations in dynamic nested classes are parsed with a shift #696

Open
Lodin opened this issue Jul 8, 2022 · 2 comments

Comments

@Lodin
Copy link

Lodin commented Jul 8, 2022

Description

I was trying to parse parameter annotations in the dynamic class constructor and found that ClassGraph applies annotations with a shift to the left. So the annotation for the first parameter goes to the implicit parameter with an instance of the parent class. The annotation for the second parameter comes to the first one.

Here is my original code:

public class BrokenAnnotation {
    class Dynamic {
        Dynamic(@Foo String param1, @Bar String param2) {}
    }
}

And ClassGraph parses it the following way:

public class BrokenAnnotation {
    class Dynamic {
        Dynamic(@Foo BrokenAnnotation this$0, @Bar String param1, String param2) {}
    }
}

Expected behavior

I expect ClassGraph to apply annotations as specified in the source code.

Repro

I have created a small reproduction example.

ClassGraph#verbose()

Jul 08, 2022 4:48:47 PM nonapi.io.github.classgraph.utils.LogNode flush
INFO: 2022-07-08T16:48:46.960+0300	ClassGraph	ClassGraph version 4.8.149
2022-07-08T16:48:46.963+0300	ClassGraph	Operating system: Windows 10 10.0 amd64
2022-07-08T16:48:46.964+0300	ClassGraph	Java version: 11.0.2 / 11.0.2+9 (Oracle Corporation)
2022-07-08T16:48:46.964+0300	ClassGraph	Java home: <...>\OpenJDK\jdk-11.0.2
2022-07-08T16:48:47.009+0300	ClassGraph	ScanSpec:
2022-07-08T16:48:47.009+0300	ClassGraph	-- packageAcceptReject:
2022-07-08T16:48:47.010+0300	ClassGraph	-- packagePrefixAcceptReject:
2022-07-08T16:48:47.010+0300	ClassGraph	-- pathAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- pathPrefixAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- classAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- classfilePathAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- classPackageAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- classPackagePathAcceptReject:
2022-07-08T16:48:47.011+0300	ClassGraph	-- moduleAcceptReject:
2022-07-08T16:48:47.012+0300	ClassGraph	-- jarAcceptReject:
2022-07-08T16:48:47.012+0300	ClassGraph	-- classpathElementResourcePathAcceptReject:
2022-07-08T16:48:47.012+0300	ClassGraph	-- libOrExtJarAcceptReject:
2022-07-08T16:48:47.013+0300	ClassGraph	-- scanJars: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- scanNestedJars: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- scanDirs: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- scanModules: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- enableClassInfo: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- enableFieldInfo: true
2022-07-08T16:48:47.013+0300	ClassGraph	-- enableMethodInfo: true
2022-07-08T16:48:47.014+0300	ClassGraph	-- enableAnnotationInfo: true
2022-07-08T16:48:47.014+0300	ClassGraph	-- enableStaticFinalFieldConstantInitializerValues: true
2022-07-08T16:48:47.014+0300	ClassGraph	-- enableInterClassDependencies: false
2022-07-08T16:48:47.014+0300	ClassGraph	-- enableExternalClasses: false
2022-07-08T16:48:47.014+0300	ClassGraph	-- enableSystemJarsAndModules: false
2022-07-08T16:48:47.014+0300	ClassGraph	-- ignoreClassVisibility: true
2022-07-08T16:48:47.014+0300	ClassGraph	-- ignoreFieldVisibility: true
2022-07-08T16:48:47.015+0300	ClassGraph	-- ignoreMethodVisibility: true
2022-07-08T16:48:47.015+0300	ClassGraph	-- disableRuntimeInvisibleAnnotations: false
2022-07-08T16:48:47.015+0300	ClassGraph	-- extendScanningUpwardsToExternalClasses: true
2022-07-08T16:48:47.015+0300	ClassGraph	-- allowedURLSchemes: null
2022-07-08T16:48:47.015+0300	ClassGraph	-- addedClassLoaders: null
2022-07-08T16:48:47.015+0300	ClassGraph	-- overrideClassLoaders: null
2022-07-08T16:48:47.016+0300	ClassGraph	-- addedModuleLayers: null
2022-07-08T16:48:47.016+0300	ClassGraph	-- overrideModuleLayers: null
2022-07-08T16:48:47.016+0300	ClassGraph	-- overrideClasspath: [<...>\brokenanno\target\classes]
2022-07-08T16:48:47.016+0300	ClassGraph	-- classpathElementFilters: null
2022-07-08T16:48:47.016+0300	ClassGraph	-- initializeLoadedClasses: false
2022-07-08T16:48:47.016+0300	ClassGraph	-- removeTemporaryFilesAfterScan: false
2022-07-08T16:48:47.016+0300	ClassGraph	-- ignoreParentClassLoaders: false
2022-07-08T16:48:47.017+0300	ClassGraph	-- ignoreParentModuleLayers: false
2022-07-08T16:48:47.017+0300	ClassGraph	-- modulePathInfo:
2022-07-08T16:48:47.017+0300	ClassGraph	-- maxBufferedJarRAMSize: 67108864
2022-07-08T16:48:47.017+0300	ClassGraph	-- enableMemoryMapping: false
2022-07-08T16:48:47.018+0300	ClassGraph	Number of worker threads: 8
2022-07-08T16:48:47.027+0300	ClassGraph	Finding classpath
2022-07-08T16:48:47.029+0300	ClassGraph	-- Finding classpath and modules
2022-07-08T16:48:47.057+0300	ClassGraph	---- Overriding classpath with: [<...>\brokenanno\target\classes]
2022-07-08T16:48:47.059+0300	ClassGraph	------ Found classpath element: <...>/brokenanno/target/classes
2022-07-08T16:48:47.060+0300	ClassGraph	------ WARNING: when the classpath is overridden, there is no guarantee that the classes found by classpath scanning will be the same as the classes loaded by the context classloader
2022-07-08T16:48:47.066+0300	ClassGraph	Opening classpath elements (took 0,028377 sec)
2022-07-08T16:48:47.090+0300	ClassGraph	-- Opening classpath element file:///<...>/brokenanno/target/classes/
2022-07-08T16:48:47.096+0300	ClassGraph	Finding nested classpath elements
2022-07-08T16:48:47.097+0300	ClassGraph	Final classpath element order:
2022-07-08T16:48:47.098+0300	ClassGraph	-- file:///<...>/brokenanno/target/classes/
2022-07-08T16:48:47.098+0300	ClassGraph	Scanning classpath elements (took 0,018478 sec)
2022-07-08T16:48:47.099+0300	ClassGraph	-- Scanning Path classpath element file:///<...>/brokenanno/target/classes/ (took 0,016358 sec)
2022-07-08T16:48:47.101+0300	ClassGraph	---- Scanning Path: <...>/brokenanno/target/classes (took 0,013924 sec)
2022-07-08T16:48:47.104+0300	ClassGraph	------ Scanning Path: <...>/brokenanno/target/classes/io (took 0,011224 sec)
2022-07-08T16:48:47.105+0300	ClassGraph	-------- Scanning Path: <...>/brokenanno/target/classes/io/github (took 0,009690 sec)
2022-07-08T16:48:47.107+0300	ClassGraph	---------- Scanning Path: <...>/brokenanno/target/classes/io/github/brokenanno (took 0,008200 sec)
2022-07-08T16:48:47.110+0300	ClassGraph	------------ Found classfile within subpackage of accepted package: io/github/brokenanno/BrokenAnnotation$Bar.class
2022-07-08T16:48:47.121+0300	ClassGraph	-------------- Parsing classfile (took 0,036136 sec)
2022-07-08T16:48:47.151+0300	ClassGraph	---------------- Found annotation class io.github.brokenanno.BrokenAnnotation$Bar
2022-07-08T16:48:47.152+0300	ClassGraph	------------------ Superclass: java.lang.Object
2022-07-08T16:48:47.152+0300	ClassGraph	------------------ Interfaces: java.lang.annotation.Annotation
2022-07-08T16:48:47.153+0300	ClassGraph	------------------ Class annotations: @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME), @java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE_USE})
2022-07-08T16:48:47.154+0300	ClassGraph	------------------ External interface java.lang.annotation.Annotation was not found in non-rejected packages -- cannot extend scanning to this class
2022-07-08T16:48:47.155+0300	ClassGraph	------------------ External class annotation java.lang.annotation.Retention was not found in non-rejected packages -- cannot extend scanning to this class
2022-07-08T16:48:47.155+0300	ClassGraph	------------------ External enum class java.lang.annotation.RetentionPolicy was not found in non-rejected packages -- cannot extend scanning to this class
2022-07-08T16:48:47.157+0300	ClassGraph	------------------ External enum class java.lang.annotation.ElementType was not found in non-rejected packages -- cannot extend scanning to this class
2022-07-08T16:48:47.113+0300	ClassGraph	------------ Found classfile within subpackage of accepted package: io/github/brokenanno/BrokenAnnotation$Dynamic.class
2022-07-08T16:48:47.121+0300	ClassGraph	-------------- Parsing classfile (took 0,036768 sec)
2022-07-08T16:48:47.154+0300	ClassGraph	---------------- Found class io.github.brokenanno.BrokenAnnotation$Dynamic
2022-07-08T16:48:47.154+0300	ClassGraph	------------------ Superclass: java.lang.Object
2022-07-08T16:48:47.157+0300	ClassGraph	------------------ Field: final synthetic this$0
2022-07-08T16:48:47.157+0300	ClassGraph	------------------ Method: <init>
2022-07-08T16:48:47.114+0300	ClassGraph	------------ Found classfile within subpackage of accepted package: io/github/brokenanno/BrokenAnnotation$Foo.class
2022-07-08T16:48:47.121+0300	ClassGraph	-------------- Parsing classfile (took 0,036093 sec)
2022-07-08T16:48:47.153+0300	ClassGraph	---------------- Found annotation class io.github.brokenanno.BrokenAnnotation$Foo
2022-07-08T16:48:47.153+0300	ClassGraph	------------------ Superclass: java.lang.Object
2022-07-08T16:48:47.153+0300	ClassGraph	------------------ Interfaces: java.lang.annotation.Annotation
2022-07-08T16:48:47.155+0300	ClassGraph	------------------ Class annotations: @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME), @java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE_USE})
2022-07-08T16:48:47.156+0300	ClassGraph	------------------ External class annotation java.lang.annotation.Target was not found in non-rejected packages -- cannot extend scanning to this class
2022-07-08T16:48:47.115+0300	ClassGraph	------------ Found classfile within subpackage of accepted package: io/github/brokenanno/BrokenAnnotation.class
2022-07-08T16:48:47.121+0300	ClassGraph	-------------- Parsing classfile (took 0,034929 sec)
2022-07-08T16:48:47.151+0300	ClassGraph	---------------- Found class io.github.brokenanno.BrokenAnnotation
2022-07-08T16:48:47.151+0300	ClassGraph	------------------ Superclass: java.lang.Object
2022-07-08T16:48:47.156+0300	ClassGraph	------------------ Method: public <init>
2022-07-08T16:48:47.116+0300	ClassGraph	Masking classfiles (took 0,000581 sec)
2022-07-08T16:48:47.120+0300	ClassGraph	Scanning classfiles (took 0,037636 sec)
2022-07-08T16:48:47.158+0300	ClassGraph	Linking related classfiles (took 0,009834 sec)
2022-07-08T16:48:47.174+0300	ClassGraph	Total time: 0,105 sec
@lukehutch
Copy link
Member

@Lodin Sorry, my initial analysis about this was totally wrong, I deleted the comment -- and I apologize that it took me months to look into this -- I have been completely swamped.

I wrote a unit test based on the code you provided, and it passes fine. Can you please try to break the unit test?

(Sorry, I tagged the commit with the wrong issue number -- it is here: 0380d46 )

@lukehutch
Copy link
Member

@Lodin any updates please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants