java反照 终极实例
//用来测试的javaBean public class Stu { private int id; public static void main(String[] args) throws ClassNotFoundException { //result: 输入一个类名: //方法一 //方法一输出结果: id stuName ////////////一个应用实例 package com.reflect; import java.beans.IntrospectionException; import com.testBean.Stu; /** } 10 //从上面的例子我觉得我们可以利用反射来简化struts(Action)中对数据库插入的操作人们可以写一个方法impl.save(StuBean.class,request); 如此种用反射从request中得到页面的值,可从StuBean中得到属性,然后利用反射配起来。忧点可以避免写重复的Bean.setXxx()..... 可以这样三步解决: StuBean stu=new StuBean(); impl.save(stu,request); dao.save(stu);//hibernate DAO 或Spring DAO //如果实体bean是私有的,用这个代替上面的方法 public void method(Object obj,List propertyNames,List propertyVales) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
private String stuName;
public Stu(){}
public Stu(int id,String stuName){
this.id=id;
this.stuName=stuName;
}
public void setAll(int id,String stuName){
this.id=id;
this.stuName=stuName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String toString(){
return id+stuName;
}
}
//
Class cla=Class.forName("com.testBean.Stu");
//解析属性信息
Field[] f=cla.getDeclaredFields();
for (Field field : f) {
System.out.println("属性="+field.toString());
System.out.println("数据类型="+field.getType());
System.out.println("属性名="+field.getName());
int mod=field.getModifiers();
System.out.println("属性修饰符="+Modifier.toString(mod));
}
//解析方法信息
Method[] methodlist=cla.getDeclaredMethods();
for (Method method : methodlist) {
System.out.println("---------------");
System.out.println("方法="+method.toString());
System.out.println("方法名="+method.getName());
int mod=method.getModifiers();
System.out.println("方法修饰符="+Modifier.toString(mod));
System.out.println("方法参数列表");
Class pts[]=method.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
Class class1 = pts[i];
if(i!=0){
System.out.println(class1);
}
System.out.println("返回类型"+method.getReturnType());
}
}
}
属性=private int com.testBean.Stu.id
数据类型=int
属性名=id
属性修饰符=private
属性=private java.lang.String com.testBean.Stu.stuName
数据类型=class java.lang.String
属性名=stuName
属性修饰符=private
---------------
方法=public void com.testBean.Stu.setAll(int,java.lang.String)
方法名=setAll
方法修饰符=public
方法参数列表
返回类型void
class java.lang.String
返回类型void
---------------
方法=public void com.testBean.Stu.setId(int)
方法名=setId
方法修饰符=public
方法参数列表
返回类型void
---------------
方法=public java.lang.String com.testBean.Stu.getStuName()
方法名=getStuName
方法修饰符=public
方法参数列表
---------------
方法=public void com.testBean.Stu.setStuName(java.lang.String)
方法名=setStuName
方法修饰符=public
方法参数列表
返回类型void
---------------
方法=public java.lang.String com.testBean.Stu.toString()
方法名=toString
方法修饰符=public
方法参数列表
---------------
方法=public int com.testBean.Stu.getId()
方法名=getId
方法修饰符=public
方法参数列表
public static void mm(Class cla){
Field[] fs=cla.getDeclaredFields();
//fs=cla.getFields();加了这个的话就只获得public 公有的
for (Field f : fs) {
System.out.println(f.getName());
}
}
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class Test {
* 利用反射对实体bean赋值
*
*/
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException {
// TODO 自动生成方法存根
Stu stu=new Stu();
Test t=new Test();
List propertyVales=t.getPropertyVal();
for (Object object : propertyVales) {
System.out.println(object.toString());
}
List propertyNames=t.getPropertyNames(Stu.class);
for (Object object : propertyNames) {
System.out.println(object);
}
t.method(stu, propertyNames, propertyVales);
System.out.println(stu);
}
public List getPropertyVal(){
List propertyValues=new ArrayList();
propertyValues.add(10);
propertyValues.add("my name");
return propertyValues;
}
public void method(Object obj,List propertyNames,List propertyVales) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
Class cla=obj.getClass();
for(int i=0;i<propertyNames.size();i++){
Field f=cla.getField(propertyNames.get(i).toString());
f.set(obj, propertyVales.get(i));
}
}
public List getPropertyNames(Class cla){
List list=new ArrayList();
Field[] fs=cla.getDeclaredFields();
//fs=cla.getFields();加了这个的话就只获得public 公有的
for (Field f : fs) {
list.add(f.getName());
}
return list;
}
//输出:
my name
id
stuName
10my name
Class cla=obj.getClass();
for(int i=0;i<propertyNames.size();i++){
Field f=cla.getDeclaredField(propertyNames.get(i).toString());
f.setAccessible(true);//加了这句才能改私有的值
f.set(obj, propertyVales.get(i));
}
}