Loading

Java 反射(二)运行时获取类的信息

一、获得类的运行时结构

1. 获得类的名字

getName():包名+类名
getSimpleName():类名

Class c1 = Class.forName("reflection.User");
// 获得类的名字
System.out.println(c1.getName());       //包名+类名
System.out.println(c1.getSimpleName()); //获得类名

运行结果:

reflection.User
User

2. 获得类的属性

获取属性列表

getFields():只能找到public属性
getDeclaredFields():可以找到所有属性

User

class User{
    private String name;
    private int id;
    private int age;
    // getter、setter、构造函数
}

示例:

// 获得类的属性
Field[] fields =c1.getFields();    // 只能找到public属性
Arrays.stream(fields).forEach(System.out::println);

System.out.println("==============");
fields = c1.getDeclaredFields();    // 可以找到所有属性
Arrays.stream(fields).forEach(System.out::println);

运行结果:

private java.lang.String reflection.User.name
private int reflection.User.id
private int reflection.User.age

获取指定属性

getField(String fieldName):public属性
getDeclaredField(String fieldName):所有属性

//获得指定属性的值
Field name = c1.getDeclaredField("name");

运行结果:

private java.lang.String reflection.User.name

3. 获取类的方法

获得类的方法列表

getMethods():获得本类和父类的public方法
getDeclaredMethods():只获得本类的所有方法(包括private的方法)

//获得本类和父类的public方法
Method[] methods = c1.getMethods();
for (Method method : methods) {
    System.out.println(method);
}
System.out.println("==============");
//只获得本类的所有方法
methods = c1.getDeclaredMethods();
for (Method method : methods) {
    System.out.println(method);
}
=====================打印结果=====================
public java.lang.String reflection.User.getName()
public java.lang.String reflection.User.toString()
public void reflection.User.setName(java.lang.String)
public int reflection.User.getId()
public void reflection.User.setAge(int)
public int reflection.User.getAge()
public void reflection.User.setId(int)
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
==============
public java.lang.String reflection.User.getName()
public java.lang.String reflection.User.toString()
public void reflection.User.setName(java.lang.String)
public int reflection.User.getId()
public void reflection.User.setAge(int)
public int reflection.User.getAge()
public void reflection.User.setId(int)

可以发现,使用getMethods()还可以获得父类Object的public方法

获得指定方法

getMethod(String methodName, Class<?> ...parameterTypes):获得本类和父类指定的public方法
getDeclaredMethods(String methodName, Class<?> ...parameterTypes):只获得本类指定的方法(包括private的方法)
其中methodName是方法名,...parameterTypes是参数列表的参数类型的Class对象,因为java中存在函数重载,可以存在多个名字相同的方法,通过参数列表不同进行区分。

// 获得指定方法
Method getName = c1.getMethod("getName", null);
Method setAge = c1.getMethod("setAge", int.class);
System.out.println(getName);
System.out.println(setAge);
=====================打印结果=====================
public java.lang.String reflection.User.getName()
public void reflection.User.setAge(int)

4. 获得的构造器

获得构造器列表

getConstructors():获取public
getDeclaredConstructors():获取所有

Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
    System.out.println(constructor);
}
constructors = c1.getDeclaredConstructors();
for (Constructor constructor : constructors) {
    System.out.println("#" + constructor);
}
public reflection.User()
public reflection.User(java.lang.String,int,int)
#public reflection.User()
#public reflection.User(java.lang.String,int,int)

获得指定构造器

getConstructor(Class<?> ...parameterTypes)
getDeclaredConstructor(Class<?> ...parameterTypes)

Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println("指定: " + declaredConstructor);
指定: public reflection.User(java.lang.String,int,int)

二、通过反射动态创建对象

1. 使用newInstance()

要求

  • 类必须有一个无参数的构造器
  • 类的构造器的访问权限需要足够
// 获得Class对象
Class<?> c1 = Class.forName("reflection.User");
// 创造一个对象
User user = (User) c1.newInstance();// 本质是使用无参构造器

2.先获取构造器再创建对象

// 通过构造器创造对象
Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user2 = (User) constructor.newInstance("秦疆", 1, 18);

三、通过反射调用对象的方法

  1. 获取Method对象method
  2. method.invoke(Object obj, Object ...args),其中obj为方法调用者,args为填入参数
