package cn.protect;
import org.apache.commons.codec.binary.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* @Description:
*/
public class Test2 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
Object obj = say(new PP());
System.out.println(obj);
}
public static Object say(Object obj) throws InvocationTargetException, IllegalAccessException {
Map<String,Method> map = new HashMap<>();
Method[] methods = obj.getClass().getMethods();
for (Method method : methods) {
map.put(method.getName(),method);
}
int i = 0;
while ( ++i<30 ){
Method getMethod = map.get("getDay" + i);
if ( getMethod == null ){
continue;
}
Object result = getMethod.invoke(obj , new Object[]{});//根据自己需求业务装换类型数据
if ( result == null ){
Method setMethod = map.get("setDay" + i);
Optional.ofNullable(setMethod).ifPresent(k-> {
try {
k.invoke(obj,new Object[]{"default_value"});
} catch (Exception e) {
System.out.println("方法错误");
}
});
}
}
return obj;
}
}
class PP{
private String day1;
private String day2;
public String getDay2() {
return day2;
}
public void setDay2(String day2) {
this.day2 = day2;
}
public String getDay1() {
return day1;
}
public void setDay1(String day1) {
this.day1 = day1;
}
@Override
public String toString() {
return "PP{" +
"day1='" + day1 + '\'' +
", day2='" + day2 + '\'' +
'}';
}
}