什么是反射?
反射:通过Java代码获取类信息及类的对象
反射可以获取类的一切信息
反射的三种方式
1、通过对象.getClass();
2、通过类路径Class.forname(“类路径”);
3、通过类名.class
反射中的方法
Field [ ] getFields ( )
Filed [ ] getDeclaredFields ()
getFields 方法将返回一个包含 Field 对象的数组 , 这些对象记录了这个类或其超类的公有域。
getDeclaredField 方法也将返回包含Field 对象的数组 , 这些对象记录了这个类的全部域。 如果类中没有域 ,或者 Class 对象描述的是基本类型或数组类型, 这些方法将返回一个长度为 0 的数组。
Method [ ] getMethods ()
Method [ ] getDeclareMethods ( )
返回包含 Method 对象的数组 :
getMethods 将返回所有的公有方法 , 包括从超类继承来的公有方法 ; getDeclaredMethods 返回这个类或接口的全部方法 , 但不包括由超类继承了的方法。
Constructor[ ] getConstructors ( )
Constructor[ ] getDecl aredConstructors ( )
返回包含 Constructor 对象的数组 , 其中包含了 Class 对象所描述的类的所有公有构造器 ( getConstructors ) 或所有构造器 (getDeclaredConstructors ) 。
Class getDeclaringClass ( )
返冋一个用于描述类中定义的构造器 、 方法或域的 Class 对象。
Class[] getExcepti onTypes ( ) ( 在 Constructor 和 Method 类 中 )
返回一个用于描述方法抛出的异常类型的 Class 对象数组。
int getModifiers ( )
返回一个用于描述构造器 、 方法或域的修饰符的整型数值。 使用 Modifier 类中的这个方法可以分析这个返回值。
String getName ( )
返冋一个用于描述构造器 、 方法或域名的字符串 。
Class [ ] getParameterTypes ( ) ( 在 Constructor 和 Method 类 中 )
返回一个用于描述参数类型的 Class 对象数组。
Class getReturnType ( ) ( 在 Method 类中 )
返回一个用于描述返 H 类型的 Class 对象。
static String toString ( int modifiers )
返回对应 modifiers 中位设置的修饰符的字符串表表示 。
static boolean isAbstract ( int modifiers )
static boolean isFinal ( int modifiers )
static boolean islnterface ( int modifiers )
static boolean isNative ( int modifiers )
static boolean isPrivate ( int modifiers )
static boolean isProtected ( int modifiers )
static boolean isPublic ( int modifiers )
static boolean isStatic ( int modifiers )
static boolean isStrict ( int modifiers )
static boolean isSynchronized ( int modifiers )
static boolean isVolati 1 e ( int modifiers )
其他一些相关方法:
类信息.newInstance();方法相当于获取一个类的实例
.isPrimitive()判断是否为基本类型
.getComponentType()获取数组中元素类型
利用invoke()来调用Method对象中的方法
反射可以获取私有信息:
Field f = cl.getDeclaredField("name");
f.setAccessible(true);//忽略安全访问修饰符的安全检查
实例:
一个Person类
package com.qcby;
public class Person1 {
//用于测试的Person1.class
public String name;
public int age;
public Person1(int age){
}
public Person1(String name,int age){
}
public void m1(String a,String b,int x){
}
public int m2(int a,int b){
return 3;
}
public static final int m3(String xx,String h){
return 5;
}
}
获取Person1.class的反射信息打印结果
package Reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectionTest {
//获取Person1.class的反射信息打印结果
//public class com.qcby.Person1
//{
//public com.qcby.Person1(int);
//public com.qcby.Person1(java.lang.String, int);
//public void m1(java.lang.String, java.lang.String, int);
//public int m2(int, int);
//public static final int m3(java.lang.String, java.lang.String);
//java.lang.String name; int age;
//}
public static void main(String[] args) {
try {
Class cl = Class.forName("com.qcby.Person1");
Class supercl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if(modifiers.length()>0) {
System.out.print(modifiers + " ");
}
System.out.print("class " + "com.qcby.Person1");
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();
System.out.println("}");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
System.exit(0);
}
public static void printConstructors(Class cl) {
Constructor[] constructors = cl.getDeclaredConstructors();
for(Constructor c : constructors) {
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[] paramTypes = c.getParameterTypes();
for(int j = 0;j < paramTypes.length;j++) {
if(j>0) {
System.out.print(", ");
}
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
public static void printMethods(Class cl) {
Method[] methods = cl.getDeclaredMethods();
for(Method m : methods) {
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 + "(");
Class[] paramTypes = m.getParameterTypes();
for(int j = 0;j < paramTypes.length;j++) {
if(j>0) {
System.out.print(", ");
}
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
public static void printFields(Class cl) {
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(type.getName() + " " + name + ";");
}
}
}
}