Created
November 4, 2011 16:50
-
-
Save xsalefter/1339825 to your computer and use it in GitHub Desktop.
How to proccess annotation member
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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