反射机制_提高反射效率_操作泛型_操作注解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

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转