标签:动态 nts ++ this 反编译 ret main 对象 turn
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的以及动态调用对象的方法的功能称为Java的反射机制。
1、反编译
2、框架
1、获取class的三种方式
// 通过class属性 String.class.getName(); String s = "Hello Word!"; // 通过getClass方法 System.out.println(s.getClass().getName()); String sClass = "java.lang.String"; try { // 通过Class的forName的静态方法 System.out.println(Class.forName(sClass).getName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
2、获取class的构造器,来新建实例
实体类如下:
public class Person { private String name; private int age; public Person() { } private Person(String name) { this.name = name; } public Person(String name, int age) { this.name = name; this.age = age; } private Person(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
(1)获取所有的构造器
// 获取所有构造器 Person p = new Person(); Class pClass = p.getClass(); Constructor[] Constructors = pClass.getDeclaredConstructors(); for (int i = 0; i < Constructors.length; i++) { System.out.println(Modifier.toString(Constructors[i].getModifiers())); System.out.println(Constructors[i].getParameterTypes().getClass().getName()); }
(2)获取无参构造
Person p = new Person();
Class pClass = p.getClass();
try { Constructor constructors = pClass.getDeclaredConstructor(); System.out.println(Modifier.toString(constructors.getModifiers())); System.out.println(constructors.getParameterTypes().getClass().getName()); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); }
(3)根据构造器中不同的参数,获取不同的构造器
Person p = new Person(); Class pClass = p.getClass(); Class[] classArray = new Class[] { String.class, int.class }; try { Constructor constructor = pClass.getDeclaredConstructor(classArray); constructor.setAccessible(true); Person person = (Person) constructor.newInstance("wsq", 12); System.out.println(person.toString()); System.out.println(Modifier.toString(constructor.getModifiers())); System.out.println(constructor.getParameterTypes().getClass().getName()); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); }
3、通过构造器获取实例对象,并获取私有方法和属性
// 内部类 static class Person{ private String name; private int age; private Person(String name, int age) { super(); this.name = name; this.age = age; } private void getName() { System.out.println(this.name); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } // 获取私有方法 public static void main(String[] args) throws Exception{ // 获取构造器之后获取私有方法 Class pClass = Person.class; // 参数 Class[] paramType = {String.class, int.class}; Constructor constructor = pClass.getDeclaredConstructor(paramType); constructor.setAccessible(true); Person p = (Person) constructor.newInstance("wsq",18); // 获取私有方法 Method method = pClass.getDeclaredMethod("getName", null); method.setAccessible(true); method.invoke(p, null); // 获取私有属性 Field field = pClass.getDeclaredField("name"); field.setAccessible(true); field.set(p, "wsq2"); System.out.println(p.toString()); }
标签:动态 nts ++ this 反编译 ret main 对象 turn
原文地址:https://www.cnblogs.com/mcjhcnblogs/p/13171608.html