protected void setObjectPropertyNull(Object obj, Set<String> fields) { if (null == obj) { return; } for (String field : fields) { try { BeanUtilsBean.getInstance().getPropertyUtils().setProperty(obj, field, null); } catch (Exception ignore) { } } } }
public BeanTypeListener() { // SHIRO-619 beanUtilsBean = new BeanUtilsBean(); beanUtilsBean.getPropertyUtils().addBeanIntrospector( SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS); }
public static <P> P setProperties(P bean, Properties properties) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { synchronized (beanUtils) { PropertyDescriptor[] descriptors = beanUtils.getPropertyUtils().getPropertyDescriptors(bean); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getReadMethod() != null && isWriteable(descriptor, null)) { String value = properties.getProperty(descriptor.getName()); if (value != null) { beanUtils.setProperty(bean, descriptor.getName(), value); } } } } return bean; }
/** * Return the PropertyUtils bean instance. * @return The PropertyUtils bean instance */ protected static PropertyUtilsBean getInstance() { return BeanUtilsBean.getInstance().getPropertyUtils(); }
/** * If we're running on JDK 1.4 or later, initialize the cause for the given throwable. * * @param throwable The throwable. * @param cause The cause of the throwable. * @return true if the cause was initialized, otherwise false. * @since 1.8.0 */ public static boolean initCause(final Throwable throwable, final Throwable cause) { return BeanUtilsBean.getInstance().initCause(throwable, cause); }
/** * Get singleton instance * @return The singleton instance */ protected static ConvertUtilsBean getInstance() { return BeanUtilsBean.getInstance().getConvertUtils(); }
.append(bean.getName()).append(" target=(").append(bean.getName()).append(")bean;\n"); PropertyDescriptor[] descriptors = BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(bean); boolean hasDict = false; for (PropertyDescriptor descriptor : descriptors) { Class type = descriptor.getPropertyType(); boolean isArray = type.isArray(); if (isArray) { .append("\"").append(beanName).append(".").append(descriptor.getName()).append("\"").append(",\"").append(dictId).append("\"") .append(",").append(typeName).append(".class);\n"); wrapMethod.append("target.").append(descriptor.getWriteMethod().getName()).append("(dict);\n"); wrapMethod.append("}");
final Resolver resolver = getPropertyUtils().getResolver(); while (resolver.hasNested(name)) { try { target = getPropertyUtils().getProperty(target, resolver.next(name)); if (target == null) { // the value of a nested property is null return; type = dynaPropertyType(dynaProperty, value); if (index >= 0 && List.class.isAssignableFrom(type)) { type = Object.class; try { descriptor = getPropertyUtils().getPropertyDescriptor(target, name); if (descriptor == null) { return; // Skip this property setter final String[] values = new String[1]; values[0] = null; newValue = getConvertUtils().convert(values, type); } else if (value instanceof String) { newValue = getConvertUtils().convert(value, type); } else if (value instanceof String[]) { newValue = getConvertUtils().convert((String[]) value, type); } else { newValue = convert(value, type); newValue = getConvertUtils().convert((String) value, type.getComponentType());
public static <P> Properties getProperties(P bean, Properties properties) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { synchronized (beanUtils) { PropertyDescriptor[] descriptors = beanUtils.getPropertyUtils().getPropertyDescriptors(bean); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getReadMethod() != null && isWriteable(descriptor, null)) { String value = beanUtils.getProperty(bean, descriptor.getName()); if (value != null) { properties.put(descriptor.getName(), value); } } } } return properties; }
private static void checkEquals(Object factory, Object factory2) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { BeanUtilsBean bean = new BeanUtilsBean(); PropertyDescriptor[] descriptors = bean.getPropertyUtils().getPropertyDescriptors(factory); for (PropertyDescriptor descriptor : descriptors) { if (descriptor.getWriteMethod() != null && descriptor.getReadMethod() != null) { Assert.assertEquals(descriptor.getName() + " incorrect", bean.getProperty(factory, descriptor.getName()), bean.getProperty(factory2, descriptor.getName())); } } }
tableMetaData.setName(table.name()); PropertyDescriptor[] descriptors = BeanUtilsBean.getInstance() .getPropertyUtils() .getPropertyDescriptors(entityClass); for (PropertyDescriptor descriptor : descriptors) { Column columnAnn = getAnnotation(entityClass, descriptor, Column.class); column.setAlias(descriptor.getName()); column.setLength(columnAnn.length()); column.setPrecision(columnAnn.precision()); column.setJavaType(descriptor.getPropertyType()); if (!columnAnn.updatable()) { column.setProperty("read-only", true); column.setColumnDefinition(columnAnn.columnDefinition()); Class propertyType = descriptor.getPropertyType();
((DynaBean) orig).getDynaClass().getDynaProperties(); for (DynaProperty origDescriptor : origDescriptors) { final String name = origDescriptor.getName(); if (getPropertyUtils().isReadable(orig, name) && getPropertyUtils().isWriteable(dest, name)) { final Object value = ((DynaBean) orig).get(name); copyProperty(dest, name, value); for (final Map.Entry<String, Object> entry : propMap.entrySet()) { final String name = entry.getKey(); if (getPropertyUtils().isWriteable(dest, name)) { copyProperty(dest, name, entry.getValue()); getPropertyUtils().getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { final String name = origDescriptor.getName(); if ("class".equals(name)) { continue; // No point in trying to set an object's class if (getPropertyUtils().isReadable(orig, name) && getPropertyUtils().isWriteable(dest, name)) { try { final Object value = getPropertyUtils().getSimpleProperty(orig, name); copyProperty(dest, name, value); } catch (final NoSuchMethodException e) {
if (bean instanceof DynaBean) { final DynaProperty[] descriptors = ((DynaBean) bean).getDynaClass().getDynaProperties(); for (DynaProperty descriptor : descriptors) { final String name = descriptor.getName(); description.put(name, getProperty(bean, name)); getPropertyUtils().getPropertyDescriptors(bean); final Class<?> clazz = bean.getClass(); for (PropertyDescriptor descriptor : descriptors) { final String name = descriptor.getName(); if (getPropertyUtils().getReadMethod(clazz, descriptor) != null) { description.put(name, getProperty(bean, name));
final Resolver resolver = getPropertyUtils().getResolver(); while (resolver.hasNested(name)) { try { target = getPropertyUtils().getProperty(target, resolver.next(name)); name = resolver.remove(name); } catch (final NoSuchMethodException e) { type = dynaPropertyType(dynaProperty, value); } else { PropertyDescriptor descriptor = null; try { descriptor = getPropertyUtils().getPropertyDescriptor(target, name); if (descriptor == null) { return; // Skip this property setter value = convertForCopy(value, type.getComponentType()); try { getPropertyUtils().setIndexedProperty(target, propName, index, value); } catch (final NoSuchMethodException e) { getPropertyUtils().setMappedProperty(target, propName, key, value); } catch (final NoSuchMethodException e) { value = convertForCopy(value, type); try { getPropertyUtils().setSimpleProperty(target, propName, value);
public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) { PropertyDescriptor propertyDescriptors[] = beanUtilsBean.getPropertyUtils().getPropertyDescriptors(type.getRawType()); final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(propertyDescriptors.length); final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getWriteMethod() != null && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) { Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType)); } } encounter.register(new MembersInjector<I>() { public void injectMembers(I instance) { for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) { try { final Injector injector = injectorProvider.get(); Object value = injector.getInstance(getMappedKey(injector, dependency.getValue())); dependency.getKey().getWriteMethod().invoke(instance, value); } catch (ConfigurationException e) { // This is ok, it simply means that we can't fulfill this dependency. // Is there a better way to do this? } catch (InvocationTargetException e) { throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.", e); } } } }); }
protected boolean isTypedProperty(Object object, String propertyName, Class clazz) { if (clazz == null) { throw new NullPointerException("type (class) argument cannot be null."); } try { PropertyDescriptor descriptor = beanUtilsBean.getPropertyUtils().getPropertyDescriptor(object, propertyName); if (descriptor == null) { String msg = "Property '" + propertyName + "' does not exist for object of " + "type " + object.getClass().getName() + "."; throw new ConfigurationException(msg); } Class propertyClazz = descriptor.getPropertyType(); return clazz.isAssignableFrom(propertyClazz); } catch (ConfigurationException ce) { //let it propagate: throw ce; } catch (Exception e) { String msg = "Unable to determine if property [" + propertyName + "] represents a " + clazz.getName(); throw new ConfigurationException(msg, e); } }
/** * Copy value to property. New instance of value will be created if * propertyType is {@link java.util.List}, {@link java.util.Set} or * {@link java.util.Map}. * * Note: ArrayList, HashSet and HashMap are being used to create new * instance of Collection, which will potential be an issue if propertyType * is not one of those implementation but sharing the same interface. */ private static void copyProperty(BeanUtilsBean beanUtilsBean, Object toBean, String property, Object value) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { Class<?> propertyType = beanUtilsBean.getPropertyUtils() .getPropertyDescriptor(toBean, property).getPropertyType(); if (isCollectionType(propertyType)) { value = createNewCollection(propertyType, value); } beanUtilsBean.copyProperty(toBean, property, value); }
newBean = ((DynaBean) bean).getDynaClass().newInstance(); } else { newBean = bean.getClass().newInstance(); getPropertyUtils().copyProperties(newBean, bean); return (newBean);
/** * Return the value of the specified indexed property of the specified * bean, as a String. The index is specified as a method parameter and * must *not* be included in the property name expression * * @param bean Bean whose property is to be extracted * @param name Simple property name of the property value to be extracted * @param index Index of the property value to be extracted * @return The indexed property's value, converted to a String * * @throws IllegalAccessException if the caller does not have * access to the property accessor method * @throws InvocationTargetException if the property accessor method * throws an exception * @throws NoSuchMethodException if an accessor method for this * property cannot be found */ public String getIndexedProperty(final Object bean, final String name, final int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Object value = getPropertyUtils().getIndexedProperty(bean, name, index); return (getConvertUtils().convert(value)); }
/** * Return the value of the specified mapped property of the specified * bean, as a String. The key is specified as a method parameter and * must *not* be included in the property name expression * * @param bean Bean whose property is to be extracted * @param name Simple property name of the property value to be extracted * @param key Lookup key of the property value to be extracted * @return The mapped property's value, converted to a String * * @throws IllegalAccessException if the caller does not have * access to the property accessor method * @throws InvocationTargetException if the property accessor method * throws an exception * @throws NoSuchMethodException if an accessor method for this * property cannot be found */ public String getMappedProperty(final Object bean, final String name, final String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Object value = getPropertyUtils().getMappedProperty(bean, name, key); return (getConvertUtils().convert(value)); }