Java基础知识点__获取运行时类的完整结构

通过反射获取运行时类的完整结构

Field,method,Construuuctor,Superclass,Interface,Annotation

实现的全部接口
继承的父类
全部的构造器
全部的构造方法
全部的Field
注解等


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestDemo02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {

       Class aClass =  Class.forName("com.linke.User");

        System.out.println("=================== 获取类名 ===================");
        //获取类的名字
        System.out.println(aClass.getName()); //获得包名+类名
        System.out.println(aClass.getSimpleName()); //获得类名

        System.out.println("=================== 获取属性 ===================");
        //或得类的属性 获取public属性的
        Field[] fields = aClass.getFields();
        for (Field file : fields) {
            System.out.println(file);
        }

        //获取类的属性,获取全部属性的
        Field [] declaredFields =  aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        System.out.println("=================== 获取方法 ===================");

        //获得类的方法 获取public的方法,父类的也会被打印出来
        Method [] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println("public method:" + method);
        }

        //获取类的方法 获取全部的方法,只打印自己的
        Method [] declaredMethods = aClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println("all method:" + method);
        }
        System.out.println("=================== 获取指定方法 ===================");
        //获取指定的方法
        Method getName = aClass.getMethod("getName",null);
        Method setName = aClass.getMethod("setName",String.class);
        System.out.println(getName);
        System.out.println(setName);

        System.out.println("=================== 获取指定的构造器 ===================");
        //获取public修饰的构造方法
        Constructor [] constructors = aClass.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        //获取全部的构造函数
        Constructor []  declaredConstructor = aClass.getDeclaredConstructors();
        for (Constructor constructor : declaredConstructor) {
            System.out.println(constructor);
        }

        //获取指定的构造函数
        Constructor constructor = aClass.getConstructor();
        Constructor constructor1 = aClass.getConstructor(String.class,String.class,String.class);
        System.out.println(constructor);
        System.out.println(constructor1);
    }
}

public class User {

    private String Id;

    private String name;

    private String age;

    public User() {
        super();
    }

    public User(String id, String name, String age) {
        Id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    
 	@Override
    public String toString() {
        return "User{" +
                "Id='" + Id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

总结:进过小测试,实际上是可以通过Class获取想要的内容的,但是不会经常开发。

使用反射操作类对象的相关内容


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestDemo03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {

        //获得Class对象
       Class aClass =  Class.forName("com.linke.User");
       //构造一个对象
        User user = (User) aClass.newInstance(); //过时了 jdk9版本
        //推荐使用 jdk9后就推荐使用该方法 如果没有无参构造器,调用该方法会报错,并且无参要保证访问权限足够
        User user1 = (User) aClass.getDeclaredConstructor().newInstance();
        System.out.println(user);
        System.out.println(user1);

        //通过构造器创建对象
        Constructor constructor = aClass.getDeclaredConstructor(String.class,String.class,String.class);
        User user2 = (User) constructor.newInstance("1233455","林可","18");
        System.out.println(user2);

        //通过反射调用普通方法
        User user3 = (User)aClass.getDeclaredConstructor().newInstance();
        Method setName = aClass.getDeclaredMethod("setName",String.class);
        setName.invoke(user3,"王老五");
        System.out.println(user3);

        //通过反射操作属性
        User user4 = (User)aClass.getDeclaredConstructor().newInstance();
        Field field = aClass.getDeclaredField("name");
        field.setAccessible(true); //开启权限,因为访问权限是private,提升运行效率,但是降低了安全性
        field.set(user4,"齐全");
        System.out.println(user4);
       
    }
}

setAccessible 了解

method和Field,Constructor对象都有setAccessible()方法
setAccessible作用是启动和禁用访问安全检查的开关
设置为true

  • 提高反射的效率,如果代码中必须使用反射,而该句代码需要频繁频繁调用,那么设置为true
  • 使得原本无法访问的私有成员也可以访问

参数值为false则指示反射对象应该实施Java语言访问检查

效率测试demo


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestDemo04 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {

     //普通方式调用
     ordinaryMethod();

     //反射方式调用
     reflexMethod();

     //反射方式调用 关闭检查
     reflexMethod2();

    }

    public static  void ordinaryMethod(){
     User user = new User();
     long startTime = System.currentTimeMillis();
     for (int i = 0; i < 1000000000; i++) {
      user.getName();
     }
     long endTime  = System.currentTimeMillis();
     System.out.println("普通方法执行1000000000次需要的时间" + (endTime - startTime)+"ms" );
    }

    public static void  reflexMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
       User user = new User();
       Class aClass = user.getClass();
       Method getName =   aClass.getDeclaredMethod("getName");
       long startTime = System.currentTimeMillis();
       for (int i = 0; i < 1000000000; i++) {
         getName.invoke(user);
       }
       long endTime  = System.currentTimeMillis();
       System.out.println("反射方式调用1000000000次需要的时间" + (endTime - startTime)+"ms" );
    }

