下面对比了通过普通new方式、反射方式及关闭Java语言安全检测的反射方式三种情况下,程序的性能(所需时间)。
package com.duo.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//性能测试
public class Test8 {
//1.普通方式new
public static void test1() {
Person person = new Person();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
String name = person.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通方式(new)调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
}
//2.反射方式调用
public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person person = new Person();
Class<? extends Person> c1 = person.getClass();
Method getName = c1.getMethod("getName", null);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
getName.invoke(person, (Object[]) null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方式调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
}
//3.关闭Java语言安全检查调用
public static void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person person = new Person();
Class<? extends Person> c1 = person.getClass();
Method getName = c1.getMethod("getName", null);
getName.setAccessible(true);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000000; i++) {
getName.invoke(person, (Object[]) null);
}
long endTime = System.currentTimeMillis();
System.out.println("关闭安全检测以反射方式调用执行1亿次所需时间:" + (endTime - startTime) + "ms");
}
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
test1();
test2();
test3();
}
}
运行结果:
由此可见,通过反射方式调用执行会影响程序效率,而采用了setAccessible()关闭语言安全检测之后可以提高效率。
Java采用泛型擦除的机制来引入泛型,Java中的泛型仅仅是给编译器javac使用的,确保数据的安全性和免去强制类型转换问题,但是一旦编译完成,所有和泛型有关的类型全部擦除
为了通过反射操作这些类型,Java新增了ParameterizedType,GenericArrayType,TypeVariable和WildcardType几种类型来代表不能被归一到Class类中的类型但又和原始类型齐名的类型
获取泛型信息:
package com.duo.reflection;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
//通过反射获取泛型
public class Test9 {
public void test01(Map<String, Person> map, List<Person> list) {}
public Map<String, Person> test02() {
System.out.println("test02");
return null;
}
public static void main(String[] args) throws NoSuchMethodException {
Method method = Test9.class.getMethod("test01", Map.class, List.class);
Type[] genericParameterTypes = method.getGenericParameterTypes(); //获得泛型的参数类型
for (Type genericParameterType : genericParameterTypes) {
System.out.println("#" + genericParameterType);
//instanceof:用来在运行时指出对象是否是特定类的一个实例
if (genericParameterType instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
System.out.println("==========================");
method = Test9.class.getMethod("test02");
Type genericReturnType = method.getGenericReturnType(); //获得泛型返回值类型
if (genericReturnType instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument);
}
}
}
}
运行结果:
getAnnotations
getAnnotation
了解什么是ORM?
Object relationship Mapping --> 对象关系映射
class Student {
int id;
String name;
int age;
}
上述对应:
id | name | age |
---|---|---|
001 | Li | 32 |
002 | Wang | 24 |
即,类和表结构对应、属性和字段对应、对象和记录对应
反射操作注解:
package com.duo.reflection;
import java.lang.annotation.*;
import java.lang.reflect.Field;
//反射操作注解
public class Test10 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> c1 = Class.forName("com.duo.reflection.men");
//通过反射获得(类名的)注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得(类名的)注解的value值
Table annotation = c1.getAnnotation(Table.class);
String value = annotation.value();
System.out.println(value);
//获得类指定的注解
Field id = c1.getDeclaredField("name");
Attribute annotation1 = id.getAnnotation(Attribute.class);
System.out.println(annotation1.columnName());
System.out.println(annotation1.type());
System.out.println(annotation1.length());
}
}
@Table("database_men")
class men {
@Attribute(columnName = "id", type = "int", length = 10)
private int id;
@Attribute(columnName = "age", type = "int", length = 10)
private int age;
@Attribute(columnName = "name", type = "varchar", length = 3)
private String name;
public men() {}
public men(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 "man{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table {
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Attribute {
String columnName();
String type();
int length();
}
运行结果: