AnnotationInvocationHandler - null check for hashCode / equals / toString computation?
Rafael Winterhalter
rafael.wth at gmail.com
Mon Nov 17 12:42:42 UTC 2014
Thanks for clearing this up Aleksey, I should have posted an example right
away so here is some code that reproduces the NullPointerException where I
would either expect: (a) an IncompleteAnnotationException or (b) a return
value of false. I guess returning false makes more sense as one expects to
being able to call the equals (and hashCode / toString) methods even for
incomplete annotations:
https://gist.github.com/raphw/baeb31d81322d3dba482
By the javadoc documentation of the memberValueEquals method in the
AnnotationInvocationHandler class, I would argue that at least the
resulting NullPointerException is not desired.
Thanks for the clarification, Rafael
2014-11-17 10:52 GMT+01:00 Aleksey Shipilev <aleksey.shipilev at oracle.com>:
> 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