为了完善昨天的工作,今天写了一个有java.lang.string类型转换为其他类型的工具类。自任为这个类写的很烂,没有进行进一步的封装,在程序中出现了不幽雅的if(................){}else if(............){}else{}语句使整个类不够OO,好在基本功能都实现。由于时间紧促现在只好先这样了。
下面是代码:
public class stringConvert {
private static Log log = LogFactory.getLog(stringConvert.class);
/**
* convert thr String to Double,Integer,Long
* @param String str
* @param class type
* @return
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
*/
public Object StringConvertTo(String str, Class type) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
System.out.println("run in convert");
int leng=0;
System.out.println(type.getName());
Object value = new Object();
if(type.getName().equals("java.util.Date"))
{
DateUtil du = new DateUtil();
try {
value = du.convertStringToDate(str);
} catch (ParseException e) {
e.printStackTrace();
}
}else if(type.getName().equals("java.lang.String"))
{
value = str;
}else if(type.getName().equals("java.lang.Integer")){
Method[] methodArray = type.getMethods();
String methodName="";
leng = methodArray.length;
for(int i=0;i<leng;i++)
{
methodName = methodArray[i].getName();
if(methodName.length()>5 && methodName.substring(0,5).equals("parse"))
{
Method methodBody = type.getMethod(methodName, new Class[]{String.class});
Object[] obj = new Object[1];
obj[0] = str;
value = methodBody.invoke(type,obj);
break;
}
}
}else{
System.out.println("not match type"+type.getName());
}
return value;
}
}
另对昨天的代码做了写修改:
package com.test.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.validator.DynaValidatorForm;
import com.test.service.IcreateEntity;
import com.test.util.stringConvert;
/**
* set the DynaValidatorForm attribute to the entity object
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @param Object entity
* @param DynaValidatorForm addForm
* @return entity Object
*
*
* @author jianglie */
public class createEntity implements IcreateEntity{
private static Log log = LogFactory.getLog(createEntity.class);
stringConvert strConverter= new stringConvert();
public Object updateValue(Object entity, DynaValidatorForm form) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException
{
Class cobj; // object
Method[] methodArray; // the object declare method
String mehtodName; // the method name
String AttributeName; // the attribute name in form
String strValue;
Method methodBody; // the method body you can invoke
Class[] methodParam; // the method's parameter type
Object[] obj = new Object[1];
int leng;
/**use reflect to obtain the user object*/
cobj = entity.getClass();
System.out.println(cobj);
/**use reflect to obtain the user's all get method*/
methodArray = cobj.getMethods();
leng = methodArray.length;
//System.out.println(leng);
for(int i=0;i<leng;i++)
{
/**obtain the method name*/
mehtodName = methodArray[i].getName();
AttributeName = mehtodName.substring(3).toLowerCase();
/**judge the method is a setter method or not*/
if(mehtodName.substring(0,3).equals("set")&&!AttributeName.equals("id"))
{
//System.out.println("AttributeName:"+AttributeName);
/**use reflect to obtain the method parameter's type*/
methodParam = methodArray[i].getParameterTypes();
//System.out.println("methodParam"+methodParam[0]);
/**obtain the method body for invoke*/
methodBody = cobj.getMethod(mehtodName, new Class[]{methodParam[0]});
//System.out.println("create method body sucess");
/**generate parameter Array*/
try{
strValue = form.getString(AttributeName)==null?"":form.getString(AttributeName).trim();
//System.out.println("strValue:"+strValue);
obj[0]=strConverter.StringConvertTo(strValue,methodParam[0]);
}catch (Exception e) {
continue;
}
System.out.println("obj[0]"+obj[0]);
methodBody.invoke(entity,(Object[]) obj);
//System.out.println("set "+AttributeName+" sucess");
}
}
return entity;
}
}
希望有经历看到这句的人,顺便帮我重构下。
下面是代码:
public class stringConvert {
private static Log log = LogFactory.getLog(stringConvert.class);
/**
* convert thr String to Double,Integer,Long
* @param String str
* @param class type
* @return
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
*/
public Object StringConvertTo(String str, Class type) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
System.out.println("run in convert");
int leng=0;
System.out.println(type.getName());
Object value = new Object();
if(type.getName().equals("java.util.Date"))
{
DateUtil du = new DateUtil();
try {
value = du.convertStringToDate(str);
} catch (ParseException e) {
e.printStackTrace();
}
}else if(type.getName().equals("java.lang.String"))
{
value = str;
}else if(type.getName().equals("java.lang.Integer")){
Method[] methodArray = type.getMethods();
String methodName="";
leng = methodArray.length;
for(int i=0;i<leng;i++)
{
methodName = methodArray[i].getName();
if(methodName.length()>5 && methodName.substring(0,5).equals("parse"))
{
Method methodBody = type.getMethod(methodName, new Class[]{String.class});
Object[] obj = new Object[1];
obj[0] = str;
value = methodBody.invoke(type,obj);
break;
}
}
}else{
System.out.println("not match type"+type.getName());
}
return value;
}
}
另对昨天的代码做了写修改:
package com.test.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.validator.DynaValidatorForm;
import com.test.service.IcreateEntity;
import com.test.util.stringConvert;
/**
* set the DynaValidatorForm attribute to the entity object
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @param Object entity
* @param DynaValidatorForm addForm
* @return entity Object
*
*
* @author jianglie */
public class createEntity implements IcreateEntity{
private static Log log = LogFactory.getLog(createEntity.class);
stringConvert strConverter= new stringConvert();
public Object updateValue(Object entity, DynaValidatorForm form) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException
{
Class cobj; // object
Method[] methodArray; // the object declare method
String mehtodName; // the method name
String AttributeName; // the attribute name in form
String strValue;
Method methodBody; // the method body you can invoke
Class[] methodParam; // the method's parameter type
Object[] obj = new Object[1];
int leng;
/**use reflect to obtain the user object*/
cobj = entity.getClass();
System.out.println(cobj);
/**use reflect to obtain the user's all get method*/
methodArray = cobj.getMethods();
leng = methodArray.length;
//System.out.println(leng);
for(int i=0;i<leng;i++)
{
/**obtain the method name*/
mehtodName = methodArray[i].getName();
AttributeName = mehtodName.substring(3).toLowerCase();
/**judge the method is a setter method or not*/
if(mehtodName.substring(0,3).equals("set")&&!AttributeName.equals("id"))
{
//System.out.println("AttributeName:"+AttributeName);
/**use reflect to obtain the method parameter's type*/
methodParam = methodArray[i].getParameterTypes();
//System.out.println("methodParam"+methodParam[0]);
/**obtain the method body for invoke*/
methodBody = cobj.getMethod(mehtodName, new Class[]{methodParam[0]});
//System.out.println("create method body sucess");
/**generate parameter Array*/
try{
strValue = form.getString(AttributeName)==null?"":form.getString(AttributeName).trim();
//System.out.println("strValue:"+strValue);
obj[0]=strConverter.StringConvertTo(strValue,methodParam[0]);
}catch (Exception e) {
continue;
}
System.out.println("obj[0]"+obj[0]);
methodBody.invoke(entity,(Object[]) obj);
//System.out.println("set "+AttributeName+" sucess");
}
}
return entity;
}
}
希望有经历看到这句的人,顺便帮我重构下。