JAVA“动态”为类添加属性

来源 : https://www.cnblogs.com/frinder6/p/7204574.html


部分参考:http://www.cnblogs.com/zy2009/p/6725843.html

pom.xml中添加:

1
2
3
4
5
6
7
8
9
10
11
     commons-beanutils
     commons-beanutils
     1.9 . 3
 
     cglib
     cglib-nodep
     3.2 . 4

  

使用cglib动态生成类与使用 commons-beanutils获取源对象属性-类型集合,封装成新对象并设置值代码:

package com.frinder.util;
 
import com.google.common.collect.Maps;
import com.kiktech.entity.LoggerEntity;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
 
/**
  * Created by frinder on 2017/7/18.
  */
public class ReflectUtil {
 
     static Logger logger = LoggerFactory.getLogger(ReflectUtil. class );
 
     public static Object getTarget(Object dest, Map addProperties) {
         // get property map
         PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
         PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
         Map propertyMap = Maps.newHashMap();
         for (PropertyDescriptor d : descriptors) {
             if (! "class" .equalsIgnoreCase(d.getName())) {
                 propertyMap.put(d.getName(), d.getPropertyType());
             }
         }
         // add extra properties
         addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
         // new dynamic bean
         DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
         // add old value
         propertyMap.forEach((k, v) -> {
             try {
                 // filter extra properties
                 if (!addProperties.containsKey(k)) {
                     dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
                 }
             } catch (Exception e) {
                 logger.error(e.getMessage(), e);
             }
         });
         // add extra value
         addProperties.forEach((k, v) -> {
             try {
                 dynamicBean.setValue(k, v);
             } catch (Exception e) {
                 logger.error(e.getMessage(), e);
             }
         });
         Object target = dynamicBean.getTarget();
         return target;
     }
 
 
     public static void main(String[] args) {
         LoggerEntity entity = new LoggerEntity();
         entity.setAppName( "appname" );
         entity.setOperator( "add" );
         entity.setResult( "result" );
         entity.setUri( "uri" );
         entity.setMethod( "method" );
         Map addProperties = new HashMap() {{
             put( "hello" , "world" );
             put( "abc" , "123" );
         }};
         System.out.println(getTarget(entity, addProperties));
     }
 
 
     public static class DynamicBean {
         /**
          * 目标对象
          */
         private Object target;
 
         /**
          * 属性集合
          */
         private BeanMap beanMap;
 
         public DynamicBean(Class superclass, Map propertyMap) {
             this .target = generateBean(superclass, propertyMap);
             this .beanMap = BeanMap.create( this .target);
         }
 
 
         /**
          * bean 添加属性和值
          *
          * @param property
          * @param value
          */
         public void setValue(String property, Object value) {
             beanMap.put(property, value);
         }
 
         /**
          * 获取属性值
          *
          * @param property
          * @return
          */
         public Object getValue(String property) {
             return beanMap.get(property);
         }
 
         /**
          * 获取对象
          *
          * @return
          */
         public Object getTarget() {
             return this .target;
         }
 
 
         /**
          * 根据属性生成对象
          *
          * @param superclass
          * @param propertyMap
          * @return
          */
         private Object generateBean(Class superclass, Map propertyMap) {
             BeanGenerator generator = new BeanGenerator();
             if ( null != superclass) {
                 generator.setSuperclass(superclass);
             }
             BeanGenerator.addProperties(generator, propertyMap);
             return generator.create();
         }
     }
 
}

  

 LoggerEntity代码:

package com.frinder.entity;
 
import net.logstash.logback.encoder.org.apache.commons.lang.builder.ReflectionToStringBuilder;
 
/**
  * Created by frinder on 2017/5/24.
  */
public class LoggerEntity {
 
     private String method;
     private String uri;
     private Object[] args;
     private Object result;
     private String operator;
     private String appName;
 
     /**
      * 获取当前对象
      *
      * @param method
      * @param uri
      * @param args
      * @param result
      * @return
      */
     public LoggerEntity get(String method, String uri, Object[] args, Object result, String operator, String appName) {
         setMethod(method);
         setUri(uri);
         setArgs(args);
         setResult(result);
         setOperator(operator);
         setAppName(appName);
         return this ;
     }
 
     public String getMethod() {
         return method;
     }
 
     public void setMethod(String method) {
         this .method = method;
     }
 
     public String getUri() {
         return uri;
     }
 
     public void setUri(String uri) {
         this .uri = uri;
     }
 
     public Object[] getArgs() {
         return args;
     }
 
     public void setArgs(Object[] args) {
         this .args = args;
     }
 
     public Object getResult() {
         return result;
     }
 
     public void setResult(Object result) {
         this .result = result;
     }
 
     public String getOperator() {
         return operator;
     }
 
     public void setOperator(String operator) {
         this .operator = operator;
     }
 
     public String getAppName() {
         return appName;
     }
 
     public void setAppName(String appName) {
         this .appName = appName;
     }
 
     @Override
     public String toString() {
         return ReflectionToStringBuilder.toString( this );
     }
}

测试结果,需要在调试模式下才能看到:

JAVA“动态”为类添加属性_第1张图片

你可能感兴趣的:(JAVA“动态”为类添加属性)