反射操作VO

    xiaoxiao2021-04-16  285

    现在所有的操作是通过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 () ); } }

     


    最新回复(0)