采用Class类和java.lang.reflect 类库一块儿实现 <反射>机制java
在程序运行期间,Java运行时系统始终为全部的对象维护一个被称为运行时的类型表示。这个信息跟踪着每一个对象所属的类。虚拟机利用运行时类型信息选择相应的方法执行。segmentfault
然而,能够经过专门的Java类访问这些信息。保存这些信息的类被成为Class,这个名字很容易让人混淆。Object 类中的getClass() 方法将返回一个Class类型的实例。数组
获取Class对象的三种方法dom
Random generator = new Random();
Class cl = generator.getClass();
System.out.println(cl.getName());//java.util.Random
复制代码
若是类名保存字符串中,并可在运行时该变,便可使用这种方法。forName() 方法会爆ClassNotFoundException 异常,因此须要进行异常处理。函数
Class.forName() 内部经过反射API根据目标类名将类手动加载到内存中,称为类加载器加载方法。加载过程当中会把目标类的static方法,变量,代码块加载到JVM,注意此时还没有建立对象实例测试
String className = "java.util.Random";
Class cl = class.forName(className);
复制代码
Class cl = Random.class;
复制代码
Constructor[] getConstructors()
:得到全部public构造器;Constructor[] getDeclaredConstructors()
:得到全部访问权限的构造器Constructor getConstructor(Class[] params)
:根据指定参数得到对应构造器;Constructor getDeclaredConstructor(Class[] params)
:根据指定参数得到对应构造器;Method[] getMethods()
:得到全部public方法;Method[] getDeclaredMethods()
:得到全部访问权限的方法;Method getMethod(String name, Class[] params)
:根据方法签名获取类自身对应public方法,或者从基类继承和接口实现的对应public方法;Method getDeclaredMethod(String name, Class[] params)
:根据方法签名得到对应的类自身声明方法,访问权限不限;Field[] getFields()
:得到类中全部public变量Field[] getDeclaredFields()
:得到类中全部访问权限变量Field getField(String name)
:根据变量名获得对应的public变量Field getDeclaredField(String name)
:根据变量名得到对应的变量,访问权限不限;咱们来个例子加深一下印象。spa
public static void printConstructors(Class cl) {
//返回包含Constructor 对象的数组,其中包含了Class对象的全部构造器
Constructor[] constructors = cl.getDeclaredConstructors();
for (Constructor c: constructors) {
//String getName()
//返回一个用于描述构造器、方法或域名的字符串
String name = c.getName();
System.out.print(" ");
String modifiers = Modifier.toString(c.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+ " ");
System.out.print(name+"(");
//Class[] getParameterTypes() (在Constructor 和 Method 类中)
//返回一个用于描述参数类型的Class对象数组
printParameterType(c.getParameterTypes());
System.out.println(");");
}
}
复制代码
public static void printMethods(Class cl) {
//返回包含Method 对象的数组,返回这个类或接口的所有方法,但不包括由超类继承了的方法
Method[] methods = cl.getDeclaredMethods();
for (Method m:methods) {
//Class getReturnType() (在 Method 类中)
//返回一个用于描述返回类型的Class对象
Class retType = m.getReturnType();
String name = m.getName();
System.out.print(" ");
//打印修饰符、返回类型和方法名称
String modifiers =Modifier.toString(m.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+" ");
System.out.print(retType.getName()+" "+ name + "(");
printParameterType(m.getParameterTypes());
System.out.println(");");
}
}
复制代码
public static void printFields(Class cl) {
//Field[] getDeclaredFields()
//返回包含Field 对象的数组,这些对象记录了这个类的所有域
Field[] fields = cl.getDeclaredFields();
for (Field f : fields){
Class type = f.getType();
String name = f.getName();
System.out.print(" ");
String modifiers = Modifier.toString(f.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+" ");
System.out.println(type.getName()+" "+ name+ ";");
}
}
复制代码
public static void printParameterType(Class[] paramTypes) {
for (int j = 0;j<paramTypes.length;j++) {
if (j>0)
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
}
复制代码
public static void main(String[] args) {
String name;
if (args.length>0)
name = args[0];
else {
Scanner in = new Scanner(System.in);
System.out.println("Enter class name(e.g java.util.Date): ");
name = in.next();
}
try {
//print class name and superclass name (if != object)
//调用Class 的静态方法 forName 得到类名对应的Class 对象
Class cl = Class.forName(name);
//获取父类所对应的Class 对象
Class supercl = cl.getSuperclass();
//返回对应modifiers 中位设置的修饰符的字符串表示
String modifiers = Modifier.toString(cl.getModifiers());
if (modifiers.length()>0)
System.out.print(modifiers+" ");
System.out.print("class "+ name);
//判断是否有继承父类
if (supercl != null && supercl != Object.class)
System.out.print(" extends "+ supercl.getName());
System.out.print("\n{\n");
printConstructors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}");
}catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
System.exit(0);
}
复制代码
反射笔记对象