利用反射获取getter方法转换成map

package com.bx.test;

import com.bx.pojo.Tbbhinfo;
import java.beans.Introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Lhj
 * @Date 2020/9/22 9:16
 */
public class ClassGet {

    //获取类属性名
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IntrospectionException, InvocationTargetException {
      
        User user = new User();
        user.setUsername("zhanghao");
        user.setUserpassword("mima");
        Map<String, String> map = ClassGet.fun(user);
		System.out.println(map);
    }
	
	//第一种方法
    public static void method(Tbbhinfo obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        Class clazz = obj.getClass();
        Field[] fields = obj.getClass().getDeclaredFields();//获得属性bai
        for (Field field : fields) {
            PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
            Method getMethod = pd.getReadMethod();//获得get方法du
            //System.out.println("getMethod---------"+getMethod);
            Object o = getMethod.invoke(obj);//执行get方法返zhi回一个daoObject
            System.out.println(o.toString());
        }
    }

	//第二种
public static Map<String, String> fun(Object th){
        Map<String, String> map = new HashMap<String, String>();
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo(th.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!key.equals("class") && !key.equals("pageNo") && !key.equals("pageSize")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();

                    Object value = getter.invoke(th);
                    String value1 = value + "";
                    System.out.println("key:"+key+"--------"+"value1:"+value1);
                    map.put(key, value1);

                }
            }
            System.out.println("反射转换完成");

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return map;
    }

}


实体类

package com.bx.test;
 
public class User {
	private String username;
	private String userpassword;
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	public String getUserpassword() {
		return userpassword;
	}
 
	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}
 
}
 

输出结果

key:username--------value1:zhanghao
key:userpassword--------value1:mima
反射转换完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值