Java程序语言的后门(4)

public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
//获取类对象,除了这份方法外还有另外两种方法
Class clazz = HaShiQi.class;

System.out.println("——————— 获取所有公有的属性 —————————"); Field[] fields = clazz.getFields(); for(Field field : fields) { field.setAccessible(true); System.out.println(field); } System.out.println("——————— 获取所有的属性 —————————"); fields = clazz.getDeclaredFields(); for(Field field : fields) { field.setAccessible(true); System.out.println(field); } System.out.println("——————— 获取所有公有的方法 —————————"); Method[] methods = clazz.getMethods(); for(Method method : methods) { System.out.println(method); } System.out.println("——————— 获取所有类中声明的方法 —————————"); methods = clazz.getDeclaredMethods(); for(Method method : methods) { System.out.println(method); } System.out.println("——————— 获取所有公有的构造器 —————————"); Constructor[] constructors = clazz.getConstructors(); for(Constructor constructor : constructors) { System.out.println(constructor); } }

  打印结果:

——————— 获取所有公有的属性 —————————
public java.lang.String com.zcz.reflecttest.HaShiQi.color
——————— 获取所有的属性 —————————
public java.lang.String com.zcz.reflecttest.HaShiQi.color
private java.lang.String com.zcz.reflecttest.HaShiQi.name
——————— 获取所有公有的方法 —————————
public void com.zcz.reflecttest.HaShiQi.run()
public java.lang.String com.zcz.reflecttest.HaShiQi.toString()
public void com.zcz.reflecttest.HaShiQi.eat()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
——————— 获取所有类中声明的方法 —————————
public void com.zcz.reflecttest.HaShiQi.run()
public java.lang.String com.zcz.reflecttest.HaShiQi.toString()
private void com.zcz.reflecttest.HaShiQi.dance()
public void com.zcz.reflecttest.HaShiQi.eat()
——————— 获取所有公有的构造器 —————————

  因为HaShiQi类中没有公有的构造器,所以这里什么都没有打印出来。自然也有获取到私有构造器的方法:

public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
//获取类对象,除了这份方法外还有另外两种方法
Class clazz = HaShiQi.class;

System.out.println("——————— 获取所有公有的属性 —————————"); Field[] fields = clazz.getFields(); for(Field field : fields) { field.setAccessible(true); System.out.println(field); } System.out.println("——————— 获取所有的属性 —————————"); fields = clazz.getDeclaredFields(); for(Field field : fields) { field.setAccessible(true); System.out.println(field); } System.out.println("——————— 获取所有公有的方法 —————————"); Method[] methods = clazz.getMethods(); for(Method method : methods) { System.out.println(method); } System.out.println("——————— 获取所有类中声明的方法 —————————"); methods = clazz.getDeclaredMethods(); for(Method method : methods) { System.out.println(method); } System.out.println("——————— 获取所有公有的构造器 —————————"); Constructor[] constructors = clazz.getConstructors(); for(Constructor constructor : constructors) { System.out.println(constructor); } System.out.println("——————— 获取所有的构造器 —————————"); constructors = clazz.getDeclaredConstructors(); for(Constructor constructor : constructors) { System.out.println(constructor); } }

  打印结果:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d6c6a85d67794374490ef85f86c9a69d.html