gpt4 book ai didi

java - Apache Commons BeanUtilsBean - 从 describe() 中排除属性

转载 作者:搜寻专家 更新时间:2023-11-01 02:28:33 26 4
gpt4 key购买 nike

我正在使用 BeanutilsBean.describe() 方法来获取数据以进行审计跟踪。它工作得很好 - 这不是问题!

然而,有些属性不需要审核。这些被记录为列表中的字符串。因此,例如,如果您有一个属性 DomainObject.myValue,则该列表将包含 "myValue",以便调用 DomainObject.getMyValue()< 的结果 不包含在审计跟踪中。

目前,代码从 BeanutilsBean.describe() 获取所有属性,然后遍历它们以丢弃不需要的属性。

我想要做的是使用要排除的属性名称列表配置 BeanUtilsBean 实例,这样它就永远不会调用这些方法。因此,在我的示例中,根本不会调用 DomainObject.getMyValue()。

通过查看 API 或代码,我无法确定这是否可行。

最佳答案

这是我用来解决这个问题的代码。

它是 BeanUtilsBean.describe() 的一个略微修改的副本,它不调用排除的属性 getter;这是 ach's answer 中的“自己动手”选项(第一个选项已经在实时代码中使用了几年,但它从未适合我!)。

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.MethodUtils;

public class BeanUtilsBeanExtensions {

private static final BeanUtilsBean BEAN_UTILS_BEAN = BeanUtilsBean
.getInstance();

public BeanUtilsBeanExtensions() {
}

/**
* Extends BeanUtilsBean.describe() so that it can be given a list of
* attributes to exclude. This avoids calling methods which might derive
* data which don't happen to be populated when the describe() call is made
* (and therefore could throw exceptions) as well as being more efficient
* than describing everything then discarding attributes which aren't
* required.
*
* @param bean
* See BeanUtilsBean.describe()
* @param excludedAttributeNames
* the attribute names which should not be described.
* @return See BeanUtilsBean.describe()
*/
public Map<String, String> describe(Object bean,
Set<String> excludedAttributeNames)
throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {

// This method is mostly just a copy/paste from BeanUtilsBean.describe()
// The only changes are:
// - Removal of reference to the (private) logger
// - Addition of Reference to a BeanUtilsBean instance
// - Addition of calls to excludedAttributeNames.contains(name)
// - Use of generics on the Collections
// - Calling of a copy of PropertyUtilsBean.getReadMethod()

if (bean == null) {
return (new java.util.HashMap<String, String>());
}

Map<String, String> description = new HashMap<String, String>();
if (bean instanceof DynaBean) {
DynaProperty[] descriptors = ((DynaBean) bean).getDynaClass()
.getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (!excludedAttributeNames.contains(name)) {
description.put(name,
BEAN_UTILS_BEAN.getProperty(bean, name));
}
}
}
else {
PropertyDescriptor[] descriptors = BEAN_UTILS_BEAN
.getPropertyUtils().getPropertyDescriptors(bean);
Class<? extends Object> clazz = bean.getClass();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (!excludedAttributeNames.contains(name)
&& getReadMethod(clazz, descriptors[i]) != null) {
description.put(name,
BEAN_UTILS_BEAN.getProperty(bean, name));
}
}
}
return description;
}

/*
* Copy of PropertyUtilsBean.getReadMethod() since that is package-private.
*/
private Method getReadMethod(Class<? extends Object> clazz,
PropertyDescriptor descriptor) {
return MethodUtils.getAccessibleMethod(clazz,
descriptor.getReadMethod());
}

}

关于java - Apache Commons BeanUtilsBean - 从 describe() 中排除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com