// 通过反射调用普通方法
User user3 = (User) c1.newInstance();
Method setName = c1.getDeclaredMethod("setName", String.class);
// (对象,方法的参数)
setName.invoke(user3,"狂神");

四、通过反射操作对象的属性

  1. 获取Field对象field
  2. field.set(Object obj, Object value),其中obj为方法调用者,value为填入的值

tips: 若操作的是private属性,在set前需要设置访问权限为true

// 通过反射操作属性
User user4 = (User)c1.newInstance();
Field name = c1.getDeclaredField("name");
// 不能直接操作私有属性,需要打开访问权限
name.setAccessible(true);
name.set(user4,"狂神2");

setAccessible(true)还可以提高代码执行效率。

五、通过反射获取泛型信息

method.getParameterTypes(),其中methodMethod类型对象,返回类型为Class<?>[]

1. 获取方法参数类型

public class Test11 {
    public void test01(Map<String, User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String, User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        // 获取泛型参数类型
        Method method = Test11.class.getMethod("test01", Map.class, List.class);
        Class<?>[] parameterTypes = method.getParameterTypes();
        Arrays.stream(parameterTypes).forEach(System.out::println);
    }
}

打印结果:

interface java.util.Map
interface java.util.List

2. 获取方法带泛型的参数类型

method.getGenericParameterTypes(),其中methodMethod类型对象,返回类型为Type[]

Type[] genericParameterTypes = method.getGenericParameterTypes();
Arrays.stream(genericParameterTypes).forEach(System.out::println);

打印结果:

java.util.Map<java.lang.String, reflection.User>
java.util.List<reflection.User>

3. 从参数类型(ParameterizedType)中获取泛型信息

  1. 先强制转换为ParameterizedType(之前先判断类型)
  2. 调用getActualTypeArguments()方法,返回类型为
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
    System.out.println("#"+genericParameterType);
    if(genericParameterType instanceof ParameterizedType){
        Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
        for (Type actualTypeArgument : actualTypeArguments) {
            System.out.println(actualTypeArgument);
        }
    }
}

运行结果:

#java.util.Map<java.lang.String, reflection.User>
class java.lang.String
class reflection.User
#java.util.List<reflection.User>
class reflection.User

获取返回值类型中的泛型也是同理,这里不再赘述。

六、通过反射获取注解信息

我们尝试编写一个模拟业务的pojo类Student2

class Student2{
    private int id;
    private int age;
    private String name;
    // getter、setter、toString
}

先编写两个注解,@Tablekuang@Fieldkuang

  • @Tablekuang用于修饰一个类,用于说明这个类和数据库中的哪个表相对应
  • @Fieldkuang用于说明和数据库的哪个字段对应,类型是什么,长度是多少
// 类的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablekuang{
    String value();
}

// 属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldkuang{
    String columnName();
    String type();
    int length();
}

然后我们使用注解在Student2类上进行标注:

@Tablekuang("db_student")
class Student2{
    @Fieldkuang(columnName = "db_id", type = "int", length = 10)
    private int id;
    @Fieldkuang(columnName = "db_age", type = "int", length = 10)
    private int age;
    @Fieldkuang(columnName = "db_name", type = "varchar", length = 3)
    private String name;
    // getter、setter、toString
}

然后我们进行注解信息的读取。

获取注解

1. 获取类上的注解

Class成员方法
getAnnotations():获得所有注解,返回值为Annotation[]
getAnnotation(Class annotationClass):获得指定注解,返回值为Annotation

Class<?> c1 = Class.forName("reflection.Student2");
// 通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
    System.out.println(annotation);
}

打印结果:

@reflection.Tablekuang(value=db_student)

2. 获取属性上的注解

Field成员方法
getAnnotations():获得所有注解,返回值为Annotation[]
getAnnotation(Class annotationClass):获得指定注解,返回值为Annotation

获取注解中携带的属性值

注解对象.属性名()

// 获得注解的value值
Tablekuang tablekuang = c1.getAnnotation(Tablekuang.class);
System.out.println(tablekuang.value());

// 获得某字段上的注解
Field f = c1.getDeclaredField("id");
Fieldkuang annotation = f.getAnnotation(Fieldkuang.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());

运行结果:

db_student
db_id
int
10
posted @ 2021-08-03 23:23  CodeReaper  阅读(280)  评论(0编辑  收藏  举报