注解与反射的部分实现操作
动态的创建对象(通过反射)
package reflection;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//动态的创建对象,通过反射
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获取Class对象
Class c1 = Class.forName("reflection.User");
//构造一个对象
//User user =(User) c1.newInstance();
//System.out.println(user);
//通过构造器创建对象
//Constructor constructor = c1.getDeclaredConstructor(String.class,int.class,int.class);
//User user2 =(User) constructor.newInstance("小明", 001, 19);
//System.out.println(user2);
//通过反射调用普通方法
User user3 =(User) c1.newInstance();
//通过反射获得一个方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke : 激活的意思
//(对象 , "方法的值")
setName.invoke(user3,"小明");
System.out.println(user3.getName());
//通过反射操作属性
User user4 =(User) c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性,我们需要关闭程序的私有检测,属性或者方法的setAccessible(true);
name.setAccessible(true);
name.set(user4,"小明2");
System.out.println(user4.getName());
}
}
反射操作注解
package reflection;
import java.lang.annotation.*;
import java.lang.reflect.Field;
//反射操作注解
public class Test04 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("reflection.StudentNow");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获取注解的value的值
TableNow tableNow =(TableNow) c1.getAnnotation(TableNow.class);
String value = tableNow.value();
System.out.println(value);
//获得类指定注解(name属性)
Field field = c1.getDeclaredField("name");
FieldNow annotation = field.getAnnotation(FieldNow.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@TableNow("db_student")
class StudentNow{
@FieldNow(columnName = "db_id", type = "int", length = 10)
private int id;
@FieldNow(columnName = "db_age", type = "int", length = 10)
private int age;
@FieldNow(columnName = "db_name", type = "varchar", length = 3)
private String name;
public StudentNow() {
}
public StudentNow(int id, int age, String name) {
this.id = id;
this.age = age;
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableNow{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldNow{
String columnName();
String type();
int length();
}