反射操作VO

Java员工信息类开发与测试
该博客围绕Java开发展开,主要介绍了员工信息类的创建,包括定位操作的属性类型,还提到创建单独的BeanOperation类实现适配,最后有测试类对整体进行测试,属于后端开发的Java领域。
现在所有的操作是通过TestDemo类调⽤EmpAction类实现的,⽽EmpAction类的主要作⽤是在于定位要操作的属性类型。同时该程序应该符合于所有简单的Java类开发形式,因此对于我们的设计⽽⾔必须有⼀个单独的类(BeanOperation)(实现此适配。范例:Emp类设计(简单Java类)

1.员工信息类(简单Java类)

public class Emp {
    private String ename;
    private String job;
    public String getName() {
        return ename;
    }
    public void setName(String ename) {
        this.ename = ename;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String toString() {
        return "Emp{" +
                "ename='" + ename + '\'' +
        ", job='" + job + '\'' +
                '}';
    }
}

2. 定位要操作的属性类型

import java.lang.reflect.InvocationTargetException;
//操作Emp的类,提供给用户使用
public class EmpAction {
    private  Emp emp=new Emp ();
    public void setEmpValue(String value) throws InvocationTargetException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
        BeanUtil.setBeanValue(this,value);
    }
    public Emp getEmp(){
        return emp;
    }
}

3.单独的类(BeanOperation)(实现此适配)

//操作简单类属性设置的工具类
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanUtil {
    private BeanUtil() {
    }
    //actionObj拿到提供给用户使用的XXAction类
//beanValue要设置的具体值
    //emp.name:dange|emp.job:Coder
    public static void setBeanValue(Object actionObject, String msg) {
        //1.字符串拆分
        String[] result = msg.split ( "\\|" );
        for (int i = 0; i < result.length; i++) {
            String[] temp = result[i].split ( ":" );
            String attribute = temp[0];
            String value = result[1];
            String[] fields = attribute.split ( "\\." );
            Object currentObject = null;
            try {
                currentObject = getObject ( actionObject, fields[0] );
            } catch (InvocationTargetException e) {
                e.printStackTrace ( );
            } catch (IllegalAccessException e) {
                e.printStackTrace ( );
            }
            try {
                setObjectValue ( currentObject, fields[1], temp[1] );
            } catch (NoSuchFieldException e) {
                e.printStackTrace ( );
            } catch (NoSuchMethodException e) {
                e.printStackTrace ( );
            } catch (InvocationTargetException e) {
                e.printStackTrace ( );
            } catch (IllegalAccessException e) {
                e.printStackTrace ( );
            }
        }
    }
    private static String initCap(String str) {
        return str.substring ( 0, 1 ).toUpperCase ( ) + str.substring ( 1 );
    }
    //根据Xxaction类取得真正操作的主题类Emp
    //通过反射调用getEmp()
    private static Object getObject(Object obj, String attribute) throws InvocationTargetException, IllegalAccessException {
        //方法名
        String methodName = "get" + initCap ( attribute );
        Field field = null;
        try {
            field = obj.getClass ( ).getDeclaredField ( attribute );
        } catch (NoSuchFieldException e) {
            e.printStackTrace ( );
        }
        if (field == null) {
            try {
                field = obj.getClass ( ).getDeclaredField ( attribute );
            } catch (NoSuchFieldException e) {
                e.printStackTrace ( );
            }
        }
        if (field == null) {
            return null;
        }
        Method method = null;
        try {
            method = obj.getClass ( ).getMethod ( methodName );
        } catch (NoSuchMethodException e) {
            e.printStackTrace ( );
        }
        try {
            return method.invoke ( obj );
        } catch (IllegalAccessException e) {
            e.printStackTrace ( );
        } catch (InvocationTargetException e) {
            e.printStackTrace ( );
        }
        return method.invoke ( obj );
    }
    public static void setObjectValue(Object obj, String attribute, String value) throws NoSuchFieldException,
            NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Field field = obj.getClass ( ).getDeclaredField ( attribute );
        if (field == null) {
            field = obj.getClass ( ).getField ( attribute );
        }
        if (field == null) {
            return;
        }
        String methodName = "set" + initCap ( attribute );
        Method setMethod = obj.getClass ( ).getMethod ( methodName, field.getType ( ) );
        setMethod.invoke ( obj, value );
    }
}

4.测试类 

import java.lang.reflect.InvocationTargetException;
public class Test{
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        String str="emp.ename:dange|emp.job:Java Coder";
        EmpAction empAction=new EmpAction ();
        empAction.setEmpValue ( str);
        System.out.println (empAction.getEmp () );
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值