Search in sources :

Example 1 with InjectionPoint

use of jodd.madvoc.config.InjectionPoint 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 2 with InjectionPoint

use of jodd.madvoc.config.InjectionPoint 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 3 with InjectionPoint

use of jodd.madvoc.config.InjectionPoint 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)

Example 4 with InjectionPoint

use of jodd.madvoc.config.InjectionPoint in project jodd by oblac.

the class ScopeDataInspectorTest method testInAnnotations.

@Test
void testInAnnotations() {
    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(Action.class);
    InjectionPoint[] in1 = scopeData.in();
    InjectionPoint in = in1[0];
    assertEquals("input", in.name());
    assertEquals(String.class, in.type());
}
Also used : InjectionPoint(jodd.madvoc.config.InjectionPoint) ScopeData(jodd.madvoc.config.ScopeData) PetiteContainer(jodd.petite.PetiteContainer) Test(org.junit.jupiter.api.Test)

Aggregations

InjectionPoint (jodd.madvoc.config.InjectionPoint)4 ScopeData (jodd.madvoc.config.ScopeData)4 In (jodd.madvoc.meta.In)2 Out (jodd.madvoc.meta.Out)2 PetiteContainer (jodd.petite.PetiteContainer)2 Test (org.junit.jupiter.api.Test)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ClassDescriptor (jodd.introspector.ClassDescriptor)1 PropertyDescriptor (jodd.introspector.PropertyDescriptor)1