【注解和反射】--05 反射的性能对比、反射操作泛型和注解

反射

05 性能对比分析

下面对比了通过普通new方式、反射方式及关闭Java语言安全检测的反射方式三种情况下,程序的性能(所需时间)。

java 复制代码
package com.duo.reflection;

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

//性能测试
public class Test8 {
    //1.普通方式new
    public static void test1() {
        Person person = new Person();

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            String name = person.getName();
        }
        long endTime = System.currentTimeMillis();

        System.out.println("普通方式(new)调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
    }

    //2.反射方式调用
    public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Person person = new Person();
        Class<? extends Person> c1 = person.getClass();

        Method getName = c1.getMethod("getName",  null);

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            getName.invoke(person, (Object[]) null);
        }
        long endTime = System.currentTimeMillis();

        System.out.println("反射方式调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
    }

    //3.关闭Java语言安全检查调用
    public static void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Person person = new Person();
        Class<? extends Person> c1 = person.getClass();

        Method getName = c1.getMethod("getName",  null);
        getName.setAccessible(true);

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            getName.invoke(person, (Object[]) null);
        }
        long endTime = System.currentTimeMillis();

        System.out.println("关闭安全检测以反射方式调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
    }

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        test1();
        test2();
        test3();
    }
}

运行结果:

由此可见,通过反射方式调用执行会影响程序效率,而采用了setAccessible()关闭语言安全检测之后可以提高效率。

06 反射操作泛型

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

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

    • ParameterizedType:表示一种参数化类型,例如Collection
    • GenericArrayType:表示一种元素类型是参数化类型或者类型变量的数组类型
    • TypeVariable:是各种类型变量的公共父接口
    • WildcardType:代表一种通配符类型表达式
  • 获取泛型信息:

java 复制代码
package com.duo.reflection;

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 Test9 {

    public void test01(Map<String, Person> map, List<Person> list) {}

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

    public static void main(String[] args) throws NoSuchMethodException {
        Method method = Test9.class.getMethod("test01", Map.class, List.class);
        Type[] genericParameterTypes = method.getGenericParameterTypes();  //获得泛型的参数类型

        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("#" + genericParameterType);

            //instanceof:用来在运行时指出对象是否是特定类的一个实例
            if (genericParameterType instanceof ParameterizedType) {
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }

        System.out.println("==========================");
        method = Test9.class.getMethod("test02");
        Type genericReturnType = method.getGenericReturnType();  //获得泛型返回值类型

        if (genericReturnType instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}

运行结果:

07 反射操作注解

  • getAnnotations

  • getAnnotation

  • 了解什么是ORM?

    • Object relationship Mapping --> 对象关系映射

    java 复制代码
    class Student {
    	int id;
    	String name;
    	int age;
    }
    • 上述对应:

      id name age
      001 Li 32
      002 Wang 24
    • 即,类和表结构对应、属性和字段对应、对象和记录对应

  • 反射操作注解:

java 复制代码
package com.duo.reflection;

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

//反射操作注解
public class Test10 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> c1 = Class.forName("com.duo.reflection.men");

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

        //获得(类名的)注解的value值
        Table annotation = c1.getAnnotation(Table.class);
        String value = annotation.value();
        System.out.println(value);

        //获得类指定的注解
        Field id = c1.getDeclaredField("name");
        Attribute annotation1 = id.getAnnotation(Attribute.class);
        System.out.println(annotation1.columnName());
        System.out.println(annotation1.type());
        System.out.println(annotation1.length());
    }
}

@Table("database_men")
class men {

    @Attribute(columnName = "id", type = "int", length = 10)
    private int id;
    @Attribute(columnName = "age", type = "int", length = 10)
    private int age;
    @Attribute(columnName = "name", type = "varchar", length = 3)
    private String name;

    public men() {}

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

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

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

运行结果:


相关推荐
zfj32111 小时前
从源码层面解析一下ThreadLocal的工作原理
java·开发语言·threadlocal
墨笔之风11 小时前
java后端根据双数据源进行不同的接口查询
java·开发语言·mysql·postgres
程序猿阿伟12 小时前
《Python复杂结构静态分析秘籍:递归类型注解的深度实践指南》
java·数据结构·算法
黑白极客12 小时前
怎么给字符串字段加索引?日志系统 一条更新语句是怎么执行的
java·数据库·sql·mysql·引擎
爬山算法12 小时前
Hibernate(32)什么是Hibernate的Criteria查询?
java·python·hibernate
hkNaruto12 小时前
【AI】AI学习笔记:A2A(智能体协作)入门指南:从概念到实践
人工智能·笔记·学习
yj_sharing12 小时前
动手学深度学习softmax回归的笔记
笔记·深度学习·回归
暖暖木头12 小时前
playwright学习笔记
笔记·学习
码农水水12 小时前
中国邮政Java面试:热点Key的探测和本地缓存方案
java·开发语言·windows·缓存·面试·职场和发展·kafka
a程序小傲13 小时前
小红书Java面试被问:TCC事务的悬挂、空回滚问题解决方案
java·开发语言·人工智能·后端·python·面试·职场和发展