import java.lang.reflect.Method;
private static Object getFieldValue(Object thisClass, String fieldName)
{
Object value = new Object();
Method method = null;
try {
String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);
method = thisClass.getClass().getMethod("get" + methodName);
value = method.invoke(thisClass);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
public static void main(String args[]) {
AuthServiceAuthorizeDO authserviceauthorizedo = new AuthServiceAuthorizeDO();
authserviceauthorizedo.setSv_gis_certified("1");
authserviceauthorizedo.setSv_gusername("2");
authserviceauthorizedo.setId("0");
YongHu yonghu = new YongHu();
yonghu.setUsername("bbbbb");
yonghu.setIs_certified("aaaaaaaaaaa");
yonghu.setId("ddddddddddd");
Field[] fields1 = authserviceauthorizedo.getClass().getDeclaredFields();//获取属性名称数组
Field[] fields2 = yonghu.getClass().getDeclaredFields();//获取属性名称数组
for (int i = 0; i < fields1.length; i++) {
Object valueObj1 = getFieldValue(authserviceauthorizedo ,fields1[i].getName());//获取属性值
for (int j = 0; j < fields2.length; j++) {
Object valueObj2 = getFieldValue(yonghu ,fields2[j].getName());//获取属性值
if(fields1[i].getName().equals("sv_g"+fields2[j].getName())){
switch (String.valueOf(valueObj1)) {
case "0":
System.out.println("---");
break;
case "1":
System.out.println(valueObj2);
break;
case "2":
System.out.println(SensitiveInfoUtils.toConceal(String.valueOf(valueObj2)));
break;
}
}
}
}
}
UserInfo userinfo = new UserInfo();
JSONObject jsonobj = JSONObject.fromObject(userinfo);
private static JSONObject JsonObj(String elementName1,String elementName2,Object valueObj1,Object valueObj2,JSONObject jsonobj){
if(elementName1.equals("sv_g"+elementName2)){
switch (String.valueOf(valueObj1)) {
case "0":
//不提供,先设置值为空,最终统一处理
jsonobj.element(elementName2, "");
break;
case "1":
//提供
break;
case "2":
//脱敏提供
jsonobj.element(elementName2, SensitiveInfoUtils.toConceal(String.valueOf(valueObj2)));
break;
}
}else if(elementName1.equals("sv_q"+elementName2)){
switch (String.valueOf(valueObj1)) {
case "0":
//不提供,属性值也不显示
jsonobj.remove(elementName2);
break;
case "1":
//提供
break;
case "2":
//脱敏提供
jsonobj.element(elementName2, SensitiveInfoUtils.toConceal(String.valueOf(valueObj2)));
break;
}
}
if(jsonobj!=null){
return jsonobj;
}
return null;
}