use of jodd.madvoc.config.ScopeData in project jodd by oblac.
the class ContextInjectorComponent method injectContext.
/**
* Inject context into target.
*/
public void injectContext(final Object targetObject) {
final Class targetType = targetObject.getClass();
final ScopeData scopeData = scopeDataInspector.inspectClassScopesWithCache(targetType);
final Targets targets = new Targets(targetObject, scopeData);
// inject no context
scopeResolver.forEachScope(madvocScope -> madvocScope.inject(targets));
// inject special case
scopeResolver.forScope(ParamsScope.class, scope -> scope.inject(targets));
// inject servlet context
final ServletContext servletContext = madvocController.getApplicationContext();
if (servletContext != null) {
scopeResolver.forEachScope(madvocScope -> madvocScope.inject(servletContext, targets));
}
}
use of jodd.madvoc.config.ScopeData in project jodd by oblac.
the class ScopeDataInspectorTest method testGenericAction.
@Test
void testGenericAction() {
ScopeDataInspector scopeDataInspector = new ScopeDataInspector();
PetiteContainer madpc = new PetiteContainer();
scopeDataInspector.scopeResolver = new ScopeResolver();
scopeDataInspector.scopeResolver.madpc = madpc;
madpc.addBean("madvocEncoding", new MadvocEncoding());
ScopeData scopeData = scopeDataInspector.inspectClassScopes(GenAction.class);
InjectionPoint[] in1 = scopeData.in();
InjectionPoint[] out1 = scopeData.out();
InjectionPoint in = in1[0];
InjectionPoint out = out1[0];
assertEquals("input", in.name());
assertEquals(String.class, in.type());
assertEquals("output", out.name());
assertEquals(Integer.class, out.type());
}
use of jodd.madvoc.config.ScopeData in project jodd by oblac.
the class ActionMethodParser method createActionRuntime.
// ---------------------------------------------------------------- create action runtime
/**
* Creates new instance of action runtime configuration.
* Initialize caches.
*/
public ActionRuntime createActionRuntime(final ActionHandler actionHandler, final Class actionClass, final Method actionClassMethod, final Class<? extends ActionResult> actionResult, final Class<? extends ActionResult> defaultActionResult, final ActionFilter[] filters, final ActionInterceptor[] interceptors, final ActionDefinition actionDefinition, final boolean async, final boolean auth) {
if (actionHandler != null) {
return new ActionRuntime(actionHandler, actionClass, actionClassMethod, filters, interceptors, actionDefinition, NoneActionResult.class, NoneActionResult.class, async, auth, null, null);
}
final ScopeData scopeData = scopeDataInspector.inspectClassScopes(actionClass);
// find ins and outs
final Class[] paramTypes = actionClassMethod.getParameterTypes();
final MethodParam[] params = new MethodParam[paramTypes.length];
final Annotation[][] paramAnns = actionClassMethod.getParameterAnnotations();
String[] methodParamNames = null;
// for all elements: action and method arguments...
for (int ndx = 0; ndx < paramTypes.length; ndx++) {
final Class paramType = paramTypes[ndx];
// lazy init to postpone bytecode usage, when method has no arguments
if (methodParamNames == null) {
methodParamNames = actionMethodParamNameResolver.resolveParamNames(actionClassMethod);
}
final String paramName = methodParamNames[ndx];
final Annotation[] parameterAnnotations = paramAnns[ndx];
final ScopeData paramsScopeData = scopeDataInspector.inspectMethodParameterScopes(paramName, paramType, parameterAnnotations);
MapperFunction mapperFunction = null;
for (final Annotation annotation : parameterAnnotations) {
if (annotation instanceof Mapper) {
mapperFunction = MapperFunctionInstances.get().lookup(((Mapper) annotation).value());
break;
}
}
params[ndx] = new MethodParam(paramTypes[ndx], paramName, scopeDataInspector.detectAnnotationType(parameterAnnotations), paramsScopeData, mapperFunction);
}
return new ActionRuntime(null, actionClass, actionClassMethod, filters, interceptors, actionDefinition, actionResult, defaultActionResult, async, auth, scopeData, params);
}
use of jodd.madvoc.config.ScopeData in project jodd by oblac.
the class ScopeDataInspector method inspectClassScopes.
/**
* Inspects {@link ScopeData} for given class. The results are not cached, so it should
* be used only dyring configuration-time.
* For cached version, use {@link #inspectClassScopesWithCache(Class)}.
*/
public ScopeData inspectClassScopes(final Class actionClass) {
final ClassDescriptor cd = ClassIntrospector.get().lookup(actionClass);
final PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
final List<InjectionPoint> listIn = new ArrayList<>(allProperties.length);
final List<InjectionPoint> listOut = new ArrayList<>(allProperties.length);
for (final PropertyDescriptor pd : allProperties) {
// collect annotations
Class<? extends MadvocScope> scope = null;
In in = null;
Out out = null;
if (pd.getFieldDescriptor() != null) {
final Field field = pd.getFieldDescriptor().getField();
in = field.getAnnotation(In.class);
out = field.getAnnotation(Out.class);
scope = resolveScopeClassFromAnnotations(field.getAnnotations());
}
if (pd.getWriteMethodDescriptor() != null) {
final Method method = pd.getWriteMethodDescriptor().getMethod();
if (in == null) {
in = method.getAnnotation(In.class);
}
if (out == null) {
out = method.getAnnotation(Out.class);
}
if (scope == null) {
scope = resolveScopeClassFromAnnotations(method.getAnnotations());
}
}
if (pd.getReadMethodDescriptor() != null) {
final Method method = pd.getReadMethodDescriptor().getMethod();
if (in == null) {
in = method.getAnnotation(In.class);
}
if (out == null) {
out = method.getAnnotation(Out.class);
}
if (scope == null) {
scope = resolveScopeClassFromAnnotations(method.getAnnotations());
}
}
// inspect all
final InjectionPoint ii = in == null ? null : buildInjectionPoint(in.value(), in.defaultValue(), pd.getName(), pd.getType(), scope);
if (ii != null) {
listIn.add(ii);
}
final InjectionPoint oi = out == null ? null : buildInjectionPoint(out.value(), null, pd.getName(), pd.getType(), scope);
if (oi != null) {
listOut.add(oi);
}
}
if ((listIn.isEmpty()) && (listOut.isEmpty())) {
return NO_SCOPE_DATA;
}
InjectionPoint[] in = null;
InjectionPoint[] out = null;
if (!listIn.isEmpty()) {
in = listIn.toArray(new InjectionPoint[0]);
}
if (!listOut.isEmpty()) {
out = listOut.toArray(new InjectionPoint[0]);
}
return new ScopeData(this, in, out);
}
use of jodd.madvoc.config.ScopeData in project jodd by oblac.
the class ScopeDataInspector method inspectMethodParameterScopes.
// ---------------------------------------------------------------- inspect method
/**
* Inspects {@link ScopeData} parameters for given method parameter information.
*/
public ScopeData inspectMethodParameterScopes(final String name, final Class type, final Annotation[] annotations) {
In in = null;
Out out = null;
for (final Annotation annotation : annotations) {
if (annotation instanceof In) {
in = (In) annotation;
} else if (annotation instanceof Out) {
out = (Out) annotation;
}
}
final Class<? extends MadvocScope> scope = resolveScopeClassFromAnnotations(annotations);
int count = 0;
InjectionPoint[] ins = null;
InjectionPoint[] outs = null;
if (in != null) {
final InjectionPoint scopeDataIn = buildInjectionPoint(in.value(), in.defaultValue(), name, type, scope);
if (scopeDataIn != null) {
count++;
ins = new InjectionPoint[] { scopeDataIn };
}
}
if (out != null) {
final InjectionPoint scopeDataOut = buildInjectionPoint(out.value(), null, name, type, scope);
if (scopeDataOut != null) {
count++;
outs = new InjectionPoint[] { scopeDataOut };
}
}
if (count == 0) {
return NO_SCOPE_DATA;
}
return new ScopeData(this, ins, outs);
}
Aggregations