Java–注解与反射–类加载器、类的运行时结构、反射动态创建对象、分析反射性能问题、反射操作泛型、反射操作注解
类加载器
package com.zy.reflection;
public class Study05 {
public static void main(String[] args) throws ClassNotFoundException {
ClassLoader loader = ClassLoader.getSystemClassLoader();
System.out.println(loader);
ClassLoader loaderParent = loader.getParent();
System.out.println(loaderParent);
ClassLoader loaderParentParent = loaderParent.getParent();
System.out.println(loaderParentParent);
ClassLoader classLoader = Class.forName("com.zy.reflection.Study05").getClassLoader();
System.out.println(classLoader);
classLoader = Class.forName("java.lang.Object").getClassLoader();
System.out.println(classLoader);
System.out.println(System.getProperty("java.class.path"));
}
}
类的运行时结构
package com.zy.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Study06 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class<?> c1 = Class.forName("com.zy.reflection.User");
String name = c1.getName();
System.out.println(name);
String simpleName = c1.getSimpleName();
System.out.println(simpleName);
Field[] fields = c1.getFields();
fields = c1.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i]+"==>");
}
Field userName = c1.getDeclaredField("name");
System.out.println(userName);
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println("公共的:"+method);
}
methods = c1.getDeclaredMethods();
for (Method method : methods) {
System.out.println("全部的:"+method);
}
Method getName = c1.getMethod("getName", null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);
Constructor<?>[] constructors = c1.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
constructors = c1.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
System.out.println(c1.getConstructor(null));
System.out.println(c1.getConstructor(String.class, int.class, int.class));
}
}
反射动态创建对象
package com.zy.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Study07 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class<?> c1 = Class.forName("com.zy.reflection.User");
User user = (User)c1.newInstance();
System.out.println(user);
Constructor<?> constructor = c1.getConstructor(String.class,int.class,int.class);
user = (User)constructor.newInstance("zhangsan", 101, 18);
System.out.println(user);
Method setName = c1.getMethod("setName",String.class);
setName.invoke(user,"lisi");
System.out.println(user);
Field userName = c1.getDeclaredField("name");
userName.setAccessible(true);
userName.set(user,"wangwu");
System.out.println(user);
}
}
分析反射性能问题
package com.zy.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Study08 {
public static void study01(){
User user = new User();
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000000; i++) {
user.getName();
}
long end = System.currentTimeMillis();
System.out.println("普通方式执行20亿次:"+(end-start)+"ms");
}
public static void study02() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException {
Class<User> c1 = User.class;
User user = c1.newInstance();
Method getName = c1.getMethod("getName", null);
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000000; i++) {
getName.invoke(user,null);
}
long end = System.currentTimeMillis();
System.out.println("反射方式执行20亿次:"+(end-start)+"ms");
}
public static void study03() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, InstantiationException {
Class<User> c1 = User.class;
User user = c1.newInstance();
Method getName = c1.getMethod("getName", null);
getName.setAccessible(true);
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000000; i++) {
getName.invoke(user,null);
}
long end = System.currentTimeMillis();
System.out.println("关闭检测后的反射方式执行20亿次:"+(end-start)+"ms");
}
public static void main(String[] args) throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
study01();
study02();
study03();
}
}
反射操作泛型
package com.zy.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 Study09 {
public static void study01(Map<String,User> map, List<User> list){
System.out.println("study01");
}
public static Map<String,User> study02(){
System.out.println("study02");
return null;
}
public static void main(String[] args) throws NoSuchMethodException {
Method study01 = Study09.class.getMethod("study01", Map.class, List.class);
Type[] types = study01.getGenericParameterTypes();
for (Type type : types) {
System.out.println(type);
if(type instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument+"==");
}
}
}
Method study02 = Study09.class.getMethod("study02");
Type genericReturnType = study02.getGenericReturnType();
System.out.println(genericReturnType.getTypeName());
if(genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println(actualTypeArgument+"==>");
}
}
}
}
反射操作注解
package com.zy.reflection;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
public class Study10 {
public static void main(String[] args) throws NoSuchFieldException {
Class<Student> studentClass = Student.class;
Annotation[] annotations = studentClass.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
TableORM tableORM = studentClass.getAnnotation(TableORM.class);
String tableORMValue = tableORM.value();
System.out.println(tableORMValue);
Field id = studentClass.getDeclaredField("id");
FieldORM idAnnotation = id.getAnnotation(FieldORM.class);
System.out.println(idAnnotation.columnName());
System.out.println(idAnnotation.columnType());
System.out.println(idAnnotation.columnLength());
}
}
@TableORM("db_student")
class Student{
@FieldORM(columnName = "intid",columnType = "int",columnLength = 9)
private int id;
@FieldORM(columnName = "strname",columnType = "varchar2",columnLength = 30)
private String name;
@FieldORM(columnName = "intage",columnType = "int",columnLength = 9)
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return id+"=="+name+"=="+age;
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableORM{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldORM{
String columnName();
String columnType();
int columnLength();
}