AnnotationInvocationHandler - null check for hashCode / equals / toString computation?
Aleksey Shipilev
aleksey.shipilev at oracle.com
Mon Nov 17 09:52:33 UTC 2014
Hi Joe,
I am missing something. Of course, JLS requires the reference types
members of annotations to be non-null. But IncompleteAnnotationEx is
here to cover the case when the annotation is mis-constructed.
Therefore, I think the original Rafael's question is still unanswered. I
can see how the NPE can be produced while calling equals on
mis-constructed annotation backed by AnnotationInvocationHandler:
private Boolean equalsImpl(Object o) {
...
for (Method memberMethod : getMemberMethods()) {
// no value, returns null
Object ourValue = memberValues.get(member);
...
if (!memberValueEquals(ourValue, hisValue))
...
}
...
}
private static boolean memberValueEquals(Object v1, Object v2) {
Class<?> type = v1.getClass(); // NPE here
...
}
Like Rafael, I wonder if we can/should throw the
IncompleteAnnotationException when accessing the annotation member
implicitly with equals(), not with the named accessor.
-Aleksey.
On 11/16/2014 09:20 PM, joe darcy wrote:
> Hello,
>
> The reference type members of annotations are all required to be non-null.
>
> Cheers,
>
> -Joe
>
> On 11/14/2014 1:51 AM, Rafael Winterhalter wrote:
>> For reasons I am not going into, I am implementing my own
>> AnnotationInvocationHandler and I want it to be compatible to the
>> OpenJDK's
>> annotation invocation handler with respect to the hash code and equals
>> functionality, thus I just looked at the source in more detail. During
>> implementing the handler, I realized that:
>>
>> 1. The invoke method applies a null check for an annotation value when
>> looking it up for explicitly reading an annotation property and throws an
>> IncompleteAnnotationException if no value and no default value is
>> provided.
>>
>> 2. No such checks are applied when invoking the hashCode / equals /
>> toString methods for the annotation which all have their own handler
>> method. This would result in a NullPointerException for all of them.
>>
>> I did not find any formal specification for what is supposed to
>> happen. Is
>> it correct to throw these exceptions or should it rather also be an
>> IncompleteAnnotationException?
>>
>> Thanks for the info! Rafael
>
More information about the core-libs-dev
mailing list