本文共 4408 字,大约阅读时间需要 14 分钟。
在以前的文章中有简单介绍过,但没有深入了解,补充一下。
反射是 JVM 在运行时才动态加载类或调用方法/访问属性,它不需要事先(写代码的时候或编译期)知道运行对象是谁。主要功能是在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法(通过反射甚至可以调用private方法);在运行时调用任意一个对象的方法
getName():获得类的完整名字。newInstance():通过类的不带参数的构造方法创建这个类的一个对象getSuperClass():这个类型的直接超类的全限定名 isInterface():这个类型是类类型还是接口类型 getTypeParamters():这个类型的访问修饰符 getInterfaces():任何直接超接口的全限定名的有序列表 得到构造器的方法Constructor getConstructor(Class[] params) -- 获得使用特殊的参数类型的公共构造函数,Constructor[] getConstructors() -- 获得类的所有公共构造函数Constructor getDeclaredConstructor(Class[] params) -- 获得使用特定参数类型的构造函数(与接入级别无关)Constructor[] getDeclaredConstructors() -- 获得类的所有构造函数(与接入级别无关)获得字段信息的方法Field getField(String name) -- 获得命名的公共字段Field[] getFields() -- 获得类的所有公共字段Field getDeclaredField(String name) -- 获得类声明的命名的字段,包括private 声明的和继承类Field[] getDeclaredFields() -- 获得类声明的所有字段获得方法信息的方法Method getMethod(String name, Class[] params) -- 使用特定的参数类型,获得命名的公共方法,name参数指定方法的名字,parameterTypes 参数指定方法的参数类型。Method[] getMethods() -- 获得类的所有公共方法Method getDeclaredMethod(String name, Class[] params) -- 使用特写的参数类型,获得类声明的命名的方法Method[] getDeclaredMethods() -- 获得类声明的所有方法,包括private 声明的和继承类
获取一个对象
Class cla = Class.forName("cn.spleated.Car");
Constructor CarConstructor = cla.getConstructor();
Object appleObj = CarConstructor.newInstance();
import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import static java.lang.Class.forName;public class ConstructorDemo { public ConstructorDemo(){} public ConstructorDemo(int a,int b){ System.out.println("a="+a+"\nb="+b); } public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Class cls = forName("ConstructorDemo"); Class partypes[]=new Class[2]; partypes[0] = Integer.TYPE; partypes[1] = Integer.TYPE; Constructor ct = cls.getConstructor(partypes); Object arg[] = new Object[2]; arg[0] = new Integer(37); arg[1] = new Integer(14); Object ret = ((Constructor) ct).newInstance(arg); }}
调用某一个方法
Method setPriceMethod = cla.getMethod("setPrice", int.class);
setPriceMethod.invoke(appleObj, 14);
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Methodemo { public int add(int a,int b){ return a+b; } public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class cls = Class.forName("Methodemo"); Class pro[] = new Class[2]; pro[0] = Integer.TYPE; pro[1] = Integer.TYPE; Method me = cls.getMethod("add",pro); Methodemo metobj = new Methodemo(); Object arg[] = new Object[2]; arg[0] = new Integer(13); arg[1] = new Integer(25); Object reobj = me.invoke(metobj,arg); Integer ret = (Integer) reobj; System.out.println(ret.intValue()); }}
由JVM创建一个本机进程,加载对应的指令到进程的地址空间中,然后执行该指令。
java.lang.Runtime.getRuntime().exec()
和 java.lang.ProcessBuilder.start()方
法,其实就是创建一个进程的方法。具体可查看java.lang.Runtime
类中,Runtime类
的构造器是private
修饰的,无法直接获得Runtime类的实例
,只能通过getRuntime()
来间接获取一个Runtime类的实例exec()方法
exec()
底层代码是调用了ProcessBuilder类
ProcessBuilder
类代码中,ProcessBuilder类
用start方法创建进程。调用java.lang.ProcessImpl
类的start方法,实现创建本机进程,执行系统命令的功能ProcessImpl类
,他是继承自Process类
的final类
java.lang
包外,直接调用ProcessImpl
类。ProcessImpl类
的start方法
,最后是返回了一个ProcessImpl 类的实例
分析下上篇文章的通过getRuntime().exec()
调用计算机
import java.lang.reflect.InvocationTargetException;public class POC { public static void main(String args[]) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { Object runtime=Class.forName("java.lang.Runtime") .getMethod("getRuntime",new Class[]{}) .invoke(null); Class.forName("java.lang.Runtime") .getMethod("exec", String.class) .invoke(runtime,"calc.exe"); }}
参考文献:
转载地址:http://xqxym.baihongyu.com/