一、主要方法
* < br>
* 获取方法: public Method getDeclaredMethod ( String name, Class< ? > . . . parameterTypes)
* < br>
* 获取属性: public Field getDeclaredField ( String name)
* < br>
* 获取属性类型( Field) : public Class< ? > getType ( )
* < br>
* 调用方法( Method) : public Object invoke ( Object obj, Object. . . args)
* < br>
二、完整代码
package reflect. andjavaclass;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class AttribAutoValue {
public static void main ( String[ ] args) {
String values = "name:张三|age:28|property:20000.209020|sex:男|birthday:1999-09-19" ;
PersonAutoValue person = ( PersonAutoValue) ClassInstanceFactoryAutoValue. getInstance ( PersonAutoValue. class , values) ;
System. out. println ( person. toString ( ) ) ;
}
}
class ClassInstanceFactoryAutoValue {
private ClassInstanceFactoryAutoValue ( ) {
}
public static < T> T getInstance ( Class< T> clazz , String values) {
try {
T t = ( T) clazz. getDeclaredConstructor ( ) . newInstance ( ) ;
BeanUtilAutoValue. autoValue ( t , values) ;
return t;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null;
}
}
}
class StringUtilAutoValue {
public static String attrib ( String value) {
if ( value == null || "" . equals ( value) ) {
return value;
} else if ( value. length ( ) == 1 ) {
return value. toUpperCase ( ) ;
} else {
return value. substring ( 0 , 1 ) . toUpperCase ( ) + value. substring ( 1 ) ;
}
}
}
class BeanUtilAutoValue {
public static void autoValue ( Object instance , String values) {
String[ ] result = values. split ( "\\|" ) ;
for ( int i = 0 ; i < result. length ; i++ ) {
String[ ] param = result[ i] . split ( ":" ) ;
try {
Field field = instance. getClass ( ) . getDeclaredField ( param[ 0 ] ) ;
Method method = instance. getClass ( ) . getDeclaredMethod ( "set" + StringUtilAutoValue. attrib ( param[ 0 ] ) , field. getType ( ) ) ;
Object attributeValue = BeanUtilAutoValue. convertAttributeValue ( field. getType ( ) . getName ( ) , param[ 1 ] ) ;
method. invoke ( instance , attributeValue) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
public static Object convertAttributeValue ( String attType , String value) {
if ( "double" . equals ( attType) || "java.lang.Double" . equals ( attType) ) {
return Double. parseDouble ( value) ;
} else if ( "int" . equals ( attType) || "java.lang.Integer" . equals ( attType) ) {
return Integer. parseInt ( value) ;
} else if ( "java.util.Date" . equals ( attType) ) {
SimpleDateFormat time;
if ( value. matches ( "\\d{4}-\\d{2}-\\d{2}" ) ) {
time= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
} else if ( value. matches ( "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}" ) ) {
time= new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
} else {
return new Date ( ) ;
}
try {
return time. parse ( value) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
return new Date ( ) ;
}
} else {
return value;
}
}
}
class PersonAutoValue {
private String name;
private int age;
private String sex;
private double property;
private Date birthday;
public void setAge ( int age) {
this . age = age;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public double getProperty ( ) {
return property;
}
public void setProperty ( double property) {
this . property = property;
}
public PersonAutoValue ( ) {
}
@Override
public String toString ( ) {
return "PersonAutoValue{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", property=" + property +
", birthday=" + birthday +
'}' ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Date getBirthday ( ) {
return birthday;
}
public void setBirthday ( Date birthday) {
this . birthday = birthday;
}
public int getAge ( ) {
return age;
}
}