views:

375

answers:

3

How can I retrieve the value of an annotation on the annotated method??

I have:

@myAnnotation(attribute1 = value1, attibute2 = value2)
public void myMethod()
{
  //I want to get value1 here
}
+4  A: 
  1. Obtain Method instance.
  2. Obtain annotation.
  3. Obtain annotation attribute value.

Something like:

Method m = getClass().getMethod("myMethod");
MyAnnotation a = m.getAnnotation(MyAnnotation.class);
MyValueType value1 = a.attribute1();

You'll need to catch / handle the appropriate exceptions, of course. The above assumes you are indeed retrieving method from the current class (replace getClass() with Class.forName() otherwise) and the method in question is public (use getDeclaredMethods() if that's not the case)

ChssPly76
shouldn't it be "MyValueType value1 = a.getAttribute1();"
DJ
or more like "a.attribute()`
Bozho
Certainly should have. Thanks, edited
ChssPly76
+4  A: 

Two important things:

  • There is no way to get the current method, e.g. there is no getMethod() such as getClass(). Therefore, the method accessing its own annotation would need to know its own name.
  • The retention policy of the annotation must be set to RUNTIME, so you can access the annotation at runtime. The default is compile-time, which means annotations are available in the class file, but cannot be accessed at runtime using reflection.

Full example:

@Retention(RetentionPolicy.RUNTIME)
public static @interface MyAnnotation {
 String value1();

 int value2();
}

@Test
@MyAnnotation(value1 = "Foo", value2 = 1337)
public void testAnnotation() throws Exception {
 Method[] methods = getClass().getMethods();
 Method method = methods[0];
 assertEquals("testAnnotation", method.getName());
 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
 assertEquals("Foo", annotation.value1());
 assertEquals(1337, annotation.value2());
}
mhaller
There is a way to get current method. Either `Thread.currentThread().getStackTrace()[2].getMethodName()` or `new Throwable().fillInStackTrace().getStackTrace()[0].getMethodName()` should do it.
ChssPly76
+1 for mentioning Retention
basszero
@ChssPly76, building a stacktrace isn't entirely reliable, as the javadocs mention: "Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information [...] is permitted to return a zero-length array from this method."
gustafc
A: 

@mhaller: a bit too long for a comment on your post. Obviously would need further refinement to deal with overloaded methods, but it is not impossible.:

import java.lang.reflect.Method;

public class Hack {
    public static void main (String[] args) {
     (new Printf()).foobar();
    }
    public void foobar () {
     Method here = getCurrentMethod(this);
     System.out.format("And here we are: %s\n", here);
    }
    public static final Method getCurrentMethod(Object o) {
     String s = Thread.currentThread().getStackTrace()[2].getMethodName();
     Method cm = null;
     for(Method m : o.getClass().getMethods()){
      if(m.getName().equals(s)){
       cm = m; break;
      }
     }
     return cm;
    }
}
I have a feeling that if you actually do this in production code, it summons the Great Old Ones to devour your very soul!
Skip Head
@Skip Head - why? There are two things to be careful about here - (1) array index **in theory** may be different from 2 on some JVMs but that's easy to work around by walking through whole trace; (2) this won't work for overloaded methods; you can do some black magic with javassist to guess the right one based on line number but that really is somewhat iffy. Other then that, though, it works perfectly - how do you think stack traces work?
ChssPly76
@ChssPly76: No need to walk the whole trace. The trace method starts at top and walks until it finds itself. And the caller is the next.
@alphazero - you'd think so, but no :-) That is, this is **normally** the case but it's not spec'ed anywhere AFAIK, meaning you can have a JVM implementation that behaves differently (e.g. the method in question may be in 2nd or even 1st argument of stack trace array)
ChssPly76