   public static void  reflexMethod2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
      User user = new User();
      Class aClass = user.getClass();
      Method getName =   aClass.getDeclaredMethod("getName");
      long startTime = System.currentTimeMillis();
      getName.setAccessible(true);
      for (int i = 0; i < 1000000000; i++) {
         getName.invoke(user);
      }
      long endTime  = System.currentTimeMillis();
      System.out.println("反射方式调用 关闭检查,调用1000000000次需要的时间" + (endTime - startTime)+"ms" );

   }

}

反射操作泛型(了解扩展)

Java采用泛型擦除的机制引入泛型,Java中的泛型仅仅是给编译Javac使用,确保数据的安全性和免去强制类型转换问题。
但是,一旦编译完成,所有和泛型有关的类型全部擦除

为了通过反射操作这些类型,Java新增了ParameterizedType,GenercArrayType,TypeVarizble和WuldcardType几种类型来代表不能被归一到Class类中的类型,但是又和原始类型齐名的类型

  • ParameterizedType: 表示一种参数化类型,比如Collection
  • GenericArrayType: 表示一种元素类型是参数化类型或者类型变量的数组类型
  • TypeVarizble: 是各种类型变量的公共父接口
  • WildcardType: 代表一种通配符类型表达

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class TestDemo5 {
    public static void main(String[] args) throws NoSuchMethodException {
        Class aclass = TestDemo5.class;

        Method method = aclass.getMethod("test01",Map.class,List.class);
        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);
                }
            }
        }

        System.out.println("===============获取返回值泛型====================");

         method = aclass.getMethod("test02",null);
         Type genericReturnType  =   method.getGenericReturnType();
        System.out.println("###@@"+genericReturnType);
        if(genericReturnType instanceof ParameterizedType){
            Type [] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println("~~~~~"+actualTypeArgument);
            }
        }
    }

    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

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

}

反射操作注解


import java.lang.annotation.*;
import java.lang.reflect.Field;

//练习
public class TestDemo06 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
      Class aClass =  Class.forName("com.linke.Student2");

      //通过反射获得注解
      Annotation [] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解的value的值
        Linkes linkeField = (Linkes)aClass.getAnnotation(Linkes.class);
        System.out.println(linkeField.value());

        //获得类指定的注解
        Field field = aClass.getDeclaredField("name");
        System.out.println(field);

        LinkeField linkeField1 = field.getAnnotation(LinkeField.class);
        System.out.println(linkeField1.columnName());
        System.out.println(linkeField1.type());
        System.out.println(linkeField1.length());

    }

}

@Linkes("db_student")
class Student2{

    @LinkeField(columnName ="db_id",type = "int", length = 10 )
    private int id;
    @LinkeField(columnName ="db_age",type = "int", length = 3 )
    private int age;
    @LinkeField(columnName ="db_name",type = "int", length = 100 )
    private String name;

    public Student2() {
    }

    public Student2(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 "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Linkes{
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface  LinkeField{
    String columnName();
    String type();
    int length();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值