LANG-1815: AnnotationUtils.equals fails for package-private annotations#1587
Open
inponomarev wants to merge 2 commits intoapache:masterfrom
Open
LANG-1815: AnnotationUtils.equals fails for package-private annotations#1587inponomarev wants to merge 2 commits intoapache:masterfrom
inponomarev wants to merge 2 commits intoapache:masterfrom
Conversation
| void equalsWorksOnPackagePrivateAnnotations() throws Exception { | ||
| Tag tagA = getClass().getDeclaredField("a").getAnnotation(Tag.class); | ||
| Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class); | ||
| Assertions.assertTrue(AnnotationUtils.equals(tagA, tagB)); |
Member
There was a problem hiding this comment.
We use static imports for JUnit Assertions (and only other JUnit classes).
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
|
|
||
| // CAUTION: in order to reproduce https://issues.apache.org/jira/browse/LANG-1815, |
| for (final Method m : type1.getDeclaredMethods()) { | ||
| if (m.getParameterTypes().length == 0 | ||
| && isValidAnnotationMemberType(m.getReturnType())) { | ||
| m.setAccessible(true); |
Member
There was a problem hiding this comment.
You'll want to call isAccessible() and only call setAccessible() if required, which will avoid calling the permission checks in setAccessible().
d0a643a to
37d6965
Compare
Member
|
Hello @inponomarev I need to think some more about #1558 before adding more calls to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AnnotationUtils.equals(Annotation, Annotation) may incorrectly return false for equal annotations when the annotation type or its members are not reflectively accessible from the caller’s package.
The root cause is twofold:
ReflectiveOperationExceptionsare swallowed and treated as “not equal” by returningfalse, which hides reflection failure.This behavior violates the method’s documented intent to implement the equality semantics defined by
Annotation.equals(Object)and makes the result depend on reflective accessibility rather than annotation content.This issue is tracked as LANG-1815.
Before you push a pull request, review this list:
mvn; that'smvnon the command line by itself.