反射

什么是反射

动态获取类信息,动态调用对象内容

在JVM运行状态中,对于任意一个类,能够获取这个类的所有属性和方法,对于任意一个对象,能够调用它的所有属性和方法

getClass()

根据对象获取类

1 2 3 4 5
String name = "abcd"; Class c = name.getClass(); System.out.println(c.getName()); java.lang.String

Class.forName()

根据类命名空间+类名称获取类

1 2 3 4 5
String name = "java.lang.String"; Class c = Class.forName(name); System.out.println(c.getName()); java.lang.String

Type

基本类型type属性

1 2 3 4
Class c1 = Boolean.TYPE; Class c2 = Byte.TYPE; Class c3 = Float.TYPE; Class c4 = Double.TYPE;

反射调用私有构造器

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Test test = new Test(); Class c = test.getClass(); Constructor[] constructors = c.getDeclaredConstructors(); //打印构造方法参数类型 for (int i = 0; i < constructors.length; i++) { System.out.print(Modifier.toString(constructors[i].getModifiers()) + " 参数:"); Class[] parametertypes = constructors[i].getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getName() + " "); } System.out.println(""); } //获取无参构造方法 constructors = c.getDeclaredConstructor(); System.out.print(Modifier.toString(constructors.getModifiers())); //获取指定参数构造方法 Class[] p = {int.class, String.class}; constructors = c.getDeclaredConstructor(p); System.out.print(Modifier.toString(constructors.getModifiers()) + " 参数:"); Class[] parametertypes = constructors.getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getName() + " "); } //调用public构造方法 Class[] p = {int.class,String.class}; constructors = c.getDeclaredConstructor(p); constructors.newInstance(11, "ab"); //调用private构造方法 Class[] p = {String.class}; constructors = c.getDeclaredConstructor(p); constructors.setAccessible(true); constructors.newInstance("abcd");

反射调用私有方法

 1  2  3  4  5  6  7  8  9 10
Test test = new Test(); Class c = test.getClass(); Class[] p = {String.class}; Method method = c.getDeclaredMethod("welcome", p); method.setAccessible(true); Object args[] = {"欢迎光临"}; //test类实例,args方法参数 method.invoke(test, args);

获取类私有字段

1 2 3 4 5 6 7
Test test = new Test(); Class c = test.getClass(); Object o = c.newInstance(); Field field = c.getDeclaredField("name"); field.setAccessible(true); field.set(o, "Young");

自定义标记

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface ColumnTag { public String type() default ""; public int size() default 0; } //使用自定义标记 @TableTag(name = Config.TBL_BOOK) public class Book { public Book() { } /** * sid,自动增长 */ @ColumnTag(type = "int", size = 10) public int sid = 0; } //获取所有字段 Field[] fields = clazz.getDeclaredFields(); //获取标记字段 private static String getTableName(Class<?> clazz) { TableTag tt = clazz.getAnnotation(TableTag.class); return tt.name(); } private static String getColumnType(Field c) { ColumnTag ct = (ColumnTag) c.getAnnotation(ColumnTag.class); return ct.type(); } private static int getColumnSize(Field c) { ColumnTag ct = (ColumnTag) c.getAnnotation(ColumnTag.class); return ct.size(); }

浙ICP备11005866号-8