/** 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; }
/** returns a map of all {@link ConfigKey} fields which are annotated 'SetFromFlag', along with the annotation */ public static Map<ConfigKey<?>,SetFromFlag> getAnnotatedConfigKeys(Class<?> type) { Map<ConfigKey<?>, SetFromFlag> result = Maps.newLinkedHashMap(); List<Field> fields = getAllFields(type, new Predicate<Field>() { @Override public boolean apply(Field f) { return (f != null) && ConfigKey.class.isAssignableFrom(f.getType()) && ((f.getModifiers() & Modifier.STATIC)!=0); }}); for (Field f: fields) { SetFromFlag cf = f.getAnnotation(SetFromFlag.class); if (cf != null) { ConfigKey<?> key = getFieldAsConfigKey(null, f); if (key != null) { result.put(key, cf); } } } return result; }
/** 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; }
ConfigKey<?> key = getFieldAsConfigKey(objectOfField, f); if (key!=null) { setConfig(objectOfField, key, value, optionalAnnotation);
/** 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); } } } }
private static void setFieldFromConfig(Object o, Field f, ConfigBag bag, SetFromFlag optionalAnnotation, boolean setDefaultVals) { String flagName = optionalAnnotation==null ? null : elvis(optionalAnnotation.value(), f.getName()); // prefer flag name, if present if (groovyTruth(flagName) && bag.containsKey(flagName)) { setField(o, f, bag.getStringKey(flagName), optionalAnnotation); return; } // first check whether it is a key ConfigKey<?> key = getFieldAsConfigKey(o, f); if (key!=null && bag.containsKey(key)) { Object uncoercedValue = bag.getObjKeyMaybe(key).get(); setField(o, f, uncoercedValue, optionalAnnotation); return; } if (setDefaultVals && optionalAnnotation!=null && groovyTruth(optionalAnnotation.defaultVal())) { Object oldValue; try { f.setAccessible(true); oldValue = f.get(o); if (oldValue==null || oldValue.equals(getDefaultValueForType(f.getType()))) { setField(o, f, optionalAnnotation.defaultVal(), optionalAnnotation); } } catch (Exception e) { Exceptions.propagate(e); } return; } }