package com.example.commons.test;标签:JAVA,String,declaredFields,System,PropertyDescriptor,Field,偶然,data,public From: https://www.cnblogs.com/ppx-doc/p/16755398.html
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class Test {
private String name;
private String id;
private String data;
private String p;
public Test(String name, String id, String data, String p) {
this.name = name;
this.id = id;
this.data = data;
this.p = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public static void normal(List<Test> datas, Field[] declaredFields, Class<Test> clz) {
long start = System.nanoTime();
for (Test data : datas) {
data.getData();
data.getId();
data.getName();
data.getP();
}
System.out.printf("normal execute take up time:%d%n", System.nanoTime() - start);
}
public static <T> void fieldReflect(List<T> datas, Field[] declaredFields, Class<T> clz) throws IllegalAccessException {
long start = System.nanoTime();
for (T data : datas) {
for (Field declaredField : declaredFields) {
declaredField.get(data);
}
}
System.out.printf("fieldReflect execute take up time:%d%n",System.nanoTime() - start);
}
public static <T> void methodHandle(List<T> datas, Field[] declaredFields, Class<T> clz) throws Throwable {
long start = System.nanoTime();
List<MethodHandle> methodHandles = methodHandle(declaredFields, clz);
for (T data : datas) {
for (MethodHandle methodHandle : methodHandles) {
methodHandle.invoke(data);
}
}
System.out.printf("methodHandle execute take up time:%d%n", System.nanoTime() - start);
}
public static <T> void propertyDescriptor(List<T> datas, Field[] declaredFields, Class<T> clz) throws InvocationTargetException, IllegalAccessException {
long start = System.nanoTime();
List<PropertyDescriptor> descriptorArrayList = new ArrayList<PropertyDescriptor>();
for (Field declaredField : declaredFields) {
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(declaredField.getName(), clz);
descriptorArrayList.add(propertyDescriptor);
} catch (IntrospectionException e) {
System.err.println("PropertyDescriptor execute error!");
}
}
for (T data : datas) {
for (PropertyDescriptor propertyDescriptor : descriptorArrayList) {
propertyDescriptor.getReadMethod().invoke(data);
}
}
System.out.printf("PropertyDescriptor execute take up time:%d%n", System.nanoTime() - start);
}
public static void main(String[] args) {
ArrayList<Test> tests = new ArrayList<>();
for (int i = 0; i < 500; i++) {
tests.add(new Test("", i + "", "", ""));
}
Field[] declaredFields = Test.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
}
try {
normal(tests, declaredFields, Test.class);
fieldReflect(tests, declaredFields, Test.class);
methodHandle(tests, declaredFields, Test.class);
propertyDescriptor(tests, declaredFields, Test.class);
} catch (Throwable e) {
e.printStackTrace();
}
}
public static <T> List<MethodHandle> methodHandle(Field[] declaredFields, Class<T> clz) throws NoSuchFieldException, IllegalAccessException {
MethodHandles.Lookup lookup = MethodHandles.lookup();
ArrayList<MethodHandle> methodHandles = new ArrayList<>();
for (Field declaredField : declaredFields) {
methodHandles.add(lookup.findGetter(clz, declaredField.getName(), declaredField.getType()));
}
return methodHandles;
}
}