使用
定义注解
public class MyAnnotation {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Person {
String name();
}
}
使用注解
public class Main {
@Person(name = "123")
public void print(){
}
public static void main(String[] args) {
Class cl = Main.class;
try {
Method m = cl.getMethod("print",new Class[]{});
Person p = m.getAnnotation(Person.class);
System.out.println(p.name());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
反编译
反编译MyAnnotation.class
public class MyAnnotation
{
public static interface Person
extends Annotation
{
public abstract String name();
}
public MyAnnotation()
{
}
}
反编译注解MyAnnotation$Person.class
public static interface MyAnnotation$Person
extends Annotation
{
public abstract String name();
}
网友评论