Search in sources :

Example 1 with ScopeData

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));
    }
}
Also used : ScopeData(jodd.madvoc.config.ScopeData) ServletContext(javax.servlet.ServletContext) Targets(jodd.madvoc.config.Targets)

Example 2 with ScopeData

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());
}
Also used : InjectionPoint(jodd.madvoc.config.InjectionPoint) ScopeData(jodd.madvoc.config.ScopeData) PetiteContainer(jodd.petite.PetiteContainer) Test(org.junit.jupiter.api.Test)

Example 3 with ScopeData

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);
}
Also used : MethodParam(jodd.madvoc.config.MethodParam) MapperFunction(jodd.introspector.MapperFunction) ActionRuntime(jodd.madvoc.config.ActionRuntime) Annotation(java.lang.annotation.Annotation) Mapper(jodd.introspector.Mapper) ScopeData(jodd.madvoc.config.ScopeData)

Example 4 with ScopeData

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);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) InjectionPoint(jodd.madvoc.config.InjectionPoint) In(jodd.madvoc.meta.In) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Out(jodd.madvoc.meta.Out) Field(java.lang.reflect.Field) ScopeData(jodd.madvoc.config.ScopeData)

Example 5 with ScopeData

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);
}
Also used : In(jodd.madvoc.meta.In) InjectionPoint(jodd.madvoc.config.InjectionPoint) ScopeData(jodd.madvoc.config.ScopeData) Annotation(java.lang.annotation.Annotation) InjectionPoint(jodd.madvoc.config.InjectionPoint) Out(jodd.madvoc.meta.Out)

Aggregations

ScopeData (jodd.madvoc.config.ScopeData)6 InjectionPoint (jodd.madvoc.config.InjectionPoint)4 Annotation (java.lang.annotation.Annotation)2 In (jodd.madvoc.meta.In)2 Out (jodd.madvoc.meta.Out)2 PetiteContainer (jodd.petite.PetiteContainer)2 Test (org.junit.jupiter.api.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 ClassDescriptor (jodd.introspector.ClassDescriptor)1 Mapper (jodd.introspector.Mapper)1 MapperFunction (jodd.introspector.MapperFunction)1 PropertyDescriptor (jodd.introspector.PropertyDescriptor)1 ActionRuntime (jodd.madvoc.config.ActionRuntime)1 MethodParam (jodd.madvoc.config.MethodParam)1 Targets (jodd.madvoc.config.Targets)1