JDK5開始增加元數據支持,也就是Annotation
List list = new ArrayList<Integer>();
list.add(10);
List<String> temp = list;
public enum RetentionPolicy {
/* Annotations are to be discarded by the compiler.* 編譯時丟棄/
SOURCE,
/* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.* 保留在calss文件中,JVM運行時丟棄/
CLASS,
/* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
* @see java.lang.reflect.AnnotatedElement 保留在class文件中,JVM運行時也會保留,用反射讀取*/
RUNTIME
}
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal(adj.正式的;拘謹的;有條理的,[專業:形式{形參}]) parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**Type parameter declaration
* @since 1.8*/
TYPE_PARAMETER,
/**Use of a type
* @since 1.8*/
TYPE_USE
}
(1)利用對象調用getClass()方法獲得Class實例
(2)利用Class類的靜態的forName()方法,使用類名獲得Class實例
(3)運用.class的方式獲得Class實例,如:類名.class
//類對象.getClass().getMethod("必須是用public標識的方法").getAnnotations())
Annotation[] aArray = Class.forName("類名").getMethod("必須是public標識的方法").getAnnotations();
但是要注意的是隻有public標識的纔可以,包括默認包行爲的都不可以
to be continue;