Unable to detect multiple parameter annotations with different RetentionPolicies.
See original GitHub issueThe issue is that parameter annotations with RetentionPolicy.RUNTIME
doesn’t work when the parameter also has another annotation with RetentionPolicy.CLASS
.
Tested with master branch - version 4.8.22-SNAPSHOT (352eb88)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface AnnoRuntime { }
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface AnnoClass { }
public void function(@AnnoRuntime @AnnoClass int arg) { }
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
java - How do different retention policies affect my annotations?
CLASS : Appear in the decompiled class, but can't be inspected at run-time with reflection with getAnnotations(). RetentionPolicy.RUNTIME : ...
Read more >classgraph/classgraph classgraph-4.8.22 on GitHub - NewReleases.io
This fix merges together annotations from both retention policies, so that method parameter annotations from one of the two retention policies are not...
Read more >Custom Java Annotations & Implementing what to do using ...
So, let's see the different retention policy options and understand their differences. There are 3 different retention policies:.
Read more >Java @Retention Annotations - GeeksforGeeks
Here we will be creating three custom annotations with retention policies such as SOURCE, CLASS, and RUNTIME. These custom annotations are later ...
Read more >Java Annotations Explained - belief driven design
But annotations can have multiple values that might be required to be set ... Being able to annotate our code and check if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@T101N I found the source of the problem. In the classfile format,
RuntimeVisibleParameterAnnotations
andRuntimeInvisibleParameterAnnotations
are listed in separate method attributes, and both of them can be present with separate arrays of annotations for the method. I was overwriting the method parameter annotations for the second attribute, rather than extending the arrays of annotations already read for the first attribute in order to merge together both annotation arrays into a single array of annotations for each method parameter.Eclipse seems to only reliably fail if commandline Maven (commandline
javac
) last built the project, notecj
. However I saw some nondeterminacy between different runs on the commandline. Apparentlyjavac
uses aSet
for storing annotations by retention type, and uses a nondeterministic hash function, probably based on the class reference (so the order in which classes are loaded can actually change the order that attributes are written to the classfile).Released (and should be fixed) in 4.8.22. Thank you for the comprehensive testcase code!
Interesting… No filtering is applied while an
AnnotationInfoList
is being constructed though, and there should be no state carried over from annotation to annotation. The parameter annotation info is read here:https://github.com/classgraph/classgraph/blob/master/src/main/java/io/github/classgraph/Classfile.java#L1259
The
AnnotationInfoList
is built here:https://github.com/classgraph/classgraph/blob/master/src/main/java/io/github/classgraph/MethodParameterInfo.java#L175
It’s all pretty straightforward…
There have been Kotlin issues in the past, see:
https://github.com/classgraph/classgraph/blob/master/src/main/java/io/github/classgraph/MethodInfo.java#L375