zoukankan      html  css  js  c++  java
  • 使用内省的方式操作JavaBean

    1. import java.beans.BeanInfo;  
    2. import java.beans.Introspector;  
    3. import java.beans.PropertyDescriptor;  
    4. import java.lang.reflect.Field;  
    5. import java.lang.reflect.Method;  
    6.   
    7. /** 
    8.  * 使用内省的方式操作JavaBean 
    9.  */  
    10. public class IntroSpectorTest {  
    11.   
    12.     public static void main(String[] args) throws Exception {  
    13.         ReflectPoint reflectPoint = new ReflectPoint(3,7);  
    14.           
    15.         //调用JavaBean中方法的传统作法  
    16.         Class classz = reflectPoint.getClass();  
    17.         Field[] fields = classz.getDeclaredFields();  
    18.         for (Field field : fields) {  
    19.             String name = "set" + field.getName().toUpperCase();  
    20.             Method method = classz.getMethod(name, int.class);  
    21.             method.invoke(reflectPoint, 3);  
    22.         }  
    23.         System.out.println(reflectPoint);  
    24.           
    25.         //使用内省的方式调用JavaBean的方法  
    26.         String propertyName = "x";  
    27.         //获得属性x的值  
    28.         Object retVal = getProperty2(reflectPoint, propertyName);  
    29.         System.out.println(retVal);  
    30.         //设置属性x的值  
    31.         setProperty(reflectPoint, propertyName,10);  
    32.         System.out.println(reflectPoint.getX());  
    33.           
    34.     }  
    35.   
    36.     /** 
    37.      * 设置属性的值 
    38.      * @param obj 属性所属的对象 
    39.      * @param propertyName 属性名 
    40.      * @param propertyValue 属性值 
    41.      */  
    42.     private static void setProperty(Object obj, String propertyName,Object propertyValue) throws Exception {  
    43.         PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,obj.getClass());  
    44.         Method setMethod = pd2.getWriteMethod();  
    45.         setMethod.invoke(obj, propertyValue);  
    46.     }  
    47.   
    48.     /** 
    49.      * 获得对象的属性值 
    50.      * @param obj 属性所属的对象 
    51.      * @param propertyName 属性名 
    52.      * @return 属性的值 
    53.      */  
    54.     private static Object getProperty(Object obj, String propertyName) throws Exception {  
    55.         PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());  
    56.         Method getMethod = pd.getReadMethod();  //获得get方法  
    57.         Object retVal = getMethod.invoke(obj);  
    58.         return retVal;  
    59.     }  
    60.       
    61.     /** 
    62.      * 使用内省操作javabean 
    63.      * @param obj 属性所属的对象 
    64.      * @param propertyName 属性名 
    65.      * @return 属性的值 
    66.      */  
    67.     private static Object getProperty2(Object obj, String propertyName) throws Exception {  
    68.         Object retVal = null;  
    69.         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
    70.         PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();  
    71.         for (PropertyDescriptor pd : pds) {  
    72.             if (pd.getName().equals(propertyName)) {  
    73.                 Method getMethod = pd.getReadMethod();  
    74.                 retVal = getMethod.invoke(obj);  
    75.                 break;  
    76.             }  
    77.         }  
    78.         return retVal;  
    79.     }  
    80. }
  • 相关阅读:
    9-10【H5混合实战】基于自定义WebView实现H5混合开发-3
    9-9【H5混合实战】基于定义WebView实现H5混合开发-2
    9-7【实战引用】球区入口实现
    9-5&9-6创建组件,使用组件
    9-4DAO首页大接口Dao层实现和调用-2
    9-4DAO首页大接口Dao层实现和调用-1
    9.3model层设计
    9.2熟悉接口好开发
    8-9 Flutter与Native通信-Android端实战
    8-8 Flutter与Native通信-Android端讲解
  • 原文地址:https://www.cnblogs.com/starhu/p/5605396.html
Copyright © 2011-2022 走看看