public class User {
private String name;
private String sex;
private String phone;
private String hobby1;
private String hobby2;
private String address1;
private String address2;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby1() {
return hobby1;
}
public void setHobby1(String hobby1) {
this.hobby1 = hobby1;
}
public String getHobby2() {
return hobby2;
}
public void setHobby2(String hobby2) {
this.hobby2 = hobby2;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
}
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import cn.com.szhtkj.dto.XmyeDtoOutput;
public class ReflectClass {
//获取实例的方法
public static List<String> getMethodForClass(Class<?> clazz, String keyWord) {
List<String> list = new ArrayList<String>();
Method[] declaredMethods = clazz.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i];
String name = method.getName();
if (name.contains(keyWord) && isNumber(name.replace(keyWord, ""))) {
list.add(name);
}
}
return list;
}
.//获取字段值
public static List<String> getValueMethod(Class<?> clazz, Object obj, List<String> nameList) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < nameList.size(); i++) {
Method declaredMethod;
try {
declaredMethod = clazz.getDeclaredMethod(nameList.get(i));
String ret = (String) declaredMethod.invoke(obj);
list.add(ret);
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
//判断是否是数字
private static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
//字符串排序
public static void sort(List<String> list) {
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return extractInt(o1) - extractInt(o2);
}
int extractInt(String s) {
String num = s.replaceAll("\\D", "");
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setAddress1("天津");
user.setAddress2("北京");
user.setHobby1("篮球");
user.setHobby2("足球");
String keyWord1 = "getAddress";
String keyWord2 = "getHobby";
Class<?> clazz = user.getClass();
List<String> methodNameList1 = getMethodForClass(clazz, keyWord1);
sort(methodNameList1);
List<String> valueMethod1 = getValueMethod(clazz, user, methodNameList1);
List<String> methodNameList2 = getMethodForClass(clazz, keyWord2);
sort(methodNameList2);
List<String> valueMethod2 = getValueMethod(clazz, user, methodNameList2);
System.out.println(methodNameList1);
System.out.println(methodNameList2);
System.out.println(valueMethod1);
System.out.println(valueMethod2);
int count=1;
for (int i = 0; i < methodNameList1.size(); i++) {
if (valueMethod1.get(i)!=null || valueMethod2.get(i)!=null) {
count |= 1<<i;
}
}
System.out.println(count);
}
}
[getAddress1, getAddress2]
[getHobby1, getHobby2]
[天津, 北京]
[篮球, 足球]
3