JAVA“动态”为类添加属性
Posted 每天一点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA“动态”为类添加属性相关的知识,希望对你有一定的参考价值。
部分参考: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动态生成类与使用 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<String, Object> addProperties) { // get property map PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest); Map<String, Class> 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<String, Object> 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<String, Class> 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<String, Class> 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“动态”为类添加属性的主要内容,如果未能解决你的问题,请参考以下文章
如果使用实例化对象,来为类动态的添加一个 字符串形式的 方法。
如何使用 Roslyn 为类中具有特定返回类型的所有属性添加 JsonIgnore 属性?
使用 WindowManager.addView 添加动态视图
没有为类 java.util.logging.ErrorManager 找到序列化程序,也没有找到用于创建 BeanSerializer 的属性