码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA“动态”为类添加属性

时间:2017-07-19 11:50:19      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:node   ons   nod   ogg   int   foreach   over   object   create   

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

pom.xml中添加:

<dependency>
	<groupId>commons-beanutils</groupId>
	<artifactId>commons-beanutils</artifactId>
	<version>1.9.3</version>
</dependency>

<dependency>
	<groupId>cglib</groupId>
	<artifactId>cglib-nodep</artifactId>
	<version>3.2.4</version>
</dependency>

  

使用cglib动态生成类代码:

package com.frinder.util;


import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

import java.util.Map;

/**
 * Created by frinder on 2017/7/18.
 */
public class DynamicBean {

    /**
     * 目标对象
     */
    private Object target;

    /**
     * 属性集合
     */
    private BeanMap beanMap;

    public DynamicBean() {
    }

    public DynamicBean(Map<String, Class> propertyMap) {
        this.target = generateBean(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 target;
    }

    /**
     * 根据属性生成对象
     *
     * @param propertyMap
     * @return
     */
    private Object generateBean(Map<String, Class> propertyMap) {
        BeanGenerator generator = new BeanGenerator();
        propertyMap.forEach((k, v) -> generator.addProperty(k, v));
        return generator.create();
    }
}

  

 

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

package com.frinder.util;

import com.kiktech.entity.LoggerEntity;
import net.logstash.logback.encoder.org.apache.commons.lang.builder.ReflectionToStringBuilder;
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 DynamicBean getTarget(Object target, Map<String, Object> addProperties) {
        // get property map
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(target);
        Map<String, Class> propertyMap = new HashMap();
        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(propertyMap);
        // add old value
        propertyMap.forEach((k, v) -> {
            try {
                dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(target, k));
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        });
        // add extra value
        addProperties.forEach((k, v) -> dynamicBean.setValue(k, v));
        return dynamicBean;
    }


    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<String, Object> addProperties = new HashMap() {{
            put("hello", "world");
            put("abc", "123");
        }};
        System.out.println(ReflectionToStringBuilder.toString(getTarget(entity, addProperties)));
    }

}

  

 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“动态”为类添加属性

标签:node   ons   nod   ogg   int   foreach   over   object   create   

原文地址:http://www.cnblogs.com/frinder6/p/7204574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!