反射机制_提高反射效率_操作泛型_操作注解JAVA213

2024-09-03 14:38

本文主要是介绍反射机制_提高反射效率_操作泛型_操作注解JAVA213,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:http://www.bjsxt.com/
一、S02E213反射机制_提高反射效率、操作泛型、操作注解

反射机制性能问题
反射机制性能问题

package com.test.reflection;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import com.test.bean.User;
/*** 通过跳过安全检查,提高反射效率* 三种执行方法的效率差异比较*/
public class ReflectionRuntime {public static void test01(){User u = new User();long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000L; i++) {u.getUname();}long endTime = System.currentTimeMillis();System.out.println("普通方法调用,执行10亿次,耗时:" + (endTime-startTime) + "ms");}public static void test02() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{User u = new User();Class clazz = u.getClass();Method m = clazz.getDeclaredMethod("getUname", null);//m.setAccessible(true);long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000L; i++) {m.invoke(u, null);}long endTime = System.currentTimeMillis();System.out.println("反射动态方法调用,执行10亿次,耗时:" + (endTime-startTime) + "ms");}public static void test03() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{User u = new User();Class clazz = u.getClass();Method m = clazz.getDeclaredMethod("getUname", null);m.setAccessible(true);//不需要执行访问安全检查long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000L; i++) {m.invoke(u, null);}long endTime = System.currentTimeMillis();System.out.println("反射动态方法调用,跳过安全检查,执行10亿次,耗时:" + (endTime-startTime) + "ms");}public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {test01();test02();test03();}
}
package com.test.bean;public class User {private int id;private int age;private String uname;//javabean必须要有无参的构造方法!public User() {}public User(int id, int age, String uname) {super();this.id = id;this.age = age;this.uname = uname;}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 getUname() {return uname;}public void setUname(String uname) {this.uname = uname;}
}

控制台输出

普通方法调用,执行10亿次,耗时:2168ms
反射动态方法调用,执行10亿次,耗时:60739ms
反射动态方法调用,跳过安全检查,执行10亿次,耗时:12310ms

反射操作泛型(Generic)
反射操作泛型(Generic)

package com.test.reflection;import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;import com.test.bean.User;/*** 通过反射获取泛型信息*/
public class GetGeneric {public void test01(Map<String,User> map,List<User> list){System.out.println("GetGeneric.test01()");}public Map<Integer,User> test02(){System.out.println("GetGeneric.test02()");return null;}public static void main(String[] args) {try {//获取指定方法参数泛型信息Method m = GetGeneric.class.getMethod("test01", Map.class, List.class);Type[] t = m.getGenericParameterTypes();for (Type paramType : t) {System.out.println("#" + paramType);if(paramType instanceof ParameterizedType){Type[] genericTypes = ((ParameterizedType) paramType).getActualTypeArguments();for (Type genericType : genericTypes) {System.out.println("泛型类型:" + genericType);}}}//获取指定方法返回值泛型信息Method m2 = GetGeneric.class.getMethod("test02", null);Type returnType = m2.getGenericReturnType();if(returnType instanceof ParameterizedType){Type[] genericTypes = ((ParameterizedType) returnType).getActualTypeArguments();for (Type genericType : genericTypes) {System.out.println("返回值,泛型类型:" + genericType);}}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();}}
}
package com.test.bean;public class User {private int id;private int age;private String uname;//javabean必须要有无参的构造方法!public User() {}public User(int id, int age, String uname) {super();this.id = id;this.age = age;this.uname = uname;}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 getUname() {return uname;}public void setUname(String uname) {this.uname = uname;}
}

控制台输出

#java.util.Map<java.lang.String, com.test.bean.User>
泛型类型:class java.lang.String
泛型类型:class com.test.bean.User
#java.util.List<com.test.bean.User>
泛型类型:class com.test.bean.User
返回值,泛型类型:class java.lang.Integer
返回值,泛型类型:class com.test.bean.User

反射操作注解(annotation)
反射操作注解(annotation)

package com.test.annotation2;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;/*** 使用反射读取注解的信息,模拟处理注解信息的流程*/
public class Demo {public static void main(String[] args) {try {Class clazz = Class.forName("com.test.annotation2.SxtStudent");//获取类的所有有效注解Annotation[] annotations = clazz.getAnnotations();for (Annotation a : annotations) {System.out.println(a);}//获取类的指定的注解SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class);System.out.println(st.value());//获取类的属性的注解Field f = clazz.getDeclaredField("sname");SxtField sxtField = f.getAnnotation(SxtField.class);System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());//根据获取的表名和字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表} catch (Exception e) {e.printStackTrace();}}
}
package com.test.annotation2;@SxtTable("tb_student")
public class SxtStudent {@SxtField(columnName="id",type="int",length=10)private int id;@SxtField(columnName="sname",type="varchar",length=10)private String sname;@SxtField(columnName="age",type="int",length=3)private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return sname;}public void setStudentName(String studentName) {this.sname = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package com.test.annotation2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {String value();
}
package com.test.annotation2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtField {String columnName();String type();int length();
}

控制台输出

@com.test.annotation2.SxtTable(value=tb_student)
tb_student
sname--varchar--10

这篇关于反射机制_提高反射效率_操作泛型_操作注解JAVA213的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133244

相关文章

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477