希望能对程序做简化,输入字符串"属性名称:属性值|属性名称:属性值|属性名称:属性值|....",就可以将类中的属性设置好。希望通过此程序实现任意的简单Java类的属性设置。
所有的操作是通过TestDemo类调用EmpAction类实现的,而EmpAction类的主要作用是在于定位要操作的属
性类型。同时该程序应该符合于所有简单的Java类开发形式,因此对于我们的设计而言必须有一个单独的类
(BeanOperation)
性类型。同时该程序应该符合于所有简单的Java类开发形式,因此对于我们的设计而言必须有一个单独的类
(BeanOperation)
Emp类设计
package Class;
public class Emp {
private String ename;
private String job;
public String getEname() {
return ename;
}
public void setEname(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+'\''+'}';
}
}
EmpAction类设计
package Class;
import java.lang.reflect.InvocationTargetException;
public class EmpAction {
private Emp emp =new Emp();
public void setValues(String value) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
BeanOperation.setBeanValue(this,value);
}
public Emp getEmp()
{
return this.emp;
}
}
BeanOperation类设计(公共程序类)
package Class;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanOperation {
private BeanOperation() {}
public static void setBeanValue(Object actionobject,String className) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String[] result = className.split("\\|");
for(int i = 0;i <result.length;i++)
{
String[] temp = result[i].split(":");
String attribute = temp[0];
String value = temp[1];
String[] fields = attribute.split("\\.");
Object currentObject = getObject(actionobject,fields[0]);
//System.out.println(fields[1]);
setObjectValue(currentObject,fields[1],temp[1]);
}
}
public static Object getObject(Object object, String attribute) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
String methodName = "get"+initCap(attribute);
Field field = object.getClass().getDeclaredField(attribute);
if(field == null)
{
field = object.getClass().getDeclaredField(attribute);
}
if(field == null)
{
return null;
}
Method method = object.getClass().getMethod(methodName);
return method.invoke(object);
}
public static void setObjectValue(Object object, String attribute, String value) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
Field field = object.getClass().getDeclaredField(attribute);
if(field == null)
{
field = object.getClass().getField(attribute);
}
if(field == null)
{
return ;
}
String methodName = "set"+initCap(attribute);
Method setMethod = object.getClass().getMethod(methodName,field.getType());
setMethod.invoke(object, value);
}
public static Object getRealObject(Object object, String className) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
// TODO Auto-generated method stub
//拿到EmpAction类中的Class对象
Class<?> class1=object.getClass();
Field field=class1.getDeclaredField(className);
//在当前类中没有找到此属性
if(field == null)
{
//再从父类中找到此属性
field =class1.getField(className);
if(field == null)
{
return null;
}
}
//拼接getter方法名字
String methodName = "get"+initCap(className);
Method getObjectMethod = class1.getDeclaredMethod(methodName);
//相当于调用empAction.getEmp();
return getObjectMethod.invoke(object);
}
//首字母大写操作
public static String initCap(String value)
{
return value.substring(0, 1).toUpperCase()+value.substring(1);
}
}
测试类设计
package Class;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String value = "emp.ename:lxy|emp.job:student";
EmpAction empAction=new EmpAction();
empAction.setValues(value);
System.out.println(empAction.getEmp());
}
}