元注解
自定義注解
使用@interface自定義注解時,自動繼承了java.lang.annotation.Annotation接口
分析:
-
@interface用來聲明一個注解,格式:public @ interface注解名{定義內容}
-
其中的每一個方法實際上是聲明了一個配置參數
-
方法的名稱就是參數的名稱
反射
java Reflection
反射得類方法:
1.
public class Test03 {
public static void main(String[] args) throws Exception{
//通過反射獲取類的class對象
Class c1 = Class.forName("com.dong.annotation.User");
System.out.println(c1);
//一個類在內存中只有一個class對象
//一個類被加載后,類的整個結構都會被封裝在class對象中
}
}
class User{
private String name;
private int id;
private int age;
public User(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public class Test04 {
public static void main(String[] args) {
Person person=new Student();
System.out.println("這個人是"+person.name);
//方式一:通過對象獲得
Class c1 = person.getClass();
System.out.println(c1);
System.out.println(c1.hashCode());
//方拾二:forName獲得
try {
Class c2 = Class.forName("com.dong.annotation.Student");
System.out.println(c2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//方式三:通過類名.class獲得
Class c3 = Student.class;
System.out.println(c3);
//方式四:基本內置類型里的包裝類有一個type屬性
Class c4 = Integer.TYPE;
System.out.println(c4);
//方式五:獲得父類類型
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
}
class Person{
String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
JVM
public static void main(String[] args) throws Exception {
//通過反射獲取類的class對象
Class c1 = Class.forName("com.dong.annotation.User");
//獲取構造方法
Constructor[] constructors = c1.getConstructors();//非私有外所有構造方法
Constructor constructor = c1.getConstructor(String.class,Integer.class,Integer.class);//指定構造方法
Constructor[] declaredConstructors = c1.getDeclaredConstructors();//所有構造方法
//獲取普通方法
Method[] methods = c1.getMethods();//非私有普通方法
Method[] declaredMethods = c1.getDeclaredMethods();//所有普通方法
Method declaredMethod = c1.getDeclaredMethod("study",String.class);//指定普通方法
//獲取屬性Field
Field[] fields = c1.getDeclaredFields();//全部屬性
c1.getFields();//非私有屬性
c1.getField("");//指定屬性
c1.getField("").get("");//屬性名 --屬性類型String.class --獲取屬性值
}
}
//動態的創建對象,通過反射
public class Test08 {
public static void main(String[] args) throws Exception {
//獲得class對象
Class c1 = Class.forName("com.dong.annotation.User");
//構造一個對象
User user=(User)c1.newInstance();//調用了類的無參構造器
//通過構造器創建對象
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user1 =(User) declaredConstructor.newInstance("趙東", 001, 18);
//通過反射調用普通方法
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(user,"趙東");//設置name參數
//invoke 激活的意思
//通過反射操作屬性
User user2=(User)c1.newInstance();
Field name = c1.getDeclaredField("name");
name.setAccessible(true);//設置是否取消私有檢測
name.set(user2,"趙東2");//設置name屬性
}
}
反射操作泛型
通過反射獲取注解信息
public class Test10 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.dong.annotation.Student2");
//通過反射獲得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//獲得注解的value值
TableNote tableNote = (TableNote) c1.getAnnotation(TableNote.class);
System.out.println(tableNote.value());//獲得並打印注解的值
//獲得類指定的注解
Field name = c1.getDeclaredField("name");//要獲得的類型
FieldNote annotation = name.getAnnotation(FieldNote.class);//獲取值的方法
System.out.println(annotation.columnName()+annotation.type()+annotation.length());
}
}