自动填充属性值【反射:调用必须存在对象,成员变量、方法、构造函数都是以对象的形式调用{一切皆对象} 】
0
public class Student {
public String name;
public String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
1
public class TestDemo {
public static void main(String[] args) throws Exception {
//给定一串属性:值字符串
String value = "student.name:张三|student.age:22";
StudentAction action = new StudentAction();
//调用action的setValue
action.setValue(value);
System.out.println(action.getStudent());
}
}
2
public class StudentAction {
//初始化一个Student对象,当然你也可以使用反射初始化
private Student student = new Student();
//调用ValueSplit 方法
public void setValue(String valueGroup) throws Exception{
ValueSplit.setValue(this, valueGroup);
}
//返回初始化的Student对象
public Student getStudent(){
return student;
}
}
3,
public class ValueSplit {
public static void setValue(Object opObject,String valueGroup) throws Exception{
//student.name:张三
//student.age:22
String proGroup[] = valueGroup.split("\\|");
for (String string : proGroup) {
//student.name
//张三
String proValue[] = string.split("\\:");
//student
//name
String aimValue[] = proValue[0].split("\\.");
//通过反射调用StudentAction中的getStudent()方法
Object obj = OprationUtils.getFun(opObject, aimValue[0]);
//通过反射给Student的name赋值为“张三”,age赋值为“22”
OprationUtils.setFun(obj, aimValue[1], proValue[1]);
}
}
}
4,
public class OprationUtils {
public static void setFun(Object opObject,String setfun,String value) throws Exception{
//获取当前类中名称为setfun接收的值的成员变量对象
Field field = opObject.getClass().getDeclaredField(setfun);
//如果本类中不存在对应名称的成员变量,则从父类中再次查找
if(field == null){
field = opObject.getClass().getField(setfun);
}
//通过方法名称获得方法对象
Method method = opObject.getClass()
.getMethod("set"+BeanObjectUtils.initCap(setfun), field.getType());
//通过反射调用方法并赋值
method.invoke(opObject, value);
}
public static Object getFun(Object opObject,String getfun) throws Exception{
Field field = opObject.getClass().getDeclaredField(getfun);
if(field == null){
field = opObject.getClass().getField(getfun);
}
Method method = opObject.getClass()
.getMethod("get"+BeanObjectUtils.initCap(getfun));
//返回通过反射调用get方法获取的对象
return method.invoke(opObject);
}
}
5,
public class BeanObjectUtils {
//首字母大写
public static String initCap(String initVal){
return initVal.substring(0,1).toUpperCase()+initVal.substring(1);
}
}
运行结果
Student [name=张三, age=22]
下面我们新建一个Company
0
public class Company {
public String companyName;
public String address;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Company [companyName=" + companyName + ", address=" + address
+ "]";
}
}
1
public class TestDemo {
public static void main(String[] args) throws Exception {
String value = "company.companyName:旅游公司|company.address:北京天坛";
CompanyAction action = new CompanyAction();
action.setValue(value);
System.out.println(action.getCompany());
}
}
2
public class CompanyAction {
private Company company = new Company();
public void setValue(String valueGroup) throws Exception{
ValueSplit.setValue(this, valueGroup);
}
public Company getCompany(){
return company;
}
}
运行结果
Company [companyName=旅游公司, address=北京天坛]
我们写的是通用的