[LANG-1815] AnnotationUtils.equals fails for package-private annotations - #1587
[LANG-1815] AnnotationUtils.equals fails for package-private annotations#1587inponomarev wants to merge 2 commits into
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)); |
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); |
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
|
Hello @inponomarev I need to think some more about #1558 before adding more calls to |
|
Hello @inponomarev |
|
@inponomarev 🔔 Ping. |
1 similar comment
|
@inponomarev 🔔 Ping. |
|
@garydgregory Since the original PR appears inactive after follow-up pings, I'd be happy to work on this if the original author is no longer available. |
|
@kzhunmax Sure, go for it 😄 |
|
@inponomarev CC @kzhunmax |
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.