简单Java单极反射

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * 1.创建成员类,提供get set方法
 * 2.创建设置成员属性类
 * 3.创建字符串参数解析类
 * 4.
 * @author Administrator
 *
 */
//成员类
class Emp {
    private String name;
    private String job;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    @Override
    public String toString() {
        return "Emp [name=" + name + ", job=" + job + "]";
    }
}
class Dept {
    private String dname;
    private String loc;
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String getLoc() {
        return loc;
    }
    public void setLoc(String loc) {
        this.loc = loc;
    }
    @Override
    public String toString() {
        return "Dept [dname=" + dname + ", loc=" + loc + "]";
    }
}
//设置成员属性
class EmpAction {
    Emp emp = new Emp();    //实例化成员
//    设置成员属性
    public void setValue(String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
        BeanOperation.setBeanValue(this,value);
    }
    public Emp getEmp() {
        return emp;
    }
}
class DeptAction {
    private Dept dept = new Dept();
    public void setValue(String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
        BeanOperation.setBeanValue(this, value);
    }
    public Dept getDept() {
        return dept;
    }
}


//根据不同的分隔符,按不同的成员变量,以及对变量的名字和值进行分隔
class BeanOperation {
    private BeanOperation() {}
    public static void setBeanValue(Object actionObject,String msg) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
        String[] result = msg.split("\\|");    //不同成员变量间分隔
        for(int x = 0; x < result.length; x ++) {    //String[2]
            String[] temp = result[x].split(":");    //名称和值的分隔
            String attribute = temp[0];    //属性名称
            String value = temp[1];    //属性值
            
            String[] fields = attribute.split("\\.");    //fileds[0] = emp fileds[1] = name
            Object currentObject = ObjectUtils.getObject(actionObject,fields[0]);//(EmpAction,"emp") or (DeptAction,"dept")
            ObjectUtils.setObjectValue(currentObject, fields[1], value);    //(Emp,name,"kang")

        }
    }
}
//字符串处理
class StringUtils {
    private StringUtils() {}
    public static String initcap(String str) {
        return str.substring(0,1).toUpperCase() + str.substring(1);    //首字母大写+剩余字符串
    }
}

class ObjectUtils {
    private ObjectUtils() {}
//    获取类属性
    public static Object getObject(Object wrapObject,String attribute) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
        String methodName = "get" + StringUtils.initcap(attribute);    //调用字符串大小写处理方法获得"getEmp"字符串
        Field field = wrapObject.getClass().getDeclaredField(attribute);    //取得EmpAction类中Emp对象
        if(field == null) {    //子类没有找到实例化的Emp类,尝试从EmpAction父类中获取Emp类实例
            field = wrapObject.getClass().getField(attribute);
        }
        if(field == null) {    //都不存在,确认为Null
            return null;
        }
        Method method = wrapObject.getClass().getMethod(methodName);    //获取EmpAction类中的getEmp方法
            return method.invoke(wrapObject);    //getEmp().invoke(EmpAction) 返回emp
    }
//    设置成员变量
    public static void setObjectValue(Object wrapObject,String attribute,String value) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Field field = wrapObject.getClass().getDeclaredField(attribute);    //emp.name
        if(field == null) {    //子类中没有找到属性值,可能是父类中的属性
            field = wrapObject.getClass().getField(attribute);
        }
        if(field == null) {    //都不存在,确认为Null
            return ;
        }
        String methodName = "set" + StringUtils.initcap(attribute);    //setName
        Method method = wrapObject.getClass().getMethod(methodName,field.getType());    //Emp.getMethod(setName,emp.name(String))
        method.invoke(wrapObject, value);    //(setName(字符串参数)).invoke(Emp,"kang")
    }
}
/*结果:
 * Emp [name=康康, job=clerk]
 * Dept [dname=技术部, loc=北京]
 */
public class Test {
    public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
        String value = "emp.name:康康|emp.job:clerk";
        String value2 = "dept.dname:技术部|dept.loc:北京";
        EmpAction action = new EmpAction();
        DeptAction action2 = new DeptAction();
        action.setValue(value);    //传入字符串参数设置并获取相应成员类属性的值
        action2.setValue(value2);
        System.out.println(action.getEmp());
        System.out.println(action2.getDept());
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值