@Override public Object value(Object root, String name) { return PropertyUtils.getProperty(root, name); } }
/** * Returns object property using JavaBean-compatible introspection with one * addition - a property can be a dot-separated property name path. */ public static Object getProperty(Object object, String nestedPropertyName) throws CayenneRuntimeException { return accessor(nestedPropertyName).getValue(object); }
protected void setIndexValue(Object object, int index) { PropertyUtils.setProperty(object, indexProperty, Integer.valueOf(index)); }
@Override public Object getValue(Object object) throws PropertyException { if (object == null) { return null; } Object value = getOrCreateSegmentAccessor(object.getClass(), segmentName).getValue(object); return nextAccessor != null ? nextAccessor.getValue(value) : value; }
public FieldAccessor(Class<?> objectClass, String propertyName, Class<?> propertyType) { // sanity check if (objectClass == null) { throw new IllegalArgumentException("Null objectClass"); } if (propertyName == null) { throw new IllegalArgumentException("Null propertyName"); } this.propertyName = propertyName; this.field = prepareField(objectClass, propertyName, propertyType); this.nullValue = PropertyUtils.defaultNullValueForType(field.getType()); }
/** * Compiles an accessor that can be used for fast access for the nested * property of the objects of a given class. * * @since 4.0 */ public static Accessor accessor(String nestedPropertyName) { if (nestedPropertyName == null) { throw new IllegalArgumentException("Null property name."); } if (nestedPropertyName.length() == 0) { throw new IllegalArgumentException("Empty property name."); } // PathAccessor is simply a chain of path segment wrappers. The actual // accessor is resolved (with caching) during evaluation. Otherwise we // won't be able to handle subclasses of declared property types... // TODO: perhaps Java 7 MethodHandles are the answer to truly "compiled" // path accessor? return compilePathAccessor(nestedPropertyName); }
static Accessor getOrCreateSegmentAccessor(Class<?> objectClass, String propertyName) { ConcurrentMap<String, Accessor> forType = SEGMENT_ACCESSORS.get(objectClass); if (forType == null) { ConcurrentMap<String, Accessor> newPropAccessors = new ConcurrentHashMap<String, Accessor>(); ConcurrentMap<String, Accessor> existingPropAccessors = SEGMENT_ACCESSORS.putIfAbsent(objectClass, newPropAccessors); forType = existingPropAccessors != null ? existingPropAccessors : newPropAccessors; } Accessor a = forType.get(propertyName); if (a == null) { Accessor newA = createSegmentAccessor(objectClass, propertyName); Accessor existingA = forType.putIfAbsent(propertyName, newA); a = existingA != null ? existingA : newA; } return a; }
if (!PropertyUtils.normalizeType(field.getType()).isAssignableFrom( PropertyUtils.normalizeType(propertyType))) { throw new CayenneRuntimeException("Expected property type '%s', got '%s'. Property: '%s.%s'." , propertyType.getName(), field.getType().getName(), beanClass.getName(), propertyName);
@Override public void setValue(Object object, Object newValue) throws PropertyException { if (object == null) { return; } Accessor segmentAccessor = getOrCreateSegmentAccessor(object.getClass(), segmentName); if (nextAccessor != null) { nextAccessor.setValue(segmentAccessor.getValue(object), newValue); } else { segmentAccessor.setValue(object, newValue); } } }
this.nullValue = PropertyUtils.defaultNullValueForType(propertyType);
static Accessor compilePathAccessor(String path) { Accessor accessor = PATH_ACCESSORS.get(path); if (accessor == null) { int dot = path.indexOf(Entity.PATH_SEPARATOR); if (dot == 0 || dot == path.length() - 1) { throw new IllegalArgumentException("Invalid path: " + path); } String segment = dot < 0 ? path : path.substring(0, dot); Accessor remainingAccessor = dot < 0 ? null : compilePathAccessor(path.substring(dot + 1)); Accessor newAccessor = new PathAccessor(segment, remainingAccessor); Accessor existingAccessor = PATH_ACCESSORS.putIfAbsent(path, newAccessor); accessor = existingAccessor != null ? existingAccessor : newAccessor; } return accessor; }
public int compare(Object o1, Object o2) { if ((o1 == null && o2 == null) || o1 == o2) { return 0; } else if (o1 == null) { return -1; } else if (o2 == null) { return 1; } Comparable p1 = (Comparable) PropertyUtils.getProperty( o1, indexProperty); Comparable p2 = (Comparable) PropertyUtils.getProperty( o2, indexProperty); return (p1 == null) ? -1 : p1.compareTo(p2); } };
/** * Sets a property value in 'obj' using JavaBean-compatible introspection * with one addition - a property can be a dot-separated property name path. */ public void setIn(Object bean, E value) { PropertyUtils.setProperty(bean, getName(), value); }
/** * Sets object property using JavaBean-compatible introspection with one * addition - a property can be a dot-separated property name path. Before * setting a value attempts to convert it to a type compatible with the * object property. Automatic conversion is supported between strings and * basic types like numbers or primitives. */ public static void setProperty(Object object, String nestedPropertyName, Object value) throws CayenneRuntimeException { accessor(nestedPropertyName).setValue(object, value); }
/** * Extracts property value from an object using JavaBean-compatible * introspection with one addition - a property can be a dot-separated * property name path. */ @SuppressWarnings("unchecked") public E getFrom(Object bean) { return (E) PropertyUtils.getProperty(bean, getName()); }
void updateIntProperty(Component c, String property, int defaultValue) { int i = getPreference().getInt(property, defaultValue); try { PropertyUtils.setProperty(c, property, i); } catch (Throwable th) { throw new PreferenceException("Error setting property: " + property, th); } }
public static PropertyReader reader(String fixedPropertyName) { Accessor accessor = PropertyUtils.accessor(fixedPropertyName); return (root, name) -> accessor.getValue(root); }
protected int getIndexValue(Object object) { Number n = (Number) PropertyUtils.getProperty(object, indexProperty); if (n == null) { throw new CayenneRuntimeException("Null index property '%s' for object %s" , indexProperty, object); } return n.intValue(); }
void injectValue(Object source, Object value) { if (!getPath().contains(ObjEntity.PATH_SEPARATOR)) { try { if (source instanceof DataObject) { ((DataObject) source).writeProperty(getPath(), value); } else { PropertyUtils.setProperty(source, getPath(), value); } } catch (CayenneRuntimeException ex) { LOGGER.warn("Failed to inject value " + value + " on path " + getPath() + " to " + source, ex); } } } }
private final Object readSimpleProperty(String property) { // side effect - resolves HOLLOW object Object object = readProperty(property); // if a null value is returned, there is still a chance to // find a non-persistent property via reflection if (object == null && !values.containsKey(property)) { object = PropertyUtils.getProperty(this, property); } return object; }