question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to detect multiple parameter annotations with different RetentionPolicies.

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
lukehutchcommented, Mar 28, 2019

@T101N I found the source of the problem. In the classfile format, RuntimeVisibleParameterAnnotations and RuntimeInvisibleParameterAnnotations 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, not ecj. However I saw some nondeterminacy between different runs on the commandline. Apparently javac uses a Set 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!

1reaction
lukehutchcommented, Mar 27, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found