use of act.inject.DependencyInjector in project actframework by actframework.
the class Endpoint method exploreParamInfo.
private void exploreParamInfo(Method method) {
Type[] paramTypes = method.getGenericParameterTypes();
int paramCount = paramTypes.length;
if (0 == paramCount) {
return;
}
DependencyInjector injector = Act.injector();
Annotation[][] allAnnos = method.getParameterAnnotations();
Map<String, Object> sampleData = new HashMap<>();
StringValueResolverManager resolver = Act.app().resolverManager();
List<String> sampleQuery = new ArrayList<>();
for (int i = 0; i < paramCount; ++i) {
Type type = paramTypes[i];
Annotation[] annos = allAnnos[i];
ParamInfo info = paramInfo(type, annos, injector, null);
if (null != info) {
params.add(info);
if (path.contains("{" + info.getName() + "}")) {
// no sample data for URL path variable
continue;
}
Object sample;
if (null != info.defaultValue) {
sample = resolver.resolve(info.defaultValue, info.beanSpec.rawType());
} else {
sample = generateSampleData(info.beanSpec, new HashSet<Type>(), new ArrayList<String>());
}
if (H.Method.GET == this.httpMethod) {
String query = generateSampleQuery(info.beanSpec.withoutName(), info.bindName, new HashSet<Type>(), C.<String>newList());
if (S.notBlank(query)) {
sampleQuery.add(query);
}
} else {
sampleData.put(info.bindName, sample);
}
}
}
if (!sampleData.isEmpty()) {
sampleJsonPost = JSON.toJSONString(sampleData, true);
}
if (!sampleQuery.isEmpty()) {
this.sampleQuery = S.join("&", sampleQuery);
}
}
use of act.inject.DependencyInjector in project actframework by actframework.
the class Endpoint method exploreParamInfo.
private void exploreParamInfo(Class<?> controller) {
DependencyInjector injector = Act.injector();
List<Field> fields = $.fieldsOf(controller, FIELD_PREDICATE);
for (Field field : fields) {
Type type = field.getGenericType();
Annotation[] annos = field.getAnnotations();
ParamInfo info = paramInfo(type, annos, injector, field.getName());
if (null != info) {
params.add(info);
}
}
}
use of act.inject.DependencyInjector in project actframework by actframework.
the class SimpleEventListenerMetaInfo method convert.
public static List<BeanSpec> convert(List<String> paramTypes, String className, String methodName, $.Var<Method> methodHolder) {
int sz = paramTypes.size();
App app = Act.app();
ClassLoader cl = app.classLoader();
Class c = $.classForName(className, cl);
Class[] paramClasses = new Class[sz];
int i = 0;
for (String s : paramTypes) {
paramClasses[i++] = $.classForName(s, cl);
}
Method method = $.getMethod(c, methodName, paramClasses);
method.setAccessible(true);
methodHolder.set(method);
if (0 == sz) {
return C.list();
}
List<BeanSpec> retVal = new ArrayList<>(sz);
Type[] types = method.getGenericParameterTypes();
Annotation[][] annotations = method.getParameterAnnotations();
DependencyInjector injector = app.injector();
for (i = 0; i < types.length; ++i) {
retVal.add(BeanSpec.of(types[i], annotations[i], null, injector));
}
return C.list(retVal);
}
use of act.inject.DependencyInjector 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 act.inject.DependencyInjector in project actframework by actframework.
the class ReflectedHandlerInvoker method initOutputVariables.
private void initOutputVariables() {
Set<String> outputNames = new HashSet<>();
outputFields = new HashMap<>();
if (!isStatic) {
List<Field> fields = $.fieldsOf(controllerClass);
for (Field field : fields) {
Output output = field.getAnnotation(Output.class);
if (null != output) {
String fieldName = field.getName();
String outputName = output.value();
if (S.blank(outputName)) {
outputName = fieldName;
}
E.unexpectedIf(outputNames.contains(outputName), "output name already used: %s", outputName);
field.setAccessible(true);
outputFields.put(field, outputName);
outputNames.add(outputName);
}
}
}
try {
outputParams = new HashMap<>();
Class<?>[] paramTypes = method.getParameterTypes();
int len = paramTypes.length;
if (0 == len) {
return;
}
Annotation outputRequestParams = method.getAnnotation(OutputRequestParams.class);
if (null != outputRequestParams) {
DependencyInjector injector = app.injector();
for (int i = 0; i < len; ++i) {
if (!injector.isProvided(paramTypes[i])) {
String outputName = handler.param(i).name();
outputParams.put(i, outputName);
outputNames.add(outputName);
}
}
} else {
Annotation[][] aaa = method.getParameterAnnotations();
for (int i = 0; i < len; ++i) {
Annotation[] aa = aaa[i];
if (null == aa) {
continue;
}
Output output = null;
for (int j = aa.length - 1; j >= 0; --j) {
Annotation a = aa[j];
if (a.annotationType() == Output.class) {
output = $.cast(a);
break;
}
}
if (null == output) {
continue;
}
String outputName = output.value();
if (S.blank(outputName)) {
HandlerParamMetaInfo paramMetaInfo = handler.param(i);
outputName = paramMetaInfo.name();
}
E.unexpectedIf(outputNames.contains(outputName), "output name already used: %s", outputName);
outputParams.put(i, outputName);
outputNames.add(outputName);
}
}
} finally {
hasOutputVar = !outputNames.isEmpty();
}
}
Aggregations