use of org.osgl.inject.BeanSpec in project actframework by actframework.
the class JsonDTOClassManager method extractBeanSpec.
private void extractBeanSpec(List<BeanSpec> beanSpecs, Method method, Class host) {
Type[] paramTypes = method.getGenericParameterTypes();
int sz = paramTypes.length;
if (0 == sz) {
return;
}
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < sz; ++i) {
Type type = paramTypes[i];
if (type instanceof TypeVariable && !Modifier.isStatic(method.getModifiers())) {
// explore type variable impl
TypeVariable typeVar = $.cast(type);
String typeVarName = typeVar.getName();
// find all generic types on host
Map<String, Class> typeVarLookup = Generics.buildTypeParamImplLookup(host);
type = typeVarLookup.get(typeVarName);
if (null == type) {
throw new UnexpectedException("Cannot determine concrete type of method parameter %s", typeVarName);
}
}
Annotation[] anno = annotations[i];
BeanSpec spec = BeanSpec.of(type, anno, injector);
if (ParamValueLoaderService.providedButNotDbBind(spec, injector)) {
continue;
}
String dbBindName = dbBindName(spec);
if (null != dbBindName) {
beanSpecs.add(BeanSpec.of(String.class, new Annotation[0], dbBindName, injector));
} else {
beanSpecs.add(spec);
}
}
}
use of org.osgl.inject.BeanSpec in project actframework by actframework.
the class ParamValueLoaderService method fieldLoaders.
private <T> Map<Field, ParamValueLoader> fieldLoaders(Class<T> beanClass) {
Map<Field, ParamValueLoader> fieldLoaders = fieldRegistry.get(beanClass);
if (null == fieldLoaders) {
Map<Field, ParamValueLoader> newFieldLoaders = new HashMap<>();
for (Field field : $.fieldsOf(beanClass, true)) {
if (shouldWaive(field)) {
continue;
}
BeanSpec spec = BeanSpec.of(field, injector);
ParamValueLoader loader = paramValueLoaderOf(spec, null);
boolean provided = (loader instanceof ProvidedValueLoader);
if (null != loader && !provided) {
newFieldLoaders.put(field, loader);
}
}
fieldLoaders = fieldRegistry.putIfAbsent(beanClass, newFieldLoaders);
if (null == fieldLoaders) {
fieldLoaders = newFieldLoaders;
}
}
return fieldLoaders;
}
use of org.osgl.inject.BeanSpec 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);
}
}
}
});
}
use of org.osgl.inject.BeanSpec in project actframework by actframework.
the class Endpoint method generateSampleQuery.
private String generateSampleQuery(BeanSpec spec, String bindName, Set<Type> typeChain, List<String> nameChain) {
Class<?> type = spec.rawType();
String specName = spec.name();
if (S.notBlank(specName)) {
nameChain.add(specName);
}
if ($.isSimpleType(type)) {
Object o = generateSampleData(spec, typeChain, nameChain);
if (null == o) {
return "";
}
return bindName + "=" + o;
}
if (type.isArray()) {
// TODO handle datetime component type
Class<?> elementType = type.getComponentType();
BeanSpec elementSpec = BeanSpec.of(elementType, Act.injector());
if ($.isSimpleType(elementType)) {
Object o = generateSampleData(elementSpec, typeChain, nameChain);
if (null == o) {
return "";
}
return bindName + "=" + o + "&" + bindName + "=" + generateSampleData(elementSpec, typeChain, nameChain);
}
} else if (Collection.class.isAssignableFrom(type)) {
// TODO handle datetime component type
List<Type> typeParams = spec.typeParams();
Type elementType = typeParams.isEmpty() ? Object.class : typeParams.get(0);
BeanSpec elementSpec = BeanSpec.of(elementType, null, Act.injector());
if ($.isSimpleType(elementSpec.rawType())) {
Object o = generateSampleData(elementSpec, typeChain, nameChain);
if (null == o) {
return "";
}
return bindName + "=" + o + "&" + bindName + "=" + generateSampleData(elementSpec, typeChain, nameChain);
}
} else if (Map.class.isAssignableFrom(type)) {
LOGGER.warn("Map not supported yet");
return "";
} else if (ReadableInstant.class.isAssignableFrom(type)) {
return bindName + "=<datetime>";
}
if (null != stringValueResolver(type)) {
return bindName + "=" + S.random(5);
}
List<String> queryPairs = new ArrayList<>();
List<Field> fields = $.fieldsOf(type);
for (Field field : fields) {
if (ParamValueLoaderService.shouldWaive(field)) {
continue;
}
String fieldBindName = bindName + "." + field.getName();
String pair = generateSampleQuery(BeanSpec.of(field, Act.injector()), fieldBindName, C.newSet(typeChain), C.newList(nameChain));
if (S.notBlank(pair)) {
queryPairs.add(pair);
}
}
return S.join(queryPairs).by("&").get();
}
use of org.osgl.inject.BeanSpec in project actframework by actframework.
the class CliContextParamLoader method findParamOptionLoaders.
private void findParamOptionLoaders(Method m, CommandMethodMetaInfo methodMetaInfo, List<OptionLoader> optionLoaders) {
Type[] types = m.getGenericParameterTypes();
int len = types.length;
if (len == 0) {
return;
}
Annotation[][] allAnnotations = m.getParameterAnnotations();
for (int i = len - 1; i >= 0; --i) {
Type type = types[i];
Annotation[] annotations = allAnnotations[i];
BeanSpec spec = BeanSpec.of(type, annotations, null, injector);
String bindName = tryFindBindName(annotations, spec.name());
if (null == bindName) {
bindName = methodMetaInfo.param(i).name();
}
ParamValueLoader loader = findContextSpecificLoader(bindName, spec);
if (loader instanceof OptionLoader) {
optionLoaders.add((OptionLoader) loader);
} else if (!$.isSimpleType(spec.rawType())) {
}
}
}
Aggregations