Search in sources :

Example 1 with PlasticClass

use of org.apache.tapestry5.plastic.PlasticClass in project flowlogix by flowlogix.

the class AjaxAnnotationWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    boolean hasTrackerMixin = model.getMixinClassNames().contains(SessionTracker.class.getName());
    for (PlasticMethod method : plasticClass.getMethodsWithAnnotation(AJAX.class)) {
        final AJAX annotation = method.getAnnotation(AJAX.class);
        final boolean isVoid = method.isVoid();
        if (annotation.requireSession() && (!hasTrackerMixin)) {
            model.addMixinClassName(SessionTracker.class.getName());
            hasTrackerMixin = true;
        }
        method.addAdvice(new MethodAdvice() {

            @Override
            @SneakyThrows(IOException.class)
            public void advise(MethodInvocation invocation) {
                if (!request.isXHR() || annotation.requireSession() == false) {
                    invocation.proceed();
                } else {
                    // do not invoke on bad sessions
                    if (SessionTrackerUtil.isValidSession(rg.getActivePageName(), rg.getRequest().getSession(false))) {
                        invocation.proceed();
                    } else {
                        showSessionExpiredMessage = true;
                        SessionTrackerUtil.redirectToSelf(rg, linkSource);
                        if (!isVoid) {
                            invocation.setReturnValue(null);
                        }
                        return;
                    }
                }
                Object result = null;
                if (!isVoid) {
                    result = invocation.getReturnValue();
                }
                if (!request.isXHR()) {
                    if (result != null) {
                        result = defaultForReturnType(result.getClass());
                    }
                } else {
                    if (annotation.discardAfter()) {
                        cs.getActivePage().getComponentResources().discardPersistentFieldChanges();
                    }
                }
                if (!isVoid) {
                    invocation.setReturnValue(result);
                }
            }
        });
    }
}
Also used : SneakyThrows(lombok.SneakyThrows) MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) SessionTracker(com.flowlogix.web.mixins.SessionTracker) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) IOException(java.io.IOException) AJAX(com.flowlogix.web.services.annotations.AJAX)

Example 2 with PlasticClass

use of org.apache.tapestry5.plastic.PlasticClass in project flowlogix by flowlogix.

the class EJBAnnotationWorker method transform.

@Override
@SneakyThrows({ NamingException.class })
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (PlasticField field : plasticClass.getFieldsWithAnnotation(EJB.class)) {
        final EJB annotation = field.getAnnotation(EJB.class);
        final Stateful stateful = field.getAnnotation(Stateful.class);
        final String fieldType = field.getTypeName();
        final String fieldName = field.getName();
        final String mappedName = annotation.mappedName();
        final JNDIObjectLocator locator = JNDIObjectLocator.builder().build();
        final String lookupname = getLookupName(annotation, fieldType, locator);
        Object injectionValue = lookupBean(field, fieldType, fieldName, lookupname, mappedName, stateful, locator);
        if (injectionValue != null) {
            field.claim(annotation);
        }
    }
}
Also used : Stateful(com.flowlogix.web.services.annotations.Stateful) PlasticField(org.apache.tapestry5.plastic.PlasticField) EJB(javax.ejb.EJB) JNDIObjectLocator(org.omnifaces.util.JNDIObjectLocator) SneakyThrows(lombok.SneakyThrows)

Example 3 with PlasticClass

use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.

the class MethodAdviceManager method createNewMethod.

private void createNewMethod() {
    String[] exceptions = advisedMethodNode.exceptions == null ? null : advisedMethodNode.exceptions.toArray(new String[0]);
    // Remove the private flag, so that the MethodInvocation implementation (in the same package)
    // can directly access the method without an additional access method.
    MethodNode mn = new MethodNode(advisedMethodNode.access & ~Opcodes.ACC_PRIVATE, newMethodName, advisedMethodNode.desc, advisedMethodNode.signature, exceptions);
    // Copy everything else about the advisedMethodNode over to the new node
    advisedMethodNode.accept(mn);
    // Add this new method, with the same implementation as the original method, to the
    // PlasticClass
    plasticClass.classNode.methods.add(mn);
}
Also used : MethodNode(org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)

Example 4 with PlasticClass

use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.

the class LogWorker method transform.

public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    List<PlasticMethod> methods = plasticClass.getMethodsWithAnnotation(Log.class);
    if (methods.isEmpty()) {
        return;
    }
    final MethodAdvice loggingAdvice = new LoggingAdvice(model.getLogger(), exceptionTracker);
    for (PlasticMethod method : methods) {
        method.addAdvice(loggingAdvice);
    }
}
Also used : LoggingAdvice(org.apache.tapestry5.ioc.internal.services.LoggingAdvice) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Example 5 with PlasticClass

