反射获取类内方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值