当前位置:网站首页>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();
}
边栏推荐
- July training (day 08) - prefix and
- Analog library function
- C# 给Word每一页设置不同文字水印
- 面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
- 语音直播系统——开发推送通知需要遵守的原则
- 七月集訓(第07天) —— 哈希表
- July training (day 17) - breadth first search
- Sentry 2 orbital data download
- 命令提示符启动不了mysql,提示发生系统错误 5。拒绝访问。解决办法
- Read the paper learning to measure changes: full revolutionary Siamese metric networks for scene change detect
猜你喜欢

Nacos做注册中心使用

Eureka 延迟注册的一个坑

Quickly apply JS to customize the effect of lunar phase change

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

NCCL (NVIDIA Collective Communications Library)
![[wechat applet] lunar calendar and Gregorian calendar are mutually converted](/img/6e/ad01756f8da54901a64c5323e4b747.png)
[wechat applet] lunar calendar and Gregorian calendar are mutually converted

The command prompt cannot start mysql, prompting system error 5. Access denied. terms of settlement

拜托!面试请不要再问我 Ribbon 的架构原理

Nacos is used as a registration center

ESP8266-Arduino编程实例-ADC
随机推荐
July training (day 19) - binary tree
工程材料期末考试试卷
ESP8266-Arduino编程实例-中断
七月集训(第12天) —— 链表
语音直播系统——开发推送通知需要遵守的原则
1640. Can you connect to form an array -c language implementation
【CTF】ciscn_2019_es_2
July training (the 26th day) - and check the collection
都什么年代了你还在用 Date
Eureka delayed registration of a pit
1640. 能否连接形成数组-c语言实现
监控神器:Prometheus 轻松入门,真香!
七月集训(第21天) —— 堆(优先队列)
快应用自定义进度条
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination
Day 6 of learning C language
[C language _ study _ exam _ review lesson 3] overview of ASCII code and C language
BGP联邦实验
35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
July training (day 04) - greed