Skip to content

Instantly share code, notes, and snippets.

@xsalefter
Created November 4, 2011 16:50
Show Gist options
  • Save xsalefter/1339825 to your computer and use it in GitHub Desktop.
Save xsalefter/1339825 to your computer and use it in GitHub Desktop.
How to proccess annotation member
// The annotation
public @interface HelloMessage {
String value() default "";
}
import java.lang.reflect.Field;
class HelloWorld {
@HelloMessage("hello")
public String message;
}
public class Hello {
public static void main(String...args) throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
HelloWorld hw = new HelloWorld();
hw.message = "world";
Class<HelloWorld> helloClass = HelloWorld.class;
Field field = helloClass.getDeclaredField("message");
String string = field.get(hw.message).toString();
String value = field.getAnnotation(HelloMessage.class).value();
System.out.println(string + " " + value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment