Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Reflective Access to @NonNull
Reflective Access to @NonNull [message #1060365] Fri, 24 May 2013 10:07 Go to next message
Ed Willink is currently offline Ed Willink
Messages: 4116
Registered: July 2009
Senior Member
Hi

I'm trying to determine if a method has an @NonNull return

I would expect to just do

myMethod.getAnnotation(NonNull.class)

but the return is always null even though org.eclipse.jdt.annotation is
on the class path and the method has an @NonNull annotation.

Is there some magic command line option to activate annotations within a
JUnit launch?

Is there some magic command line option to avoid annotations being
stripped out somewhere?

[I've exported org.eclipse.jdt.annotation from the plugin containing the
annotated method.]

Regards

Ed Willink
Re: Reflective Access to @NonNull [message #1060419 is a reply to message #1060365] Fri, 24 May 2013 14:48 Go to previous messageGo to next message
Brian Vosburgh is currently offline Brian Vosburgh
Messages: 132
Registered: July 2009
Senior Member
No Message Body

[Updated on: Fri, 24 May 2013 14:56]

Report message to a moderator

Re: Reflective Access to @NonNull [message #1060420 is a reply to message #1060419] Fri, 24 May 2013 14:49 Go to previous messageGo to next message
Brian Vosburgh is currently offline Brian Vosburgh
Messages: 132
Registered: July 2009
Senior Member
No Message Body

[Updated on: Fri, 24 May 2013 14:56]

Report message to a moderator

Re: Reflective Access to @NonNull [message #1060421 is a reply to message #1060420] Fri, 24 May 2013 14:53 Go to previous messageGo to next message
Brian Vosburgh is currently offline Brian Vosburgh
Messages: 132
Registered: July 2009
Senior Member
[Wow - serious browser issues - sorry about the incomplete responses above....]

The RetentionPolicy for @NonNull is CLASS (as opposed to SOURCE or RUNTIME); which means the annotation is available for the compiler (either via the source file, obviously, or the class file), but the annotation is not loaded by the JVM and available at runtime via reflection.
Re: Reflective Access to @NonNull [message #1060443 is a reply to message #1060421] Fri, 24 May 2013 16:26 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1024
Registered: July 2009
Senior Member
Right, @NonNull is not available at runtime.

The reason being: people want to be able to run the code without linking the annotation library. Makes sense?

If the JUnit has access to the class files it could, however, use your favorite bytecode library (BCEL, ASM, ...) to retrieve the annotation from there. Would that be feasible for your case?
Re: Reflective Access to @NonNull [message #1060446 is a reply to message #1060443] Fri, 24 May 2013 16:59 Go to previous message
Ed Willink is currently offline Ed Willink
Messages: 4116
Registered: July 2009
Senior Member
Hi

Thanks for the explanation.

At one level I agree, at another, I regard @NonNull as part of the type
system and so it should be available just like the rest of it.

I have no favourite bytecode library. Never used any of them. Can you
recommend one that is in Orbit or better?

org.objectweb.asm?
org.apache.xbean.asm?
org.apache.bcel?

Any short cuts to a cookbook example just like
myMethod.getAnnotation(NonNull.class)?

FYI. My OCL to Java codegen plugin generates Java code with annotations
that indicate the internal non-null and non-invalid analysis. However
the CG analysis knows more than Java and so spurious warnings need to be
suppressed by auto-generating @suppressWarnings("null").

Regards

Ed Willink


On 24/05/2013 17:26, Stephan Herrmann wrote:
> Right, @NonNull is not available at runtime.
> The reason being: people want to be able to run the code without
> linking the annotation library. Makes sense?
>
> If the JUnit has access to the class files it could, however, use your
> favorite bytecode library (BCEL, ASM, ...) to retrieve the annotation
> from there. Would that be feasible for your case?
>
Previous Topic:Could not connect to New MySQL.
Next Topic:Issue with Extension point for action
Goto Forum:
  


Current Time: Sat Nov 01 03:29:38 GMT 2014

Powered by FUDForum. Page generated in 0.06096 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software