[size=medium](一) 得到字节码的三种方式[/size]
[size=medium](二) 通过反射取得成员变量值[/size]
成员变量反射练习:
[size=medium](三)成员方法的反射[/size]
[size=medium](四) 数组的反射[/size]
[size=medium](五) 通过类加载器读取配置文件[/size]
'config.properties'文件内容
className=java.util.ArrayList
[size=medium](六) BeanUtils工具包使用[/size]
[size=medium](七) PropertyUtils[/size]
//第一种方式:
//得到Ref的字节码
Class bytecodesRef = Ref.class;
//取得Ref字节码对应的构造方法
Constructor constructorRef = bytecodesRef.getConstructor(String.class, Company.class);
//构造这个对象
Ref ref = (Ref) constructorRef.newInstance(new String("apq"), new Company());
//第二种方式:
Class bytecodesRef = Class.forName("com.apq.reflect.Ref");
//第三种方式:
Class bytecodesRef = new com.apq.reflect.Ref(new String("apq"), new Company()).getClass();
[size=medium](二) 通过反射取得成员变量值[/size]
//取得公有成员字段
Field publicFieldX = ref.getClass().getField("x");
System.out.println(publicFieldX.get(ref)); //'public field'
//取得私有成员字段
Field privateFieldY = ref.getClass().getDeclaredField("y");
privateFieldY.setAccessible(true);
System.out.println(privateFieldY.get(ref)); //'private field'
成员变量反射练习:
getFields(ref);
//[x=public field_0, y=private field_1]
System.out.println(ref.toString());
//取得所有成员变量名字和值
public static void getFields(Ref ref) throws Exception {
Field[] fields = ref.getClass().getDeclaredFields();
for(int i = 0, len = fields.length; i < len; i++) {
Field field = fields[i];
field.setAccessible(true);
//取得成员变量名字
String fieldName = field.getName();
String fieldValue = "";
if(field.getType() == String.class) {
//取得成员变量值
fieldValue = (String) field.get(ref);
//设置成员变量值
field.set(ref, fieldValue + "_" + i);
}
}
}
[size=medium](三)成员方法的反射[/size]
//一般调用
String str = "abc";
System.out.println(str.charAt(1)); //b
//反射调用
Method methodCharAt = String.class.getMethod("charAt", int.class);
System.out.println(methodCharAt.invoke(str, 1)); //b
//一般调用: Ref.staticMethodPrintCompanyName(new Company());
//反射调用
Method staticMethodPrintCompanyName = Ref.class.getMethod("printCompanyName", Company.class);
//静态方法,invoke第一个参数设置成null
staticMethodPrintCompanyName.invoke(null, new Company()); //xyz
//一般调用: new Ref.methodPrint(new Company());
//反射调用
Ref ref = (Ref) Class.forName("com.apq.reflect.Ref").newInstance();
Method methodPrint = Ref.class.getMethod("print", Company.class);
//非静态方法,invoke第一个参数要传个对象过去
methodPrint.invoke(ref, new Company()); //xyz
//jdk1.4语法
methodPrint.invoke(ref, new Object[] {new Company()}); //xyz
//对接收数组成员方法的进行反射
Method arrMethod = Class.forName("com.apq.test.arrayReflect").getMethod("print", String[].class);
arrMethod.invoke(null, new Object[] {new String[] {"x", "y", "z"}}); //xyz
//or
//arrMethod.invoke(null, (Object) new String[] {"x", "y", "z"}); //xyz
class arrayReflect{
public static void print(String[] args) {
for(int i = 0, len = args.length; i < len; i++) {
System.out.print(args[i]);
}
}
}
[size=medium](四) 数组的反射[/size]
String[] arr = new String[] {"a", "b", "c"};
printObj(arr);
private static void printObj(Object obj) {
Class clazz = obj.getClass();
if(clazz.isArray()) {
int len = Array.getLength(obj);
for(int i = 0; i < len; i++) {
System.out.println(Array.get(obj, i));
}
}else {
System.out.println(obj);
}
}
[size=medium](五) 通过类加载器读取配置文件[/size]
'config.properties'文件内容
className=java.util.ArrayList
Properties props = new Properties();
//config.properties要和Test.java放在一个包下面
//InputStream is = Test.class.getResourceAsStream("config.properties");
//放到src目录下,在eclipse
InputStream is = Test.class.getClassLoader().getResourceAsStream("config.properties");
props.load(is);
String className = props.getProperty("className");
is.close();
List list = (List) Class.forName(className).newInstance();
list.add("a");
System.out.println(list); //[a]
[size=medium](六) BeanUtils工具包使用[/size]
RefPoint rp = new RefPoint(10, 20);
//get一个属性
System.out.println(BeanUtils.getProperty(rp, "x")); //10
//set一个属性
BeanUtils.setProperty(rp, "x", "50");
System.out.println(rp.getX()); //50
BeanUtils.setProperty(rp, "birthday.time", "1980");
System.out.println(BeanUtils.getProperty(rp, "birthday.time"));
//RefPoint类里要new个Company出来
BeanUtils.setProperty(rp, "company.name", "apq company");
System.out.println(BeanUtils.getProperty(rp, "company.name"));
//可以对Map set和get
Map map = new HashMap();
BeanUtils.setProperty(map, "name", "baby69yy2000");
System.out.println(BeanUtils.getProperty(map, "name")); //baby69yy2000
[size=medium](七) PropertyUtils[/size]
Product product = new Product(); //产品
Vendor vendor = new Vendor(); //产品的供应商
PropertyUtils.setProperty(vendor, "name", "Intel");
PropertyUtils.setProperty(product, "name", "E2160");
PropertyUtils.setProperty(product, "quantity", 30);
PropertyUtils.setProperty(product, "vendor", vendor);
System.out.println(PropertyUtils.getProperty(product, "name"));//E2160
System.out.println(PropertyUtils.getProperty(product, "quantity"));//30
vendor = (Vendor) PropertyUtils.getProperty(product, "vendor");
System.out.println(vendor.getName()); //Intel