use of org.apache.tapestry5.plastic.PlasticClass in project tapestry-5 by apache.

the class OnEventWorker method implementDispatchMethod.

private void implementDispatchMethod(final PlasticClass plasticClass, final boolean isRoot, final MutableComponentModel model, final Flow<EventHandlerMethod> eventHandlerMethods) {
    plasticClass.introduceMethod(TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION).changeImplementation(new InstructionBuilderCallback() {

        public void doBuild(InstructionBuilder builder) {
            builder.startVariable("boolean", new LocalVariableCallback() {

                public void doBuild(LocalVariable resultVariable, InstructionBuilder builder) {
                    if (!isRoot) {
                        // As a subclass, there will be a base class implementation (possibly empty).
                        builder.loadThis().loadArguments().invokeSpecial(plasticClass.getSuperClassName(), TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION);
                        // First store the result of the super() call into the variable.
                        builder.storeVariable(resultVariable);
                        builder.loadArgument(0).invoke(Event.class, boolean.class, "isAborted");
                        builder.when(Condition.NON_ZERO, RETURN_TRUE);
                    } else {
                        // No event handler method has yet been invoked.
                        builder.loadConstant(false).storeVariable(resultVariable);
                    }
                    boolean hasRestEndpointEventHandlerMethod = false;
                    JSONArray restEndpointEventHandlerMethods = null;
                    for (EventHandlerMethod method : eventHandlerMethods) {
                        method.buildMatchAndInvocation(builder, resultVariable);
                        model.addEventHandler(method.eventType);
                        if (method.handleActivationEventContext) {
                            model.doHandleActivationEventContext();
                        }
                        // We're collecting this info for all components, even considering REST
                        // events are only triggered in pages, because we can have REST event
                        // handler methods in base classes too, and we need this info
                        // for generating complete, correct OpenAPI documentation.
                        final OnEvent onEvent = method.method.getAnnotation(OnEvent.class);
                        final String methodName = method.method.getDescription().methodName;
                        if (isRestEndpointEventHandlerMethod(onEvent, methodName)) {
                            hasRestEndpointEventHandlerMethod = true;
                            if (restEndpointEventHandlerMethods == null) {
                                restEndpointEventHandlerMethods = new JSONArray();
                            }
                            JSONObject methodMeta = new JSONObject();
                            methodMeta.put("name", methodName);
                            JSONArray parameters = new JSONArray();
                            for (MethodParameter parameter : method.method.getParameters()) {
                                parameters.add(parameter.getType());
                            }
                            methodMeta.put("parameters", parameters);
                            restEndpointEventHandlerMethods.add(methodMeta);
                        }
                    }
                    // memory by not setting it to all component models.
                    if (model.isPage()) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHOD_PRESENT, hasRestEndpointEventHandlerMethod ? InternalConstants.TRUE : InternalConstants.FALSE);
                    }
                    // methods in components, something that would be ignored anyway.
                    if (restEndpointEventHandlerMethods != null) {
                        model.setMeta(InternalConstants.REST_ENDPOINT_EVENT_HANDLER_METHODS, restEndpointEventHandlerMethods.toCompactString());
                    }
                    builder.loadVariable(resultVariable).returnResult();
                }
            });
        }
    });
}
Also used : LocalVariableCallback(org.apache.tapestry5.plastic.LocalVariableCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) JSONObject(org.apache.tapestry5.json.JSONObject) LocalVariable(org.apache.tapestry5.plastic.LocalVariable) JSONArray(org.apache.tapestry5.json.JSONArray) MethodParameter(org.apache.tapestry5.plastic.MethodParameter) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback) OnEvent(org.apache.tapestry5.annotations.OnEvent)

Aggregations

PlasticField (org.apache.tapestry5.plastic.PlasticField)13 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)11 InstructionBuilder (org.apache.tapestry5.plastic.InstructionBuilder)5 InstructionBuilderCallback (org.apache.tapestry5.plastic.InstructionBuilderCallback)5 PlasticMethod (org.apache.tapestry5.plastic.PlasticMethod)5 Method (java.lang.reflect.Method)4 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)4 PlasticClassTransformer (org.apache.tapestry5.plastic.PlasticClassTransformer)3 Test (org.testng.annotations.Test)3 PersistenceContext (javax.persistence.PersistenceContext)2 SneakyThrows (lombok.SneakyThrows)2 ComponentResources (org.apache.tapestry5.ComponentResources)2 Import (org.apache.tapestry5.annotations.Import)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)2 MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)2 IFn (clojure.lang.IFn)1 Symbol (clojure.lang.Symbol)1 SessionTracker (com.flowlogix.web.mixins.SessionTracker)1 AJAX (com.flowlogix.web.services.annotations.AJAX)1