想要了解反射,最基本的类就是Class类,因为Class能够保存类的信息。在类中含有构造方法、函数、属性。这些都能够通过某种手段得到。
1.获得字节码的方法:
(1)Class.forName(String)
(2)p.getClass();
(3)P.class;
Class c1 =...;
c1.getDeclaredConstructors();返回一个Constructor数组。 包含一个类中的构造函数。
2.通过构造方法进行反射:newInstance
getConstructor(Class)进行对某个类的构造函数进行声明。
newInstance(Object);能够创建一个实例。
以下为实例:
Constructor c = String.class.getConstructor(StringBuffer.class);
String str = (String) c.newInstance(new StringBuffer("123"));
System.out.println(str);
这样就能够创建一个String的实例。 3.对于域的反射:get
能够反映出当前对象的域的值是多少.
私有变量的反射需要f.setAccessible(true);
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectField {
public static void main(String[] args) {
ReflectPoint o = new ReflectPoint(1, 2);
Field fy = null;
try {
fy = o.getClass().getDeclaredField("y");
fy.setAccessible(true);
Integer x = (Integer) fy.get(o);
System.out.println(x);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ReflectPoint {
private int x;
private int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
示例:有一个类,其中包含很多字符串的属性,想要把每个字符串属性中的‘b’变成‘a’,并显示结果
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class ReflectField {
public static void main(String[] args) {
ReflectPoint o = new ReflectPoint(1, 2);
Field[] fs ;
try {
fs = o.getClass().getDeclaredFields();
AccessibleObject.setAccessible(fs, true);
for(Field f:fs)
{
if(f.getType() ==String.class)
{
String str = (String)f.get(o);
String result = str.replace("b", "a");
f.set(o, result);
System.out.println(result);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ReflectPoint {
private int x;
private int y;
private String str1 = "basketball";
private String str2 = "ball";
private String str3 = "it";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
4.方法反射: (1)getMethod()声明
(2)invoke()方法调用
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectField {
public static void main(String[] args) {
ReflectPoint o = new ReflectPoint();
try {
Method m = o.getClass().getDeclaredMethod("getStr1", null);
String name = (String) m.invoke(o, null);
System.out.println(name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ReflectPoint {
private String str1 = "busketBall";
public ReflectPoint() {
super();
}
public String getStr1()
{
return str1;
}
}
Constructor:
(1)getModifiers();返回访问权限。能够由Modifier.toString(i)转换成字符串。
(2)getName();返回构造函数的函数名。
(3)getParameterTypes()返回参数的Class,需要对每个参数Class调用getName()获得参数类型。
c1.getDeclaredFields();返回一个Field数组。 包含一个类中的属性。
Field:
(1)getType()返回属性类型
(2)f.get(Object obj);Obj是一个类,包含f属性,返回当前f属性的当前值。
(3)int i=getModifiers()返回访问权限。能够由Modifier.toString(i)转换成字符串。
(4)getName()返回属性名称。
(5)f.set(Object obj,Object val);把Object类中的f属性设置为val值。
c1.getDeclaredMethods()返回一个Method数组。 包含一个类中的一般方法。
Method:
(1)getModifiers()返回访问权限。能够由Modifier.toString(i)转换成字符串。
(2)getName();返回函数的函数名。
(3)getParameterTypes()返回参数的Class,需要对每个参数Class调用getName()获得参数类型。
(4)getReturnType()返回值的类型
下面是一个示例代码,输入一个类,然后把输入类的构造方法,域,函数都打印出来:
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
public class ReflectTest {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
Class c = null;
try {
c = Class.forName(str);
} catch (ClassNotFoundException e) {
System.out.println("********出错!********");
e.printStackTrace();
}
String tmp = Modifier.toString(c.getModifiers());
System.out.println(tmp+" class "+c.getName()+"{");
printField(c);
printConstructor(c);
printMethod(c);
System.out.println("}");
}
public static void printField(Class c)
{
Field[] f = c.getDeclaredFields();
for(int i=0;i<f.length;i++)
{
int p = f[i].getModifiers();
String tmp = Modifier.toString(p);
String type = f[i].getType().getName();
String name = f[i].getName();
System.out.println(" "+tmp+" "+type+" "+name+";");
}
}
public static void printConstructor(Class c)
{
Constructor[] con = c.getDeclaredConstructors();
for(int i=0;i<con.length;i++)
{
int p = con[i].getModifiers();
String tmp = Modifier.toString(p);
String name = con[i].getName();
Class[] param = con[i].getParameterTypes();
System.out.print(" "+tmp+" "+name+"(");
for(int j=0;j<param.length;j++)
{
System.out.print(param[j].getName());
}
System.out.println(");");
}
}
public static void printMethod(Class c)
{
Method[] m = c.getDeclaredMethods();
for(int i=0;i<m.length;i++)
{
int p = m[i].getModifiers();
String tmp = Modifier.toString(p);
String type = m[i].getReturnType().getName();
String name = m[i].getName();
Class[] param = m[i].getParameterTypes();
System.out.print(" "+tmp+" "+type+" "+name+"(");
for(int j=0;j<param.length;j++)
{
System.out.print(param[j].getName());
}
System.out.println(");");
}
}
}