Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotation returns cryptic class names

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.

like image 352
bdorry Avatar asked Sep 01 '09 16:09

bdorry


3 Answers

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());
}
like image 83
Jherico Avatar answered Oct 20 '22 16:10

Jherico


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.

like image 1
Uri Avatar answered Oct 20 '22 17:10

Uri


also.. remember to set this:

@Retention(RetentionPolicy.RUNTIME)

on your annotation so that it lives beyond the compile.

like image 1
Nico Avatar answered Oct 20 '22 15:10

Nico