Question

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.

Was it helpful?

Solution

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());
}

OTHER TIPS

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.

also.. remember to set this:

@Retention(RetentionPolicy.RUNTIME)

on your annotation so that it lives beyond the compile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top