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; } } } } }
时间: 2024-09-27 09:11:28