use of act.inject.DefaultValue in project actframework by actframework.
the class ActionContextParamLoader method findContextSpecificLoader.
@Override
protected ParamValueLoader findContextSpecificLoader(String bindName, BeanSpec spec) {
HeaderVariable headerVariable = spec.getAnnotation(HeaderVariable.class);
if (null != headerVariable) {
return new HeaderValueLoader(headerVariable.value(), spec);
}
SessionVariable sessionVariable = spec.getAnnotation(SessionVariable.class);
if (null != sessionVariable) {
return new SessionValueLoader(sessionVariable.value(), spec);
}
if (spec.hasAnnotation(PartialPath.class)) {
return new PartialPathLoader(bindName);
}
DefaultValue def = spec.getAnnotation(DefaultValue.class);
Class<?> rawType = spec.rawType();
ParamValueLoader loader = binder(spec, bindName);
if (null == loader) {
Resolve resolve = spec.getAnnotation(Resolve.class);
if (null != resolve) {
Class<? extends StringValueResolver>[] resolvers = resolve.value();
for (Class<? extends StringValueResolver> resolverClass : resolvers) {
StringValueResolver resolver = injector.get(resolverClass);
if (rawType.isAssignableFrom(resolver.targetType())) {
loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
}
}
}
}
if (null == loader) {
Annotation[] aa = spec.allAnnotations();
for (Annotation a : aa) {
Resolve resolve = AnnotationUtil.tagAnnotation(a, Resolve.class);
if (null != resolve) {
Class<? extends StringValueResolver>[] resolvers = resolve.value();
for (Class<? extends StringValueResolver> resolverClass : resolvers) {
StringValueResolver resolver = injector.get(resolverClass);
resolver.attributes($.evaluate(a));
if (rawType.isAssignableFrom(resolver.targetType())) {
loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
break;
}
}
}
}
}
if (null == loader) {
StringValueResolver resolver = null;
Param param = spec.getAnnotation(Param.class);
if (null != param) {
Class<? extends StringValueResolver> resolverClass = param.resolverClass();
if (Param.DEFAULT_RESOLVER.class != resolverClass) {
resolver = injector.get(resolverClass);
}
}
if (null == resolver) {
resolver = resolverManager.resolver(rawType, spec);
}
loader = (null != resolver) ? new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, param, def, rawType) : buildLoader(ParamKey.of(bindName), spec);
}
return loader;
}
use of act.inject.DefaultValue in project actframework by actframework.
the class ParamValueLoaderService method findLoader.
private ParamValueLoader findLoader(ParamKey paramKey, BeanSpec spec) {
if (provided(spec, injector)) {
return ProvidedValueLoader.get(spec, injector);
}
String name = spec.name();
ParamKey key = paramKey.child(name);
ParamValueLoader loader = binder(spec, key.toString());
if (null != loader) {
return loader;
}
Class fieldType = spec.rawType();
StringValueResolver resolver = resolverManager.resolver(fieldType, spec);
if (null != resolver) {
DefaultValue def = spec.getAnnotation(DefaultValue.class);
return new StringValueResolverValueLoader(key, resolver, null, def, fieldType);
}
return buildLoader(key, spec);
}
use of act.inject.DefaultValue 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