本文整理匯總了Java中org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors方法的典型用法代碼示例。如果您正苦於以下問題:Java PropertyUtils.getPropertyDescriptors方法的具體用法?Java PropertyUtils.getPropertyDescriptors怎麽用?Java PropertyUtils.getPropertyDescriptors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.beanutils.PropertyUtils
的用法示例。
在下文中一共展示了PropertyUtils.getPropertyDescriptors方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildClass
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
public void buildClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String clazz=req.getParameter("clazz");
List<Field> result=new ArrayList<Field>();
try{
Class<?> targetClass=Class.forName(clazz);
PropertyDescriptor[] propertyDescriptors=PropertyUtils.getPropertyDescriptors(targetClass);
for(PropertyDescriptor pd:propertyDescriptors){
String name=pd.getName();
if("class".equals(name)){
continue;
}
result.add(new Field(name));
}
writeObjectToJson(resp, result);
}catch(Exception ex){
throw new ReportDesignException(ex);
}
}
示例2: copyBeanNotNull2Bean
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* 對象拷貝 數據對象空值不拷貝到目標對象
*
* @param databean 待拷貝對象
* @param tobean 目標對象
* @throws NoSuchMethodException
*/
public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception {
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if (value != null) {
getInstance().setSimpleProperty(tobean, name, value);
}
} catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
} catch (Exception e) {
; // Should not happen
}
}
}
}
示例3: copyTo
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
public void copyTo(T aObject) {
ModelProperties theProperties = aObject.getProperties();
try {
for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
Object theValue = PropertyUtils.getProperty(this, theDescriptor.getName());
if (theValue != null) {
theProperties.setProperty(theDescriptor.getName(), theValue.toString());
} else {
theProperties.setProperty(theDescriptor.getName(), null);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例4: updateConfigFromBean
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
private static void updateConfigFromBean(APropertyChangeSupport bean, String... forceAddThisProperties) {
List<String> addThoseL = Arrays.asList(forceAddThisProperties);
logger.debug("updating config from bean: "+bean.getClass().getName()+ ", adding those props: "+CoreUtils.toListString(addThoseL));
for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(bean)) {
Object value = null;
try {
// logger.debug("property name: "+pd.getName());
value = PropertyUtils.getProperty(bean, pd.getName());
String strVal = getValue(value);
if (config.getProperty(pd.getName())!=null) {
logger.trace("updating property "+pd.getName()+" to "+strVal);
config.setProperty(pd.getName(), strVal);
} else if (addThoseL.contains(pd.getName())) {
logger.debug("adding new property "+pd.getName()+" to "+strVal);
config.addProperty(pd.getName(), strVal);
}
} catch (Exception e) {
logger.warn("Could not set property "+pd.getName()+" to "+value+", error: "+e.getMessage());
}
}
}
示例5: generateBeanCode
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
public String generateBeanCode(Object bean) {
if (processedBeans.contains(bean)) {
return getVariableName(bean);
}
processedBeans.add(bean);
String varName = generateVariableName(bean);
method.addStatement("$T " + varName + " = new $T()", bean.getClass(), bean.getClass());
PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(bean.getClass());
for (PropertyDescriptor propertyDescriptor : properties) {
try {
generatePropertySetter(bean, varName, propertyDescriptor);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return varName;
}
示例6: copyParentProperties
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private void copyParentProperties(final CustomField parent, final CustomField child) {
final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(parent);
for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
final String name = propertyDescriptor.getName();
final boolean isWritable = propertyDescriptor.getWriteMethod() != null;
final boolean isReadable = propertyDescriptor.getReadMethod() != null;
if (isReadable && isWritable && !EXCLUDED_PROPERTIES_FOR_DEPENDENT_FIELDS.contains(name)) {
Object value = PropertyHelper.get(parent, name);
if (value instanceof Collection) {
value = new ArrayList<Object>((Collection<Object>) value);
}
PropertyHelper.set(child, name, value);
}
}
}
示例7: copyProperties
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* Copies all possible properties from source to dest, ignoring the given properties list. Exceptions are ignored
*/
public static void copyProperties(final Object source, final Object dest, final String... ignored) {
if (source == null || dest == null) {
return;
}
final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(source);
for (final PropertyDescriptor sourceDescriptor : propertyDescriptors) {
try {
final String name = sourceDescriptor.getName();
// Check for ignored properties
if (ArrayUtils.contains(ignored, name)) {
continue;
}
final PropertyDescriptor destProperty = PropertyUtils.getPropertyDescriptor(dest, name);
if (destProperty.getWriteMethod() == null) {
// Ignore read-only properties
continue;
}
final Object value = CoercionHelper.coerce(destProperty.getPropertyType(), get(source, name));
set(dest, name, value);
} catch (final Exception e) {
// Ignore this property
}
}
}
示例8: propertyDescriptorsFor
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* Returns a Map with basic properties for the given entity
*/
public static Map<String, PropertyDescriptor> propertyDescriptorsFor(final Entity entity) {
final Class<? extends Entity> clazz = getRealClass(entity);
SortedMap<String, PropertyDescriptor> properties = cachedPropertiesByClass.get(clazz);
if (properties == null) {
properties = new TreeMap<String, PropertyDescriptor>();
final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(clazz);
for (final PropertyDescriptor descriptor : propertyDescriptors) {
final String name = descriptor.getName();
boolean ok = name.equals("id");
if (!ok) {
final Method readMethod = descriptor.getReadMethod();
if (readMethod != null) {
final Class<?> declaringClass = readMethod.getDeclaringClass();
ok = !declaringClass.equals(Entity.class) && !declaringClass.equals(CustomFieldsContainer.class);
}
}
if (ok) {
properties.put(name, descriptor);
}
}
properties = Collections.unmodifiableSortedMap(properties);
cachedPropertiesByClass.put(clazz, properties);
}
return properties;
}
示例9: mergeObjects
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* Deep merge to POJO objects
*
* @param target The target POJO
* @param source The source POJO
* @param <T> The POJO type
* @return The merge result
*/
public static <T> T mergeObjects(T target, T source) {
// Check that target and source are of the same type
Preconditions.checkArgument(
target.getClass().equals(source.getClass()),
"Type mismatch %s -> %s",
source.getClass().getSimpleName(),
target.getClass().getSimpleName()
);
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(target);
EStream.from(descriptors).forEach(propertyDescriptor -> {
Object targetValue = PropertyUtils.getProperty(target, propertyDescriptor.getName());
Object sourceValue = PropertyUtils.getProperty(source, propertyDescriptor.getName());
if (sourceValue != null) {
if (targetValue == null) {
PropertyUtils.setProperty(target, propertyDescriptor.getName(), sourceValue);
} else {
if (isJavaType(propertyDescriptor)) {
if (targetValue != sourceValue) {
PropertyUtils.setProperty(target, propertyDescriptor.getName(), sourceValue);
}
} else {
mergeObjects(targetValue, sourceValue);
}
}
}
});
return target;
}
示例10: initializeObjectGraph
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* Recursively initialize all non-primitive properties of an object
*
* @param o The object to initialize
*/
public static void initializeObjectGraph(Object o) {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(o);
EStream.from(descriptors)
.filter(propertyDescriptor -> !isJavaType(propertyDescriptor))
.filter(propertyDescriptor -> PropertyUtils.getProperty(o, propertyDescriptor.getName()) == null)
.forEach(propertyDescriptor -> {
Class<?> type = propertyDescriptor.getPropertyType();
Object subO = type.getDeclaredConstructor().newInstance();
PropertyUtils.setProperty(o, propertyDescriptor.getName(), subO);
initializeObjectGraph(subO);
});
}
示例11: initializeBean
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
protected void initializeBean(Object bean) {
for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(bean)) {
Method writeMethod = pd.getWriteMethod();
if (writeMethod == null) {
continue;
}
injectBean(bean, pd);
injectValue(bean, pd);
}
}
示例12: getConectorPropertyNames
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
public static List /*String*/ getConectorPropertyNames(Conector conector) {
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(conector);
List list = new ArrayList(descriptors.length);
for (int i = 0; i < descriptors.length; i++) {
PropertyDescriptor descriptor = descriptors[i];
if (descriptor.getWriteMethod() != null) {
list.add(descriptor.getName());
}
}
return list;
}
示例13: getProperties
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
private static Set<String> getProperties(Class<?> clss) {
final Set<String> result = new TreeSet<>();
final PropertyDescriptor[] map = PropertyUtils.getPropertyDescriptors(clss);
for (PropertyDescriptor p : map) {
if (p.getWriteMethod() != null) {
result.add(p.getName());
}
}
return result;
}
示例14: copyPropertiesFromTo
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
private void copyPropertiesFromTo(User from, User to) throws Exception {
for (PropertyDescriptor property : PropertyUtils.getPropertyDescriptors(User.class)) {
if (NO_UPDATE_PROPERTIES.contains(property.getName())) {
log.debug("Copying property {}", property.getName());
Method read = property.getReadMethod();
Method write = property.getWriteMethod();
if (read.invoke(from) != null) {
write.invoke(to, read.invoke(from));
}
}
}
}
示例15: getFilters
import org.apache.commons.beanutils.PropertyUtils; //導入方法依賴的package包/類
/**
* 獲取篩選
*
* @param params
* 參數
* @param type
* 參數類型
* @param ignoreProperties
* 忽略屬性
* @return 篩選
*/
protected List<Filter> getFilters(Map<String, TemplateModel> params, Class<?> type, String... ignoreProperties) throws TemplateModelException {
List<Filter> filters = new ArrayList<Filter>();
PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type);
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String propertyName = propertyDescriptor.getName();
Class<?> propertyType = propertyDescriptor.getPropertyType();
if (!ArrayUtils.contains(ignoreProperties, propertyName) && params.containsKey(propertyName)) {
Object value = FreemarkerUtils.getParameter(propertyName, propertyType, params);
filters.add(Filter.eq(propertyName, value));
}
}
return filters;
}