use of org.osgl.util.Const in project actframework by actframework.
the class ConfigurationByteCodeScanner method scanFinished.
@Override
public void scanFinished(final String className) {
E.unexpectedIf(S.neq(this.className, className), "oops");
if (staticConfigurationFields.isEmpty()) {
return;
}
final DependencyInjector injector = app().injector();
final Map<String, String> staticConfigurationFields = new HashMap<>(this.staticConfigurationFields);
app().jobManager().on(SysEventId.PRE_START, new Runnable() {
@Override
public void run() {
Class<?> theClass = app().classForName(className);
for (Map.Entry<String, String> entry : staticConfigurationFields.entrySet()) {
String fieldName = entry.getKey();
String conf = entry.getValue();
Field field = $.fieldOf(theClass, fieldName, false);
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
field.setAccessible(true);
Class<?> fieldType = field.getType();
boolean isConst = false;
boolean isVal = false;
BeanSpec valueSpec = BeanSpec.of(field, injector);
if (Const.class.isAssignableFrom(fieldType)) {
valueSpec = BeanSpec.of(field, injector).componentSpec();
isConst = true;
} else if ($.Val.class.isAssignableFrom(fieldType)) {
valueSpec = BeanSpec.of(field, injector).componentSpec();
isVal = true;
}
ConfigurationValueLoader loader = app().getInstance(ConfigurationValueLoader.class);
Map<String, String> map = C.newMap("value", conf);
if (null != defaultValue) {
map.put("defaultValue", defaultValue.value());
}
loader.init(map, valueSpec);
Object value = loader.get();
try {
if (isConst) {
Const<?> fieldValue = $.cast(field.get(null));
if (null == fieldValue) {
fieldValue = $.constant(value);
field.set(null, fieldValue);
} else {
Field fv = Const.class.getDeclaredField("v");
fv.setAccessible(true);
fv.set(fieldValue, value);
}
} else if (isVal) {
$.Val<?> fieldValue = $.cast(field.get(null));
if (null == fieldValue) {
fieldValue = $.val(value);
field.set(null, fieldValue);
} else {
Field fv = $.Var.class.getDeclaredField("v");
fv.setAccessible(true);
fv.set(fieldValue, value);
}
} else {
field.set(null, value);
}
} catch (Exception e) {
throw E.unexpected(e, "failed to set configuration value[%] to field[%s]", value, field);
}
}
}
});
}
Aggregations