views:

305

answers:

3

I am somewhat new to Java so perhaps I misunderstand the use cases for annotations in java. My issue is the following:

After annotating a method I receive class names such as $Proxy31 when inspecting the annotations on the method. I am curious why I am receiving class names for my annotations that are similar to this, and what I can do to fix this problem.

Method m = this.remoteServiceClass.getMethod(rpcRequest.getMethod().getName());
RequiredPermission a = m.getAnnotation(RequiredPermission.class);

This returns a null annotation even though I know that the method it is looking up has the RequiredPermission annotation implemented.

for(Annotation a : m.getAnnotations())
{
    System.out.println(a.getClass().getName());
}

This prints out the $Proxy31 class names.

+6  A: 

Given Annotation a, you need to call annotationType(), not getClass() to determine the type of the annotation. An Annotation object is just a proxy that represents that instance of the annotation on that class.

Object o = ...;
Class c = o.getClass();
Annotation[] as = c.getAnnotations();
for (Annotation a : as) {
   // prints out the proxy class name
   System.out.println(a.getClass().getName());
   // prints out the name of the actual annotation
   System.out.println(a.annotationType().getName());
}
Jherico
(Because an annotation is an interface and therefore an instance can't be a concrete runtime class.)
Tom Hawtin - tackline
That's incorrect. An interface still has a Class object associated with it.
Jherico
@Jherico, that is true, but an actual runtime instance won't have an interface as the result of its getClass() method.
Yishai
A: 

When you add annotations in the source code, Java actually creates a bunch of interfaces and classes "under the hood" to allow you (or your tools) to ask the program things about the annotations using restrictions. Method annotations create "dyanmic proxies", and accordingly Java creates classes for you, probably with the name Proxy.

If you are interested in this, read on java.lang.reflect.InvocationHandler and its subtype, AnnotationInvocationHandler

That being said, you should not have to worry about what Java actually generates. I suspect you are not using reflection correctly to inspect your annotations from within a Java program.

Uri
+1  A: 

also.. remember to set this:

@Retention(RetentionPolicy.RUNTIME)

on your annotation so that it lives beyond the compile.

Nico