将 class 文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的 java.lang.Class 对象,作为方法区中类数据的访问入口。
//java学习交流737251827 package com.reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Test03 { public static void main(String[] args) throws Exception { //获得 class 对象 Class c1 = Class.forName("com.reflection.User"); //1、构造一个对象,本质是无参构造器 User user1 = (User) c1.newInstance(); System.out.println(user1); //2、通过构造器创建对象 Constructor constructor = c1.getDeclaredConstructor(int.class, String.class, int.class); User user2 = (User) constructor.newInstance(1,"郭一诺",1); System.out.println(user2); //3、通过反射调用普通方法 User user3 = (User) c1.newInstance(); Method setName = c1.getDeclaredMethod("setName", String.class); //invoke 激活 setName.invoke(user3,"素小暖"); System.out.println(user3.getName()); //4、通过反射操作属性 User user4 = (User) c1.newInstance(); Field name = c1.getDeclaredField("name"); //true:取消 Java 语言访问检查 name.setAccessible(true); name.set(user4,"素小暖 2"); System.out.println(user4.getName()); } }
1、代码实例
package com.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 Test04 { public void test01(Map<String,User> map, List<User> list){ System.out.println("test01"); } public Map<String,User> test02(){ System.out.println("test02"); return null; } //通过反射获取泛型信息 public static void main(String[] args) throws Exception { Method method = Test04.class.getMethod("test01", Map.class, List.class); Type[] genericParameterTypes = method.getGenericParameterTypes(); for (Type genericParameterType : genericParameterTypes) { System.out.println("***"+genericParameterType); if(genericParameterType instanceof ParameterizedType){ Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments(); for (Type actualTypeArgument : actualTypeArguments) { System.out.println(actualTypeArgument); } } } method = Test04.class.getMethod("test02", null); Type genericReturnType = method.getGenericReturnType(); if(genericReturnType instanceof ParameterizedType){ Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments(); for (Type actualTypeArgument : actualTypeArguments) { System.out.println("test02,"+actualTypeArgument); } } } }
2、控制台输出?
3、反射解决泛型问题
1、代码实例
package com.reflection; import java.lang.annotation.*; import java.lang.reflect.Field; public class Test05 { public static void main(String[] args) throws Exception { Class c1 = Class.forName("com.reflection.Student"); //通过反射获取注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获得注解 value 的值 TableSu tableSu = (TableSu) c1.getAnnotation(TableSu.class); String value = tableSu.value(); System.out.println(value); //获得类指定的注解 Field field = c1.getDeclaredField("name"); FieldSu annotation = field.getAnnotation(FieldSu.class); System.out.println(annotation.columnName()); System.out.println(annotation.type()); System.out.println(annotation.length()); } } @TableSu("db_student") class Student{ @FieldSu(columnName = "db_id",type = "int",length = 10) private int id; @FieldSu(columnName = "db_name",type = "varchar2",length = 10) private String name; @FieldSu(columnName = "db_age",type = "int",length = 10) 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 "Student{" + "id=" + id + ", name='" + name + ''' + ", age=" + age + '}'; } } //类名的注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableSu{ String value(); } //属性的注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FieldSu{ String columnName(); String type(); int length(); }
2、控制台输出?