当前位置:网站首页>Annotation and reflection
Annotation and reflection
2022-07-27 09:37:00 【Look at the bugs】
Performance analysis
The ordinary way > Reflection off detection > Reflection
package TestReflected;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// Analyze performance issues
public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
test01();
test02();
test03();
}
// Call... In a normal way
public static void test01() {
User user = new User();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
user.getName();
}
long endTime = System.currentTimeMillis();
System.out.println(" The common way to perform 1000000 Time needed :" + (endTime - startTime) + "ms");
}
// Reflection mode call
public static void test02() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User01 user01 = new User01();
Class C1 = Class.forName("User01");
Method getName = C1.getDeclaredMethod("getName", null);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
getName.invoke(user01, null);
}
long endTime = System.currentTimeMillis();
System.out.println(" Reflection method execution 1000000 Time needed :" + (endTime - startTime) + "ms");
}
// Reflection mode call , Turn off detection
public static void test03() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User01 user01 = new User01();
Class C1 = Class.forName("User01");
Method getName = C1.getDeclaredMethod("getName", null);
getName.setAccessible(true);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
getName.invoke(user01, null);
}
long endTime = System.currentTimeMillis();
System.out.println(" Reflection method , Turn off test execution 1000000 Time needed :" + (endTime - startTime) + "ms");
}
}
Manipulate objects through reflection
package TestReflected;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// Creating objects dynamically , By reflection
public class Test07 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
// get Class object
Class c1 =User.class;
// Construct an object
// User user = (User) c1.getDeclaredConstructor().newInstance();
// System.out.println(user);
// Create objects through constructors
Constructor constructor = c1.getDeclaredConstructor(int.class, String.class);
User user2 = (User) constructor.newInstance(18, "yan");
System.out.println(user2);
// Call normal methods through reflection
User user3=(User)c1.newInstance();
// Get a method by reflection
Method setName =c1.getDeclaredMethod("setName", String.class);
//invoke: Activation means
//( object ,"" The value of the method )
setName.invoke(user3,"yan");
System.out.println(user3.getName());
// Manipulate attributes by reflection
User user4=(User)c1.newInstance();
Field name = c1.getDeclaredField("name");
// Private properties cannot be manipulated directly , We need to turn off the security check of the program , attribute / Methodical setAccessible(true)
name.setAccessible(true); //
name.set(user4,"yan2");
System.out.println(user4.getName());
}
}
Annotate with reflection operation
package TestReflected;
// Practice reflection operation notes
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test09 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class C1 = Class.forName("TestReflected.student2");
// Get annotations by reflection
//System.out.println(C1.getDeclaredAnnotations());
Annotation[] annotations = C1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
// Get annotations value Value
TableYan tableYan = (TableYan)C1.getAnnotation(TableYan.class);
String value = tableYan.value();
System.out.println(value);
// Get the annotation specified by the class
Field f = C1.getDeclaredField("name");
FieldYan annotation = f.getAnnotation(FieldYan.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@TableYan("db_Student")
class student2{
@FieldYan(columnName = "db.id",type = "int",length = 10)
private int id;
@FieldYan(columnName = "db.age",type = "int",length = 10)
private int age;
@FieldYan(columnName = "db.name",type = "String",length = 10)
private String name;
public student2() {
}
public student2(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 "student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
// Annotation of class name
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableYan{
String value();
}
// Comment for attribute
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldYan{
String columnName();
String type();
int length();
}
边栏推荐
猜你喜欢

聊聊索引失效的10种场景,太坑了

抢了个票,还以为发现了12306的系统BUG

【武汉理工大学】考研初试复试资料分享

ESP8266-Arduino编程实例-PWM

快应用自定义进度条

C# 给Word每一页设置不同文字水印

Proposed relocation! 211 the new campus of China University of Petroleum (East China) is officially opened!

Monitoring artifact: Prometheus easy to get started, really fragrant!

好久不送书,浑身不舒服

645. Wrong set
随机推荐
七月集训(第17天) —— 广度优先搜索
材料工程基础-重点
都什么年代了你还在用 Date
[C language - zero basis _ study _ review _ lesson 5] operational properties of basic operators
七月集训(第13天) —— 双向链表
Lua function nested call
BGP的社团属性
七月集训(第18天) —— 树
IDL calls 6S atmospheric correction
[C language - zero foundation lesson 13] the mystery of string
Eureka 延迟注册的一个坑
ArcGIS pro2.8 deep learning environment configuration based on rtx30 graphics card
七月集训(第08天) —— 前缀和
swagger-editor
七月集训(第03天) —— 排序
[cloud native] how can I compete with this database?
Day 6 of learning C language
The third day of learning C language
[C language - zero foundation lesson 14] variable scope and storage class
July training (day 21) - heap (priority queue)