【注解和反射】--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();
}

运行结果:


相关推荐
言慢行善22 分钟前
sqlserver模糊查询问题
java·数据库·sqlserver
专吃海绵宝宝菠萝屋的派大星28 分钟前
使用Dify对接自己开发的mcp
java·服务器·前端
大数据新鸟1 小时前
操作系统之虚拟内存
java·服务器·网络
Tong Z1 小时前
常见的限流算法和实现原理
java·开发语言
凭君语未可1 小时前
Java 中的实现类是什么
java·开发语言
He少年1 小时前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
克里斯蒂亚诺更新1 小时前
myeclipse的pojie
java·ide·myeclipse
迷藏4941 小时前
**eBPF实战进阶:从零构建网络流量监控与过滤系统**在现代云原生架构中,**网络可观测性**和**安全隔离**已成为
java·网络·python·云原生·架构
迷藏4941 小时前
**发散创新:基于Solid协议的Web3.0去中心化身份认证系统实战解析**在Web3.
java·python·web3·去中心化·区块链
qq_433502181 小时前
Codex cli 飞书文档创建进阶实用命令 + Skill 创建&使用 小白完整教程
java·前端·飞书