Search in sources :

Example 61 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project summerb by skarpushin.

the class ObjCopyUtils method assignFields.

/**
 * Get property values from source object (using getters) and assign to
 * destination (using setters)
 *
 * @param src
 *            source object
 * @param dst
 *            target object. source class expected to be assignable from
 *            destination
 */
public static <TSrc, TDst extends TSrc> void assignFields(TSrc src, TDst dst) {
    Preconditions.checkArgument(src != null, "Src must not be null");
    Preconditions.checkArgument(dst != null, "Dst must not be null");
    Preconditions.checkArgument(src.getClass().isAssignableFrom(dst.getClass()), "source class expected to be assignable from destination");
    try {
        PropertyDescriptor[] srcProps = BeanUtils.getPropertyDescriptors(src.getClass());
        for (PropertyDescriptor pd : srcProps) {
            Method writeMethod = pd.getWriteMethod();
            if (writeMethod == null) {
                continue;
            }
            Object value = pd.getReadMethod().invoke(src);
            writeMethod.invoke(dst, value);
        }
    } catch (Throwable t) {
        throw new RuntimeException("Failed to copy properties from " + src + " to " + dst, t);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 62 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project summerb by skarpushin.

the class EasyCrudDomUtils method buildReferencedObjectsList.

/**
 * Finds all referenced objects and creates a list of them
 *
 * @param dataSet
 *            data set where source and all possible targets are located
 * @param src
 *            id of the referencer
 * @param ref
 *            description of the reference
 * @param rowDtoClass
 *            DTO class, it's used to avoid cimpiler confusing between
 *            TRowDto and TRetDto class, since latter one might be a
 *            subclass of former one
 * @param builder
 *            function that can take id of an referencee and build new
 *            instance. This will be added to the returned list
 * @return list of referenced objects or empty list if none found. Changes
 *         to a list will not affect the database
 *
 *         TODO: THINK: Why not add this functionality?
 */
public static <TSrcId, TSrcDto extends HasId<TSrcId>, TId, TRowDto extends HasId<TId>, TRetDto extends TRowDto> List<TRetDto> buildReferencedObjectsList(DataSet dataSet, TSrcDto src, Ref ref, Class<TRowDto> rowDtoClass, Function<TRowDto, TRetDto> builder) {
    try {
        Preconditions.checkArgument(ref.getQuantity() != RefQuantity.Many2Many, "ManyToMany is not supported (yet) by this method");
        Object srcValue = findSrcFieldValue(src, ref.getFromField());
        @SuppressWarnings("unchecked") DataTable<TId, TRowDto> target = dataSet.get(ref.getToEntity());
        if (target.getRows().size() == 0) {
            return Collections.emptyList();
        }
        TRowDto targetExample = target.getRows().values().iterator().next();
        PropertyDescriptor targetField = BeanUtils.getPropertyDescriptor(targetExample.getClass(), ref.getToField());
        List<TRetDto> ret = new ArrayList<>();
        for (TRowDto row : target.getRows().values()) {
            Object targetValue = targetField.getReadMethod().invoke(row);
            if (ObjectUtils.nullSafeEquals(srcValue, targetValue)) {
                TRetDto rowToAdd = builder.apply(row);
                ret.add(rowToAdd);
            }
        }
        return ret;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to build ist of referenced (" + ref.getName() + ") objects for " + src, t);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList)

Example 63 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class Main method addPropertyOptions.

private static void addPropertyOptions(Options options, Bean<Configuration> conf) {
    for (PropertyDescriptor property : conf.getProperties()) {
        if (property.getWriteMethod().getAnnotation(Adjustable.class).editable()) {
            String name = property.getName();
            String type = property.getPropertyType().getSimpleName();
            String info = property.getWriteMethod().getAnnotation(Adjustable.class).info();
            Option option = OptionBuilder.withArgName(type).hasArg().withDescription(info).withValueSeparator('=').withLongOpt(name).create();
            options.addOption(option);
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Option(org.apache.commons.cli.Option) Adjustable(net.sf.sdedit.ui.components.configuration.Adjustable)

Example 64 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class Bean method hashCode.

/**
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    StringBuffer code = new StringBuffer();
    for (PropertyDescriptor property : getProperties()) {
        Object val = getValue(property.getName());
        code.append(val);
    }
    return code.hashCode();
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor)

Example 65 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project abstools by abstools.

the class Bean method equals.

/**
 * Returns true if and only if <tt>o</tt> is a reference to a bean with
 * the same properties that have the same values as this bean's properties.
 *
 * @return true if and only if <tt>o</tt> is a reference to a bean with
 *         the same properties that have the same values as this bean's
 *         properties
 */
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
    Bean<? extends DataObject> bean = (Bean<? extends DataObject>) o;
    for (PropertyDescriptor property : getProperties()) {
        Object myVal = getValue(property.getName());
        Object yourVal = bean.getValue(property.getName());
        if (myVal == null && yourVal == null) {
            // check next property if both are null
            continue;
        }
        if (myVal == null || yourVal == null) {
            // if one is null, the other is not, so return false
            return false;
        }
        if (!myVal.equals(yourVal)) {
            return false;
        }
    }
    return true;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)836 Method (java.lang.reflect.Method)396 BeanInfo (java.beans.BeanInfo)339 IntrospectionException (java.beans.IntrospectionException)188 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)108 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)59 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)34 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 HashSet (java.util.HashSet)21 Type (java.lang.reflect.Type)20 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19