Java源码示例:org.apache.commons.beanutils.DynaClass
示例1
/**
* {@inheritDoc}
*/
public String toString()
{
StringBuilder result = new StringBuilder();
DynaClass type = getDynaClass();
DynaProperty[] props = type.getDynaProperties();
result.append(type.getName());
result.append(": ");
for (int idx = 0; idx < props.length; idx++)
{
if (idx > 0)
{
result.append(", ");
}
result.append(props[idx].getName());
result.append(" = ");
result.append(get(props[idx].getName()));
}
return result.toString();
}
示例2
/**
* {@inheritDoc}
*/
public String toString()
{
StringBuilder result = new StringBuilder();
DynaClass type = getDynaClass();
DynaProperty[] props = type.getDynaProperties();
result.append(type.getName());
result.append(": ");
for (int idx = 0; idx < props.length; idx++)
{
if (idx > 0)
{
result.append(", ");
}
result.append(props[idx].getName());
result.append(" = ");
result.append(get(props[idx].getName()));
}
return result.toString();
}
示例3
/**
* build a map of business object with its specified property names and corresponding values
*
* @param businessObject the given business object
* @param the specified fields that need to be included in the return map
* @return the map of business object with its property names and values
*/
public static Map<String, Object> buildPropertyMap(Object object, List<String> keyFields) {
DynaClass dynaClass = WrapDynaClass.createDynaClass(object.getClass());
DynaProperty[] properties = dynaClass.getDynaProperties();
Map<String, Object> propertyMap = new LinkedHashMap<String, Object>();
for (DynaProperty property : properties) {
String propertyName = property.getName();
if (PropertyUtils.isReadable(object, propertyName) && keyFields.contains(propertyName)) {
try {
Object propertyValue = PropertyUtils.getProperty(object, propertyName);
if (propertyValue != null && !StringUtils.isEmpty(propertyValue.toString())) {
propertyMap.put(propertyName, propertyValue);
}
}
catch (Exception e) {
LOG.info(e);
}
}
}
return propertyMap;
}
示例4
/**
* determine if the source object has a field with null as its value
*
* @param sourceObject the source object
*/
public static boolean hasNullValueField(Object sourceObject) {
DynaClass dynaClass = WrapDynaClass.createDynaClass(sourceObject.getClass());
DynaProperty[] properties = dynaClass.getDynaProperties();
for (DynaProperty property : properties) {
String propertyName = property.getName();
if (PropertyUtils.isReadable(sourceObject, propertyName)) {
try {
Object propertyValue = PropertyUtils.getProperty(sourceObject, propertyName);
if (propertyValue == null) {
return true;
}
}
catch (Exception e) {
LOG.info(e);
return false;
}
}
}
return false;
}
示例5
/**
* This method builds a map of business object with its property names and values
*
* @param businessObject the given business object
* @return the map of business object with its property names and values
*/
public static LinkedHashMap buildPropertyMap(Object businessObject) {
DynaClass dynaClass = WrapDynaClass.createDynaClass(businessObject.getClass());
DynaProperty[] properties = dynaClass.getDynaProperties();
LinkedHashMap propertyMap = new LinkedHashMap();
try {
for (int numOfProperty = 0; numOfProperty < properties.length; numOfProperty++) {
String propertyName = properties[numOfProperty].getName();
if (PropertyUtils.isWriteable(businessObject, propertyName)) {
Object propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
propertyMap.put(propertyName, propertyValue);
}
}
}
catch (Exception e) {
LOG.error("OJBUtility.buildPropertyMap()" + e);
}
return propertyMap;
}
示例6
/**
* Creates a new instance of {@code MultiWrapDynaBean} and initializes it
* with the given collections of beans to be wrapped.
*
* @param beans the wrapped beans
*/
public MultiWrapDynaBean(final Collection<?> beans)
{
propsToBeans = new HashMap<>();
final Collection<DynaClass> beanClasses =
new ArrayList<>(beans.size());
for (final Object bean : beans)
{
final DynaBean dynaBean = createDynaBean(bean);
final DynaClass beanClass = dynaBean.getDynaClass();
for (final DynaProperty prop : beanClass.getDynaProperties())
{
// ensure an order of properties
if (!propsToBeans.containsKey(prop.getName()))
{
propsToBeans.put(prop.getName(), dynaBean);
}
}
beanClasses.add(beanClass);
}
dynaClass = new MultiWrapDynaClass(beanClasses);
}
示例7
public String[] getPropertyNames() {
/* @todo do something about the sorting - LIKE WHAT? - MJB */
if (names == null) {
DynaClass dynaClass = dynaBean.getDynaClass();
DynaProperty[] dynaProperties = dynaClass.getDynaProperties();
ArrayList properties = new ArrayList(dynaProperties.length);
for (int i = 0; i < dynaProperties.length; i++) {
String name = dynaProperties[i].getName();
if (!CLASS.equals(name)) {
properties.add(name);
}
}
names = (String[]) properties.toArray(new String[properties.size()]);
Arrays.sort(names);
}
return names;
}
示例8
/**
* Convert a value to the appropriate property type.
* @param value to convert
* @param element whether this should be a collection element.
* @return conversion result
*/
private Object convert(Object value, boolean element) {
DynaClass dynaClass = dynaBean.getDynaClass();
DynaProperty property = dynaClass.getDynaProperty(getPropertyName());
Class type = property.getType();
if (element) {
if (type.isArray()) {
type = type.getComponentType();
}
else {
return value; // No need to convert
}
}
try {
return TypeUtils.convert(value, type);
}
catch (Exception ex) {
String string = value == null ? "null" : value.getClass().getName();
throw new JXPathTypeConversionException(
"Cannot convert value of class " + string + " to type "
+ type, ex);
}
}
示例9
/**
* Returns the {@link SqlDynaClass} for the given bean.
*
* @param dynaBean The bean
* @return The dyna bean class
*/
public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException
{
DynaClass dynaClass = dynaBean.getDynaClass();
if (dynaClass instanceof SqlDynaClass)
{
return (SqlDynaClass)dynaClass;
}
else
{
// TODO: we could autogenerate an SqlDynaClass here ?
throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass");
}
}
示例10
/**
* {@inheritDoc}
*/
public boolean equals(Object obj)
{
if (obj instanceof SqlDynaBean)
{
SqlDynaBean other = (SqlDynaBean)obj;
DynaClass dynaClass = getDynaClass();
if (dynaClass.equals(other.getDynaClass()))
{
DynaProperty[] props = dynaClass.getDynaProperties();
for (int idx = 0; idx < props.length; idx++)
{
Object value = get(props[idx].getName());
Object otherValue = other.get(props[idx].getName());
if (value == null)
{
if (otherValue != null)
{
return false;
}
}
else
{
return value.equals(otherValue);
}
}
return true;
}
}
return false;
}
示例11
/**
* Returns the {@link SqlDynaClass} for the given bean.
*
* @param dynaBean The bean
* @return The dyna bean class
*/
public SqlDynaClass getDynaClass(DynaBean dynaBean) throws SqlDynaException
{
DynaClass dynaClass = dynaBean.getDynaClass();
if (dynaClass instanceof SqlDynaClass)
{
return (SqlDynaClass)dynaClass;
}
else
{
// TODO: we could autogenerate an SqlDynaClass here ?
throw new SqlDynaException("The dyna bean is not an instance of a SqlDynaClass");
}
}
示例12
/**
* {@inheritDoc}
*/
public boolean equals(Object obj)
{
if (obj instanceof SqlDynaBean)
{
SqlDynaBean other = (SqlDynaBean)obj;
DynaClass dynaClass = getDynaClass();
if (dynaClass.equals(other.getDynaClass()))
{
DynaProperty[] props = dynaClass.getDynaProperties();
for (int idx = 0; idx < props.length; idx++)
{
Object value = get(props[idx].getName());
Object otherValue = other.get(props[idx].getName());
if (value == null)
{
if (otherValue != null)
{
return false;
}
}
else
{
return value.equals(otherValue);
}
}
return true;
}
}
return false;
}
示例13
public Class<?> getFieldType(Object obj, String name) {
DynaClass dynaClass = ((DynaBean) obj).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(name);
if (dynaProperty == null) {
throw new FieldnameNotFoundException("DynaBean: Could not find this fieldName ("+name+") on the target object: " + obj, name, null);
}
return dynaProperty.getType();
}
示例14
/**
* Populate the target object with the source object
*
* @param targetObject the target object
* @param sourceObject the source object
*/
public static void buildObject(Object targetObject, Object sourceObject) {
DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
DynaProperty[] properties = dynaClass.getDynaProperties();
for (DynaProperty property : properties) {
ObjectUtil.setProperty(targetObject, sourceObject, property, false);
}
}
示例15
/**
* Populate the target object with the source object
*
* @param targetObject the target object
* @param sourceObject the source object
*/
public static void buildObjectWithoutReferenceFields(Object targetObject, Object sourceObject) {
DynaClass dynaClass = WrapDynaClass.createDynaClass(targetObject.getClass());
DynaProperty[] properties = dynaClass.getDynaProperties();
for (DynaProperty property : properties) {
ObjectUtil.setProperty(targetObject, sourceObject, property, true);
}
}
示例16
/**
* Creates a new instance of {@code MultiWrapDynaClass} and initializes it
* with the collection of classes to be wrapped.
*
* @param wrappedCls the collection with wrapped classes
*/
public MultiWrapDynaClass(final Collection<? extends DynaClass> wrappedCls)
{
properties = new LinkedList<>();
namedProperties = new HashMap<>();
initProperties(wrappedCls);
}
示例17
/**
* Initializes the members related to the properties of the wrapped classes.
*
* @param wrappedCls the collection with the wrapped classes
*/
private void initProperties(final Collection<? extends DynaClass> wrappedCls)
{
for (final DynaClass cls : wrappedCls)
{
final DynaProperty[] props = cls.getDynaProperties();
for (final DynaProperty p : props)
{
properties.add(p);
namedProperties.put(p.getName(), p);
}
}
}
示例18
/**
* Tests whether the class of bean can be queried.
*/
@Test
public void testGetDynaClass()
{
final DynaClass cls = createBean(false).getDynaClass();
assertNotNull("Property not found (1)",
cls.getDynaProperty("throwExceptionOnMissing"));
assertNotNull("Property not found (2)", cls.getDynaProperty("text"));
}
示例19
public FilterableListContainer(DynaClass type, String... properties) {
super(type, properties);
}
示例20
public FilterableListContainer(DynaClass type) {
super(type);
}
示例21
private ListContainer<T> createContainer(DynaClass type) {
return new ListContainer<>(type);
}
示例22
@Override
public DynaClass getDynaClass()
{
return new ConfigurationDynaClass(getConfiguration());
}
示例23
/**
* {@inheritDoc} This implementation returns an instance of
* {@code MultiWrapDynaClass}.
*/
@Override
public DynaClass getDynaClass()
{
return dynaClass;
}
示例24
/**
* Returns true if the bean has the currently selected property.
* @return boolean
*/
protected boolean isActualProperty() {
DynaClass dynaClass = dynaBean.getDynaClass();
return dynaClass.getDynaProperty(getPropertyName()) != null;
}
示例25
/**
* Learn whether the property referenced is an indexed property.
* @return boolean
*/
protected boolean isIndexedProperty() {
DynaClass dynaClass = dynaBean.getDynaClass();
DynaProperty property = dynaClass.getDynaProperty(name);
return property.isIndexed();
}
示例26
/**
* Creates a new dyna bean of the given class.
*
* @param dynaClass The dyna class
*/
public SqlDynaBean(DynaClass dynaClass)
{
super(dynaClass);
}
示例27
/**
* Creates a new dyna bean of the given class.
*
* @param dynaClass The dyna class
*/
public SqlDynaBean(DynaClass dynaClass)
{
super(dynaClass);
}
示例28
/**
* Constructs a Table with explicit bean type. Handy for example if your
* beans are JPA proxies or the table is empty when showing it initially.
*
* @param type the type of beans that are listed in this table
*/
public MTable(DynaClass type) {
bic = createContainer(type);
setContainerDataSource(bic);
}