test2.java

来自「java编程源代码」· Java 代码 · 共 27 行

JAVA
27
字号
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class test2
{
  public static void main( String[] args ) throws Exception
  {
    //access the class annotation
    Class<MyClass> cls = MyClass.class;
    System.out.println( cls.getAnnotation( HelloTag.class ) );

    //access the constructor annotation
    Constructor<MyClass> con = cls.getConstructor((Class[]) null);
    System.out.println(con.getAnnotation(HelloTag.class));

    //access the method annotation
    Method mth = cls.getMethod( "show" );
    System.out.println(mth.getAnnotation(HelloTag.class));

    //access the field annotation
    Field field = cls.getField("name");
    System.out.println(field.getAnnotation(HelloTag.class));
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?