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);
}
}
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);
}
}
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);
}
}
}
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();
}
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;
}
Aggregations