public void visit(Class<?> c) { annotatedFields.addAll(getAnnotatedFields(c, annotation)); } });
private List<Field> getBindableModelFields() { List<Field> allModelFields = Reflections.getFieldsOfTypeForClassHierarchy(object.getClass(), BindableModel.class); List<Field> notBindableFields = Reflections.getAnnotatedFieldsForClassHierarchy(object.getClass(), NotBindable.class); allModelFields = ImmutableList.copyOf(Iterables.filter(allModelFields, Predicates.not(Predicates.in(notBindableFields)))); List<Field> nonFinalModelFields = ImmutableList.copyOf(Iterables.filter(allModelFields, Predicates.not(Reflections.IS_FIELD_FINAL))); if (!nonFinalModelFields.isEmpty()) { throw new BindingException("All BindableModels have to be final or marked with @NotBindable, but "+ Iterables.transform(nonFinalModelFields, Reflections.FIELD_TO_NAME)+" are not."); } return allModelFields; }
/** * Returns the value of the specified Field on the object bound by this {@link BindingContext} * * @param field {@link Field} to pull the value from * @param klass return type of value in the {@link Field} * @return value of type <code>klass</code> from field <code>field</code> on bound object. * @throws IllegalArgumentException if the passed {@link Field} is not a field on the object * bound by this {@link BindingContext} */ public <T> T getFieldObject(Field field, Class<T> klass) throws IllegalArgumentException { return Reflections.getFieldObject(object, field, klass); }
/** * Resolves a string reference, as specified in the <code>on</code> parameter of * a {@link Bound} annotation to an Enum object in this runtime. * @param on <code>on</code> parameter from a {@link Bound} annotation. * @param model * @return the resolved object * @throws IllegalArgumentException if the referenced object can't be found. */ public static ModelUpdate findOnObject(final String on, final BindableModel model) { ModelUpdate onObject = null; if (!isNullOrBlank(on)) { final List<Class<?>> updateClasses = findModelUpdateClass(model); for (Class<?> updateClass : updateClasses) { try { onObject = (ModelUpdate)Reflections.evalEnum(updateClass, on); return onObject; } catch (IllegalArgumentException e) { // swallow this if we don't find the enum on one of the // classes, continue to next class. } } throw new IllegalArgumentException("could not find \"on\" parameter " + on); } return onObject; }
private Map<String, ObjectFieldMethod> indexBindableMethods() throws IllegalArgumentException { // Get all fields marked @Bindable List<Field> bindables = getAnnotatedFields(Bindable.class); if (Iterables.any(bindables, Predicates.not(Reflections.IS_FIELD_FINAL))) { throw new BindingException("all @Bindables have to be final"); } // Add all BindableModels bindables.addAll(getBindableModelFields()); // Index those methods. List<ObjectFieldMethod> methods = getParameterlessMethodsOnFieldTypes(object, bindables); // Add methods for classes marked @Bindable if (Reflections.isClassAnnotatedForClassHierarchy(object, Bindable.class)) { methods.addAll(Reflections.getParameterlessMethodsForClassHierarchy(object)); } return indexMethods(methods); }
public void visit(Class<?> c) { fields.addAll(getFieldsOfType(c, targetClass)); } });
public void visit(Class<?> c) { classes.addAll(getTypesOfType(c, targetClass)); } });
public List<Field> getAnnotatedFields(Class<? extends Annotation> klass) { return Reflections.getAnnotatedFieldsForClassHierarchy(object.getClass(), klass); }
public List<ObjectFieldMethod> getAnnotatedParameterlessMethods(final Class<? extends Annotation> annotation) { return Lists.newArrayList(Iterables.filter(Reflections.getParameterlessMethodsForClassHierarchy(object), new Predicate<ObjectFieldMethod>() { public boolean apply(ObjectFieldMethod input) { return input.getMethod().isAnnotationPresent(annotation); } })); }
/** * Returns the list of {@link ModelUpdate} types in this binding context. * @param modelClass * @return the of {@link Class}es that implement {@link ModelUpdate} in this binding context. */ public static List<Class<?>> findModelUpdateClass(final BindableModel modelClass) { List<Class<?>> classes = Reflections.getTypesOfTypeForClassHierarchy( modelClass.getClass(), ModelUpdate.class); Predicate<Class<?>> isEnum = new Predicate<Class<?>>() { public boolean apply(final Class<?> input) { return input.isEnum(); } }; // Look for ModelUpdate classes in implemented interfaces classes = Lists.newArrayList(Iterables.filter(classes, isEnum)); for (Class<?> iface : modelClass.getClass().getInterfaces()) { classes.addAll(Lists.newArrayList(Iterables.filter( Reflections.getTypesOfTypeForClassHierarchy( iface, ModelUpdate.class), isEnum))); } if (classes.size() == 0) { return null; } return classes; }
/** * Create a BindingContext for the given, non-null object. Throws a {@link BindingException} * if there is a problem. * @param object the object - cannot be null */ public BindingContext(Object object) { Preconditions.checkNotNull(object, "object"); this.object = object; try { bindableModels = indexBindableModels(); bindableMethods = indexBindableMethods(); bindableModelMethods = indexBindableModelMethods(); bindableConstants = indexBindableConstants(); bindableGetters = indexBindableProperties(Reflections.getterFunction(PropertyDescriptor.class, Method.class, "readMethod")); bindableSetters = indexBindableProperties(Reflections.getterFunction(PropertyDescriptor.class, Method.class, "writeMethod")); } catch (Exception e) { throw new BindingException("could not create BindingContext", e); } }
public void testEnumTypes() throws Exception { Object o = Reflections.evalEnum(TestEnum.class, "FOO"); assertTrue("Object is not a ModelUpdate instance",ModelUpdate.class.isAssignableFrom(o.getClass())); } }
public static Binding bindJToggleButtonToEnum(final String value, final Class<?> enumType, final Mutator mutator, final AbstractButton button) { final Object enumValue = Reflections.evalEnum(enumType, value); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { mutator.set(enumValue); } catch (Exception ex) { Wiring.logger.error("exception in JToggleButton binding", ex); //$NON-NLS-1$ } } }); Binding binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { try { button.setSelected(mutator.get() == enumValue); } catch (Exception ex) { Wiring.logger.error("exception in JToggleButton binding", ex); //$NON-NLS-1$ } } }; mutator.getModel().bind(binding); return binding; } }
private static List<ObjectFieldMethod> getParameterlessMethodsOnFieldTypes(Object object, List<Field> fields) throws IllegalArgumentException { List<ObjectFieldMethod> methods = Lists.newArrayList(); for (Field field : fields) { Object fieldObject = Reflections.getFieldObject(object, field, Object.class); methods.addAll(getParameterlessMethods(fieldObject, field)); } return methods; }