/** * Computes the attribute key according to the definition in {@link Attribute#key()}. * * @param clazz class containing the attribute * @param fieldName name of the field representing the attribute * @return key of the attribute */ public static String getKey(Class<?> clazz, String fieldName) { try { return BindableUtils.getKey(clazz.getDeclaredField(fieldName)); } catch (final SecurityException e) { throw new RuntimeException(e); } catch (final NoSuchFieldException e) { throw new RuntimeException(e); } } }
/** * Builds descriptors for direct attributes. */ private static Map<String, AttributeDescriptor> buildAttributeDescriptors( Object initializedInstance, BindableMetadata bindableMetadata) { final Class<?> clazz = initializedInstance.getClass(); final Map<String, AttributeDescriptor> result = Maps.newLinkedHashMap(); final Collection<Field> fieldsFromBindableHierarchy = BindableUtils .getFieldsFromBindableHierarchy(clazz); for (final Field field : fieldsFromBindableHierarchy) { if (field.getAnnotation(Attribute.class) != null) { result.put(BindableUtils.getKey(field), buildAttributeDescriptor( initializedInstance, field, bindableMetadata)); } } return result; }
/** * Returns all fields from all {@link Bindable} types in the hierarchy of the provided * <code>clazz</code>. The collected fields gets cached. */ static Collection<Field> getFieldsFromBindableHierarchy(Class<?> clazz) { return getFieldsFromHierarchy(clazz, Bindable.class); }
/** * Computes the attribute key according to the definition in {@link Attribute#key()}. */ public static String getKey(Field field) { final Attribute attributeAnnotation = field.getAnnotation(Attribute.class); if (attributeAnnotation == null) { return null; } if ("".equals(attributeAnnotation.key())) { return getPrefix(field.getDeclaringClass()) + "." + field.getName(); } else { return attributeAnnotation.key(); } }
final Collection<Field> fieldsFromBindableHierarchy = BindableUtils.getFieldsFromBindableHierarchy(clazz); for (final Field field : fieldsFromBindableHierarchy)
/** * An internal constructor. */ BindableDescriptor(Class<?> bindableType, BindableMetadata metadata, Map<Field, BindableDescriptor> bindableDescriptors, Map<String, AttributeDescriptor> attributeDescriptors, GroupingMethod groupBy) { this.type = bindableType; this.prefix = BindableUtils.getPrefix(bindableType); this.metadata = metadata; this.bindableDescriptorsInternal = bindableDescriptors; this.attributeDescriptorsInternal = attributeDescriptors; this.groupedBy = groupBy; final LinkedHashMap<String, AttributeDescriptor> newAttributeDescriptors = Maps .newLinkedHashMap(); final LinkedHashMap<Object, Map<String, AttributeDescriptor>> newAttributeGroups = Maps .newLinkedHashMap(); buildAttributeGroups(newAttributeDescriptors, newAttributeGroups, this, groupedBy); this.attributeDescriptors = Collections.unmodifiableMap(newAttributeDescriptors); this.attributeGroups = Collections.unmodifiableMap(newAttributeGroups); }
/** * */ AttributeDescriptor(Field field, Object defaultValue, List<Annotation> constraints, AttributeMetadata metadata) { this.attributeField = field; this.attributeDeclaringClassString = field.getDeclaringClass().getName(); this.key = BindableUtils.getKey(field); this.type = Primitives.wrap(field.getType()); this.defaultValue = defaultValue; this.constraints = constraints; this.metadata = metadata; this.inputAttribute = field.getAnnotation(Input.class) != null; this.outputAttribute = field.getAnnotation(Output.class) != null; this.requiredAttribute = field.getAnnotation(Required.class) != null; prepareForSerialization(); }
/** * Returns all fields from all {@link Bindable} types in the hierarchy of the provided * <code>clazz</code>. The collected fields get cached. */ static Collection<Field> getFieldsFromHierarchy(Class<?> clazz, Class<? extends Annotation> marker) { synchronized (FIELD_CACHE) { final Pair<Class<? extends Annotation>, Class<?>> key = new Pair<Class<? extends Annotation>, Class<?>>(marker, clazz); Collection<Field> fields = FIELD_CACHE.get(key); if (fields == null) { fields = new LinkedHashSet<Field>(); if (clazz.getAnnotation(marker) != null) { fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } final Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { fields.addAll(getFieldsFromHierarchy(superClass, marker)); } FIELD_CACHE.put(key, fields); } return fields; } }
throws InstantiationException final String key = BindableUtils.getKey(field); final Object value = values.get(key); if (value == null || Class.class.isInstance(value)
final Collection<Field> fieldSet = BindableUtils.getFieldsFromHierarchy( object.getClass(), markerAnnotation);
/** * Builds {@link AttributeDescriptor} for a field from a {@link Bindable} type. */ private static AttributeDescriptor buildAttributeDescriptor( Object initializedInstance, Field field, BindableMetadata bindableMetadata) { Object defaultValue = null; try { defaultValue = field.get(initializedInstance); } catch (IllegalAccessException e) { throw new AssertionError("An attribute field must be public: " + field.getDeclaringClass().getName() + "#" + field.getName()); } catch (Exception e) { throw new RuntimeException("Could not retrieve default value of attribute: " + BindableUtils.getKey(field), e); } AttributeMetadata attributeMetadata = null; if (bindableMetadata != null) { attributeMetadata = bindableMetadata.getAttributeMetadata().get( field.getName()); } return new AttributeDescriptor(field, defaultValue, getConstraintAnnotations(field), attributeMetadata); }
public void performAction(BindingTracker bindingTracker, int level, Object object, Field field, Object value, Predicate<Field> predicate) throws InstantiationException { final String key = BindableUtils.getKey(field); try { // Apply transforms for (IAttributeTransformer transformer : transformers) { value = transformer.transform(value, key, field); } if (bindingTracker.canBind(object, key, level)) { values.put(key, value); } } catch (final Exception e) { throw new AttributeBindingException(key, "Could not get field value " + object.getClass().getName() + "#" + field.getName(), e); } } }
&& checkRequired; final Object currentValue = value; final String key = BindableUtils.getKey(field);