狂神Java学习笔记——注解和反射(二)

获取运行时类的完整结构

  • Field、Method、Constructor、Superclass、Interface、Annotation
//获得类的信息
public class Test1 {
    public static void main(String[] args) throws Exception {
        Thread thread=new Thread();
        Class c1=thread.getClass();
        //类的名字
        System.out.println(c1.getName());//包名+类名
        System.out.println(c1.getSimpleName());//类名
        //获得属性
        System.out.println("-----------------------------------------");
        Field[] field1=c1.getFields();//只能找到public属性
        for (Field f :field1) {
            System.out.println(f);
        }
        Field[] field2=c1.getDeclaredFields();//获得所有属性
        for (Field f :field2) {
            System.out.println(f);
        }
        //获得指定的属性
        Field tid=c1.getDeclaredField("tid");
        System.out.println(tid);

        //获得类的方法
        System.out.println("--------------------");
        Method[] methods=c1.getMethods();//只能获取public方法(本类和父类)
        for (Method method:methods) {
            System.out.println("1"+method);
        }
        methods=c1.getDeclaredMethods();//可以获得所有方法(只有本类)
        for (Method method:methods) {
            System.out.println("2"+method);
        }
        //获得指定方法只需要在()中添加参数(方法名,方法参数类)
        //获得指定构造器
        System.out.println("-------------------------");
        Constructor[] constructors=c1.getConstructors();//获得public方法
        for (Constructor c :constructors) {
            System.out.println("#"+c);
        }
        constructors=c1.getDeclaredConstructors();//获得所有方法
        for (Constructor c :constructors) {
            System.out.println("$"+c);
        }
        //获得指定构造器
        c1.getDeclaredConstructor(String.class);
        
    }
}

有了Class对象能做什么

  • 创建类的对象
    • 调用Class对象的newInstance()方法
      • 必须要有一个无参数的构造器
      • 类的构造器的访问权限要够
    • 通过获取Class对象的构造器创建
      • 通过Class对象的getDeclaredConstructor()方法获取本类指定参数类型的构造器(上一节有讲)
      • 向构造器传入一个对象数组,其中包含此构造器所需要的各个参数
      • 通过Constructor实例化对象
  • 调用普通方法
  • 获取和修改属性
public class Test2 {

    public static void main(String[] args) throws Exception {
        Class c=String.class;
        //构造一个对象
        String s1=(String) c.newInstance();//本质上是调用了类的无参构造器
        //通过构造器创建对象
        Constructor constructor=c.getDeclaredConstructor(String.class);
        String s2= (String) constructor.newInstance("123");
        System.out.println(s2);


        //通过反射调用普通方法
        //通过反射获取方法
        Method method= c.getMethod("equals", Object.class);
        boolean b=(boolean)method.invoke(s2, "123");
        System.out.println(b);
        //通过反射操作属性
        Field hash=c.getDeclaredField("hash");
        hash.setAccessible(true);//private修饰的属性不能被修改,但是可以通过这句话忽略权限
        System.out.println(s2.hashCode());
        hash.set(s2,1);//修改属性值
        System.out.println(s2.hashCode());

    }
}
  • 注意:其中setAccessible(true)方法调用后会关闭对应属性、方法的安全检查,但会改善反射调用的效率问题
public class Test3 {
   public static void main(String[] args) throws Exception {
       test1();
       test2();
       test3();
   }
   //普通直接调用方法
   public static void test1(){
       String s="123";
       long startTime=System.currentTimeMillis();
       for (int i = 0; i < 1000000000; i++) {
           s.hashCode();
       }
       long endTime=System.currentTimeMillis();
       System.out.println("直接调用消耗了"+(endTime-startTime)+"ms");
   }
   //通过反射而不关闭安全检查
   public static void test2() throws Exception {
       String s="123";
       Method hashCode= s.getClass().getDeclaredMethod("hashCode",null);
       long startTime=System.currentTimeMillis();
       for (int i = 0; i < 1000000000; i++) {
           hashCode.invoke(s,null);
       }
       long endTime=System.currentTimeMillis();
       System.out.println("反射调用消耗了"+(endTime-startTime)+"ms");
   }
   //通过反射而关闭安全检查
   public static void test3() throws Exception {
       String s="123";
       Method hashCode= s.getClass().getDeclaredMethod("hashCode",null);
       hashCode.setAccessible(true);
       long startTime=System.currentTimeMillis();
       for (int i = 0; i < 1000000000; i++) {
           hashCode.invoke(s,null);
       }
       long endTime=System.currentTimeMillis();
       System.out.println("关闭检查消耗了"+(endTime-startTime)+"ms");
   }
}

  • 反射操作泛型
  • Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转换的问题,但是,一旦编译完成,所有和泛型有关的类型全部擦除。
  • 为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但是又和原始类型齐名的类型。
类型说明
ParameterizedType表示一种参数化类型,比如Collection< String >
GenericArrayType表示一种元素类型是参数化类型或者类型变量的数组类型
TypeVariable是各种类型变量的公共父接口
WildcardType代表一种通配符类型表达式
//通过反射获取泛型
public class Test4 {
    public void  test1(Map<String,Integer> map, List<Integer> list){
        System.out.println("test1");
    }
    public  Map<String,Integer> test2(){
        System.out.println("test2");
        return null;
    }
    public static void main(String[] args) throws Exception {
        //获得参数类型
        Method method = Test4.class.getMethod("test1", Map.class, List.class);
        //获得参数类型即Map和List
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("#"+genericParameterType);
            if (genericParameterType instanceof ParameterizedType){
                //获得Map和List中的泛型类型
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println("    "+actualTypeArgument);
                }
            }
        }

        //获得返回值类型
        method =Test4.class.getMethod("test2",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);
            }
        }
    }
}

  • 反射操作注解
  • 练习ORM——Object Relationship Mapping–>对象关系映射
    • 类和表结构对应
    • 属性和字段对应
    • 对象和记录对应
  • 利用注解和反射完成类和表结构的映射关系
//练习反射操作注解
public class Test5 {
    public static void main(String[] args) throws Exception {
        Class c1 = Class.forName("Day9.Student");
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获得注解value值
        MyTable annotation =(MyTable)c1.getAnnotation(MyTable.class);
        System.out.println(annotation.value());
        //获得类指定的注解
        Field name = c1.getDeclaredField("name");
        MyField annotation1 = name.getAnnotation(MyField.class);
        System.out.println(annotation1.columnName());
        System.out.println(annotation1.type());
        System.out.println(annotation1.length());
    }
}

@MyTable("db_student")
class Student{
    @MyField(columnName ="db_id",type = "int",length =10)
    private int id;
    @MyField(columnName ="db_age",type = "int",length =10)
    private int age;
    @MyField(columnName ="db_name",type = "varchar",length =3)
    private String name;
    public Student() {
    }
    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyTable{
    String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyField{
    String columnName();
    String type();
    int length();
}
  • 1
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值