zoukankan      html  css  js  c++  java
  • 通过PropertyDescriptor反映射调用set和get方法

    package com.zhoushun;
    import java.lang.reflect.Method;
    import java.lang.reflect.Field;
    import java.beans.PropertyDescriptor;
     
    public class PropertyUtil {
    	@SuppressWarnings("unchecked")
    	public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) {
    		StringBuffer sb = new StringBuffer();//构建一个可变字符串用来构建方法名称
    		Method setMethod = null;
    		Method getMethod = null;
    		PropertyDescriptor pd = null;
    		try {
    			Field f = clazz.getDeclaredField(propertyName);//根据字段名来获取字段
    			if (f!= null) {
    				//构建方法的后缀
    			   String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    			   sb.append("set" + methodEnd);//构建set方法
    			   setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ f.getType() });
    			   sb.delete(0, sb.length());//清空整个可变字符串
    			   sb.append("get" + methodEnd);//构建get方法
    			   //构建get 方法
    			   getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ });
    			   //构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
    			   pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
    			}
    		} catch (Exception ex) {
    				ex.printStackTrace();
    		}
    	
    		return pd;
    	}
    	
    	@SuppressWarnings("unchecked")
    	public static void setProperty(Object obj,String propertyName,Object value){
    		Class clazz = obj.getClass();//获取对象的类型
    		PropertyDescriptor pd = getPropertyDescriptor(clazz,propertyName);//获取 clazz 类型中的 propertyName 的属性描述器
    		Method setMethod = pd.getWriteMethod();//从属性描述器中获取 set 方法
    		try {
    			setMethod.invoke(obj, new Object[]{value});//调用 set 方法将传入的value值保存属性中去
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    	
    	@SuppressWarnings("unchecked")
    	public static Object getProperty(Object obj, String propertyName){
    	   Class clazz = obj.getClass();//获取对象的类型
    	   PropertyDescriptor pd = getPropertyDescriptor(clazz,propertyName);//获取 clazz 类型中的 propertyName 的属性描述器
    	   Method getMethod = pd.getReadMethod();//从属性描述器中获取 get 方法
    	   Object value =null ;
    	   try {
    		   value = getMethod.invoke(clazz, new Object[]{});//调用方法获取方法的返回值
    	   } catch (Exception e) {
    		   e.printStackTrace();
    	   }
    	   return value;//返回值
    	}
    }
    
     public String getFiled(Object object, String field) {
            Class<? extends Object> clazz  = object.getClass();
            PropertyDescriptor pd = null;
            Method getMethod = null;
            try {
                pd = new PropertyDescriptor(field, clazz);
                if (null != pd) {
                    // 获取  这个 field 属性 的get方法
                    getMethod = pd.getReadMethod();
                    Object invoke = getMethod.invoke(object);
                    if(invoke==null) {
                        invoke = "";
                    }
                    return invoke.toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }
    

      

    参考https://blog.csdn.net/z69183787/article/details/8443777

  • 相关阅读:
    动态面板——axure线框图部件库介绍
    如何安装(装载)axure组件(部件)
    文本框、文本编辑框、按钮——axure线框图部件库介绍
    图片热区——axure线框图部件库介绍
    水平线、垂直线——axure线框图部件库介绍
    矩形、占位符组件——axure线框图部件库介绍
    文本面板——axure线框图部件库介绍
    windows server 2008 R2服务器安装IIS并添加网站
    使用SplitContainer来实现隐藏窗口的部分内容(转)
    C# VS2010结合SQL Server 2008数据库编程实现方法
  • 原文地址:https://www.cnblogs.com/alice-cj/p/10357876.html
Copyright © 2011-2022 走看看