前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java---通过属性名反射获取get和set方法

Java---通过属性名反射获取get和set方法

作者头像
IT云清
发布2019-01-22 10:31:35
6.6K0
发布2019-01-22 10:31:35
举报
文章被收录于专栏:IT云清IT云清

最近有此需求:拿到name,需要执行setName()方法,这里记录一下,可以当做工具类直接使用:

代码语言:javascript
复制
package test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;

import everyDay.Person;

/**
 * Created by lightClouds917
 * Date 2017/12/29
 * Description:根据属性名反射获取get和set方法
 */
public class TestReflect3 {

	public static void main(String[] args)throws Exception{
		//test removeLine
		System.out.println(removeLine("abg_dwr"));
		System.out.println(removeLine("ab_wr"));
		System.out.println(removeLine("abgwr"));
		System.out.println(removeLine(null));

		//test get
		Person person1 = new Person();
		person1.setAge(11);
		person1.setName("旺旺");
		Object ob = getGetMethod(person1, "name");
		System.out.println(ob);
		
		//test set
		Person person2 = new Person();
		String field2 = "name";
		setValue(person2, person2.getClass(), field2, Person.class.getDeclaredField(field2).getType(), "汪汪");
		System.out.println(person2);
		//获取某个属性的类型
		System.out.println(Person.class.getDeclaredField("age").getType());
	
	}
	
	/**
	 * 根据属性,获取get方法
	 * @param ob 对象
	 * @param name 属性名
	 * @return
	 * @throws Exception
	 */
	public static Object getGetMethod(Object ob , String name)throws Exception{
		Method[] m = ob.getClass().getMethods();
		for(int i = 0;i < m.length;i++){
			if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
				return m[i].invoke(ob);
			}
		}
		return null;
	}
	
	/**
	 * 根据属性,拿到set方法,并把值set到对象中
	 * @param obj 对象
	 * @param clazz 对象的class
	 * @param fileName 需要设置值得属性
	 * @param typeClass 
	 * @param value
	 */
	public static void setValue(Object obj,Class<?> clazz,String filedName,Class<?> typeClass,Object value){
		filedName = removeLine(filedName);
		String methodName = "set" + filedName.substring(0,1).toUpperCase()+filedName.substring(1);
		try{
			Method method =  clazz.getDeclaredMethod(methodName, new Class[]{typeClass});
			method.invoke(obj, new Object[]{getClassTypeValue(typeClass, value)});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	/**
     * 通过class类型获取获取对应类型的值
     * @param typeClass class类型
     * @param value 值
     * @return Object
     */
	private static Object getClassTypeValue(Class<?> typeClass, Object value){
        if(typeClass == int.class  || value instanceof Integer){
            if(null == value){
                return 0;
            }
            return value;
        }else if(typeClass == short.class){
            if(null == value){
                return 0;
            }
            return value;
        }else if(typeClass == byte.class){
            if(null == value){
                return 0;
            }
            return value;
        }else if(typeClass == double.class){
            if(null == value){
                return 0;
            }
            return value;
        }else if(typeClass == long.class){
            if(null == value){
                return 0;
            }
            return value;
        }else if(typeClass == String.class){
            if(null == value){
                return "";
            }
            return value;
        }else if(typeClass == boolean.class){
            if(null == value){
                return true;
            }
            return value;
        }else if(typeClass == BigDecimal.class){
            if(null == value){
                return new BigDecimal(0);
            }
            return new BigDecimal(value+"");
        }else {
            return typeClass.cast(value);
        }
    }
    /**
     * 处理字符串  如:  abc_dex ---> abcDex
     * @param str
     * @return
     */
    public static  String removeLine(String str){
		if(null != str && str.contains("_")){
			int i = str.indexOf("_");
			char ch = str.charAt(i+1);
			char newCh = (ch+"").substring(0, 1).toUpperCase().toCharArray()[0];
			String newStr = str.replace(str.charAt(i+1), newCh);
			String newStr2 = newStr.replace("_", "");
			return newStr2;
		}
		return str;
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档