java利用反射给对象赋值
public static Object getFiledName(Object obj) throws Exception {
if(obj==null) return obj;
Field[] fields=obj.getClass().getDeclaredFields();
String type;
String Name;
for(int i=0; i<fields.length; i++){
fields[i].setAccessible(true);
type=fields[i].getType().toString();
Name=fields[i].getName().substring(0, 1).toUpperCase()+fields[i].getName().substring(1);
Method method = null;
if(type.equals("class java.lang.String")){
method = obj.getClass().getMethod("get"+Name);
String value = (String) method.invoke(obj);
method = obj.getClass().getMethod("set"+Name,String.class);
method.invoke(obj, value!=null?value.replaceAll("<","(").replaceAll(">",")"):value);
}
}
return obj;
}
public static void main(String[] args) throws Exception {
TalentPool talentpool = new TalentPool();
talentpool.setBirthPlace("sddsaa<2>");
talentpool.setCandidateId(2222);
getFiledName(talentpool);
}