@Override protected CatalogItemDtoAbstract<T, SpecT> configure(Map<?, ?> flags) { FlagUtils.setFieldsFromFlags(flags, this); return this; }
public static List<Field> getAllFields(Class<?> base, Predicate<Field> filter) { return getLocalFields(getAllAssignableTypes(base), filter); } /** returns all fields explicitly declared on the given classes */
/** sets _all_ accessible _{@link ConfigKey}_ and {@link HasConfigKey} fields on the given object, * using the indicated flags/config-bag */ public static void setAllConfigKeys(Configurable o, ConfigBag bag, boolean includeFlags) { for (Field f: getAllFields(o.getClass())) { ConfigKey<?> key = getFieldAsConfigKey(o, f); if (key!=null) { FlagConfigKeyAndValueRecord record = getFlagConfigKeyRecord(f, key, bag); if ((includeFlags && record.isValuePresent()) || record.getConfigKeyMaybeValue().isPresent()) { setField(o, f, record.getValueOrNullPreferringConfigKey(), null); } } } }
/** get all fields (including private and static) on the given object and all supertypes, * that are annotated with SetFromFlags. */ public static Map<String, ?> getFieldsWithFlags(Object o) { return getFieldsWithFlagsInternal(o, getAllFields(o.getClass())); }
/** * Finds the {@link Field} on the given object annotated with the given name flag. */ public static Field findFieldForFlag(String flagName, Object o) { return findFieldForFlagInternal(flagName, o, getAllFields(o.getClass())); }
@Override protected void addConfig(RebindContext rebindContext, FeedMemento memento) { // TODO entity does config-lookup differently; the memento contains the config keys. // BasicEntityMemento.postDeserialize uses the injectTypeClass to call EntityTypes.getDefinedConfigKeys(clazz) ConfigBag configBag = ConfigBag.newInstance(memento.getConfig()); FlagUtils.setFieldsFromFlags(feed, configBag); FlagUtils.setAllConfigKeys(feed, configBag, false); }
/** see {@link #setFieldsFromFlags(Object, ConfigBag)} */ public static Map<?, ?> setFieldsFromFlags(Map<?, ?> flags, Object o) { return setFieldsFromFlagsInternal(o, getAllFields(o.getClass()), flags, null, true); }
@Test public void testCheckRequired() { WithImmutableNonNullableObject f = new WithImmutableNonNullableObject(); FlagUtils.setFieldsFromFlags(ImmutableMap.of("a", "a is a"), f); assertEquals(f.a, "a is a"); assertEquals(f.b, null); int exceptions = 0; try { FlagUtils.checkRequiredFields(f); } catch (IllegalStateException e) { exceptions++; } assertEquals(exceptions, 1); }
/** gets all the flags/keys in the given config bag which are applicable to the given type's config keys and flags */ public static <T> List<FlagConfigKeyAndValueRecord> findAllFlagsAndConfigKeys(T optionalInstance, Class<? extends T> type, ConfigBag input) { List<FlagConfigKeyAndValueRecord> output = new ArrayList<FlagUtils.FlagConfigKeyAndValueRecord>(); for (Field f: getAllFields(type)) { ConfigKey<?> key = getFieldAsConfigKey(optionalInstance, f); FlagConfigKeyAndValueRecord record = getFlagConfigKeyRecord(f, key, input); if (record.isValuePresent()) output.add(record); } return output; }
public static List<Field> getAllFields(Class<?> base) { return getAllFields(base, Predicates.<Field>alwaysTrue()); } public static List<Field> getAllFields(Class<?> base, Predicate<Field> filter) {
@Test public void testSetFieldsFromFlags() { Foo f = new Foo(); Map<?,?> m = MutableMap.of("w", 3, "x", 1, "y", 7, "z", 9); Map<?, ?> unused = FlagUtils.setFieldsFromFlags(m, f); assertEquals(f.w, 3); assertEquals(f.x, 1); assertEquals(f.yNotY, 7); assertEquals(unused, ImmutableMap.of("z", 9)); Map<?,?> m2 = FlagUtils.getFieldsWithValues(f); m.remove("z"); assertEquals(m2, m); }
@Test public void testGetAllFields() { log.info("types {}", FlagUtils.getAllAssignableTypes(Baz.class)); assertEquals(FlagUtils.getAllAssignableTypes(Baz.class), ImmutableList.of(Baz.class, Foo.class, Bar.class)); List<Field> fs = FlagUtils.getAllFields(Baz.class); for (Field f : fs) { log.info("field {} {}", f.getName(), f); } List<String> fsn = ImmutableList.copyOf(Iterables.transform(fs, new Function<Field, String>() { @Override public String apply(Field f) { return f.getName(); }})); assertTrue(fsn.indexOf("A") >= 0); assertTrue(fsn.indexOf("w") > fsn.indexOf("A")); assertTrue(fsn.indexOf("x") > fsn.indexOf("A") ); assertTrue(fsn.indexOf("yNotY") > fsn.indexOf("A")); assertTrue(fsn.indexOf("Z") > fsn.indexOf("yNotY") ); }
/** gets all the config keys for the given type */ public static <T> List<ConfigKey<?>> findAllConfigKeys(T optionalInstance, Class<? extends T> type) { List<ConfigKey<?>> output = new ArrayList<ConfigKey<?>>(); for (Field f: getAllFields(type)) { ConfigKey<?> key = getFieldAsConfigKey(optionalInstance, f); if (key != null) output.add(key); } return output; }
/** * Sets the field with the given flag (if it exists) to the given value. * Will attempt to coerce the value to the required type. * Will respect "nullable" on the SetFromFlag annotation. * * @throws IllegalArgumentException If fieldVal is null and the SetFromFlag annotation set nullable=false */ public static boolean setFieldFromFlag(Object o, String flagName, Object fieldVal) { return setFieldFromFlagInternal(checkNotNull(flagName, "flagName"), fieldVal, o, getAllFields(o.getClass())); }
/** sets _all_ accessible _{@link ConfigKey}_ and {@link HasConfigKey} fields on the given object, * using the indicated flags/config-bag * @deprecated since 0.7.0 use {@link #setAllConfigKeys(Map, Configurable, boolean)} */ @Deprecated public static Map<String, ?> setAllConfigKeys(Map<String, ?> flagsOrConfig, Configurable instance) { return setAllConfigKeys(flagsOrConfig, instance, false); } /** sets _all_ accessible _{@link ConfigKey}_ and {@link HasConfigKey} fields on the given object,
/** * Searches for config keys in the type, additional interfaces and the implementation (if specified) */ private Collection<FlagUtils.FlagConfigKeyAndValueRecord> findAllFlagsAndConfigKeys(EntitySpec<? extends Entity> spec, ConfigBag bagFlags) { Set<FlagUtils.FlagConfigKeyAndValueRecord> allKeys = MutableSet.of(); allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, spec.getType(), bagFlags)); if (spec.getImplementation() != null) { allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, spec.getImplementation(), bagFlags)); } for (Class<?> iface : spec.getAdditionalInterfaces()) { allKeys.addAll(FlagUtils.findAllFlagsAndConfigKeys(null, iface, bagFlags)); } return allKeys; }
public AbstractEntityAdjunct(@SuppressWarnings("rawtypes") Map properties) { super(properties); _legacyNoConstructionInit = (properties != null) && Boolean.TRUE.equals(properties.get("noConstructionInit")); if (isLegacyConstruction()) { AbstractEntityAdjunct checkWeGetThis = configure(properties); assert this.equals(checkWeGetThis) : this+" configure method does not return itself; returns "+checkWeGetThis+" instead of "+this; boolean deferConstructionChecks = (properties.containsKey("deferConstructionChecks") && TypeCoercions.coerce(properties.get("deferConstructionChecks"), Boolean.class)); if (!deferConstructionChecks) { FlagUtils.checkRequiredFields(this); } } }
/** returns a map of all fields which are annotated 'SetFromFlag' with their current values; * useful if you want to clone settings from one object */ public static Map<String,Object> getFieldsWithValues(Object o) { try { Map<String, Object> result = Maps.newLinkedHashMap(); for (Map.Entry<Field, SetFromFlag> entry : getAnnotatedFields(o.getClass()).entrySet()) { Field f = entry.getKey(); SetFromFlag cf = entry.getValue(); String flagName = elvis(cf.value(), f.getName()); if (groovyTruth(flagName)) { if (!f.isAccessible()) f.setAccessible(true); result.put(flagName, f.get(o)); } } return result; } catch (IllegalAccessException e) { throw Throwables.propagate(e); } }
field = FlagUtils.findFieldForFlag(flagName, location); } catch (NoSuchElementException e) { ConfigKey<?> configKey = (ConfigKey<?>) FlagUtils.getField(location, field); location.config().set((ConfigKey<Object>)configKey, entry.getValue()); } else { FlagUtils.setFieldFromFlag(location, flagName, value);
FlagUtils.setFieldsFromFlagsWithBag(this, properties, configBag, firstTime); FlagUtils.setAllConfigKeys(this, configBag, false);