public ConfigurationObjectFactory(ConfigSource config) { this.config = config; this.bully = new Bully(); }
public void addCoercible(final Coercible<?> coercible) { this.bully.addCoercible(coercible); }
private Object coerce(Class<?> clazz, String value) { Coercer<?> coercer = getCoercerFor(coercibles, clazz); if (coercer == null) { coercer = getCoercerFor(DEFAULT_COERCIBLES, clazz); if (coercer == null) { throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'", clazz, value)); } } return coercer.coerce(value); }
public synchronized Object coerce(Type type, String value, Separator separator) { if (type instanceof Class) { Class<?> clazz = (Class<?>)type; if (clazz.isArray()) { return coerceArray(clazz.getComponentType(), value, separator); } else if (Class.class.equals(clazz)) { return coerceClass(type, null, value); } else { return coerce(clazz, value); } } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)type; Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class<?>) { Type[] args = parameterizedType.getActualTypeArguments(); if (args != null && args.length == 1) { if (args[0] instanceof Class<?>) { return coerceCollection((Class<?>)rawType, (Class<?>)args[0], value, separator); } else if (args[0] instanceof WildcardType) { return coerceClass(type, (WildcardType)args[0], value); } } } } throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'", type, value)); }
private Object coerceArray(Class<?> elemType, String value, Separator separator) { if (value == null) { return null; } else if (value.length() == 0) { return Array.newInstance(elemType, 0); } else { String[] tokens = value.split(separator == null ? Separator.DEFAULT : separator.value()); Object targetArray = Array.newInstance(elemType, tokens.length); for (int idx = 0; idx < tokens.length; idx++) { Array.set(targetArray, idx, coerce(elemType, tokens[idx])); } return targetArray; } }
private Class<?> coerceClass(Type type, WildcardType wildcardType, String value) { if (value == null) { return null; } else { try { Class<?> clazz = Class.forName(value); if (!isAssignableFrom(wildcardType, clazz)) { throw new IllegalArgumentException("Specified class " + clazz + " is not compatible with required type " + type); } return clazz; } catch (Exception ex) { throw new IllegalArgumentException(ex); } } }
public synchronized Object coerce(Type type, String value, Separator separator) { if (type instanceof Class) { Class<?> clazz = (Class<?>)type; if (clazz.isArray()) { return coerceArray(clazz.getComponentType(), value, separator); } else if (Class.class.equals(clazz)) { return coerceClass(type, null, value); } else { return coerce(clazz, value); } } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType)type; Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class<?>) { Type[] args = parameterizedType.getActualTypeArguments(); if (args != null && args.length == 1) { if (args[0] instanceof Class<?>) { return coerceCollection((Class<?>)rawType, (Class<?>)args[0], value, separator); } else if (args[0] instanceof WildcardType) { return coerceClass(type, (WildcardType)args[0], value); } } } } throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'", type, value)); }
private Object coerceArray(Class<?> elemType, String value, Separator separator) { if (value == null) { return null; } else if (value.length() == 0) { return Array.newInstance(elemType, 0); } else { String[] tokens = value.split(separator == null ? Separator.DEFAULT : separator.value()); Object targetArray = Array.newInstance(elemType, tokens.length); for (int idx = 0; idx < tokens.length; idx++) { Array.set(targetArray, idx, coerce(elemType, tokens[idx])); } return targetArray; } }
private Class<?> coerceClass(Type type, WildcardType wildcardType, String value) { if (value == null) { return null; } else { try { Class<?> clazz = Class.forName(value); if (!isAssignableFrom(wildcardType, clazz)) { throw new IllegalArgumentException("Specified class " + clazz + " is not compatible with required type " + type); } return clazz; } catch (Exception ex) { throw new IllegalArgumentException(ex); } } }
public Object intercept(final Object o, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable { for (String property : properties) { if (args.length == paramTokenList.size()) { for (int i = 0; i < args.length; ++i) { property = property.replace(paramTokenList.get(i), String.valueOf(args[i])); } String value = config.getString(property); if (value != null) { logger.info("Assigning value [{}] for [{}] on [{}#{}()]", new Object[] { value, property, method.getDeclaringClass().getName(), method.getName() }); return bully.coerce(method.getGenericReturnType(), value, method.getAnnotation(Separator.class)); } } else { throw new IllegalStateException("Argument list doesn't match @Param list"); } } logger.info("Assigning default value [{}] for {} on [{}#{}()]", new Object[] { defaultValue, properties, method.getDeclaringClass().getName(), method.getName() }); return defaultValue; }
public ConfigurationObjectFactory(ConfigSource config) { this.config = config; this.bully = new Bully(); }
private Object coerce(Class<?> clazz, String value) { Coercer<?> coercer = getCoercerFor(coercibles, clazz); if (coercer == null) { coercer = getCoercerFor(DEFAULT_COERCIBLES, clazz); if (coercer == null) { throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'", clazz, value)); } } return coercer.coerce(value); }
public void addCoercible(final Coercible<?> coercible) { this.bully.addCoercible(coercible); }
public Object intercept(final Object o, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable { for (String property : properties) { if (args.length == paramTokenList.size()) { for (int i = 0; i < args.length; ++i) { property = property.replace(paramTokenList.get(i), String.valueOf(args[i])); } String value = config.getString(property); if (value != null) { logger.info("Assigning value [{}] for [{}] on [{}#{}()]", new Object[] { value, property, method.getDeclaringClass().getName(), method.getName() }); return bully.coerce(method.getGenericReturnType(), value, method.getAnnotation(Separator.class)); } } else { throw new IllegalStateException("Argument list doesn't match @Param list"); } } logger.info("Assigning default value [{}] for {} on [{}#{}()]", new Object[] { defaultValue, properties, method.getDeclaringClass().getName(), method.getName() }); return defaultValue; }
result.add(coerce(elemType, token));
result.add(coerce(elemType, token));
final Object bulliedDefaultValue = bully.coerce(method.getGenericReturnType(), defaultValue, method.getAnnotation(Separator.class)); final String[] annotationValues = annotation.value();
final Object bulliedDefaultValue = bully.coerce(method.getGenericReturnType(), defaultValue, method.getAnnotation(Separator.class)); final String[] annotationValues = annotation.value();
final Object finalValue = bully.coerce(method.getGenericReturnType(), value, method.getAnnotation(Separator.class)); callbacks.add(new ConfigMagicFixedValue(method, assignedFrom, finalValue, useMethod));
final Object finalValue = bully.coerce(method.getGenericReturnType(), value, method.getAnnotation(Separator.class)); callbacks.add(new ConfigMagicFixedValue(method, assignedFrom, finalValue, useMethod));