Search in sources :

Example 16 with PlasticClass

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

the class PersistenceContextWorker method transform.

@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (final PlasticField field : plasticClass.getFieldsWithAnnotation(PersistenceContext.class)) {
        final PersistenceContext annotation = field.getAnnotation(PersistenceContext.class);
        field.claim(annotation);
        field.setConduit(new ReadOnlyComponentFieldConduit(plasticClass.getClassName(), field.getName()) {

            @Override
            public Object get(Object instance, InstanceContext context) {
                return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
            }
        });
    }
}
Also used : ReadOnlyComponentFieldConduit(org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit) InstanceContext(org.apache.tapestry5.plastic.InstanceContext) PlasticField(org.apache.tapestry5.plastic.PlasticField) PersistenceContext(javax.persistence.PersistenceContext)

Example 17 with PlasticClass

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

the class StrategyBuilderImpl method createProxy.

private <S> S createProxy(final Class<S> interfaceType, final StrategyRegistry<S> registry) {
    ClassInstantiator instantiator = proxyFactory.createProxy(interfaceType, new PlasticClassTransformer() {

        @Override
        public void transform(PlasticClass plasticClass) {
            final PlasticField registryField = plasticClass.introduceField(StrategyRegistry.class, "registry").inject(registry);
            Class<?> interfaceSelectorType = null;
            for (final Method method : interfaceType.getMethods()) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 0) {
                    throw new IllegalArgumentException("Invalid method " + method + ", when using the strategy pattern, every method must take at least the selector as its parameter");
                }
                Class<?> methodSelectorType = parameterTypes[0];
                if (interfaceSelectorType == null) {
                    interfaceSelectorType = methodSelectorType;
                } else if (!interfaceSelectorType.equals(methodSelectorType)) {
                    throw new IllegalArgumentException("Conflicting method definitions," + " expecting the first argument of every method to have the same type");
                }
                plasticClass.introduceMethod(new MethodDescription(method), new InstructionBuilderCallback() {

                    @Override
                    public void doBuild(InstructionBuilder builder) {
                        Class returnType = method.getReturnType();
                        builder.loadThis().getField(registryField);
                        // Argument 0 is the selector used to find the adapter and should be an object reference,
                        // not a primitive.
                        builder.loadArgument(0);
                        // Use the StrategyRegistry to get the adapter to re-invoke the method on
                        builder.invoke(StrategyRegistry.class, Object.class, "getByInstance", Object.class).checkcast(interfaceType);
                        // That leaves the correct adapter on top of the stack. Get the
                        // selector and the rest of the arguments in place and invoke the method.
                        builder.loadArguments().invoke(interfaceType, returnType, method.getName(), method.getParameterTypes());
                        builder.returnResult();
                    }
                });
            }
            plasticClass.addToString(String.format("<Strategy for %s>", interfaceType.getName()));
        }
    });
    return interfaceType.cast(instantiator.newInstance());
}
Also used : PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) ClassInstantiator(org.apache.tapestry5.plastic.ClassInstantiator) StrategyRegistry(org.apache.tapestry5.commons.util.StrategyRegistry) MethodDescription(org.apache.tapestry5.plastic.MethodDescription) Method(java.lang.reflect.Method) PlasticClassTransformer(org.apache.tapestry5.plastic.PlasticClassTransformer) PlasticField(org.apache.tapestry5.plastic.PlasticField) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 18 with PlasticClass

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

the class ChainBuilderImpl method implementMethod.

private void implementMethod(PlasticClass plasticClass, final Method method, final PlasticField commandsField) {
    plasticClass.introduceMethod(method).changeImplementation(new InstructionBuilderCallback() {

        @Override
        public void doBuild(InstructionBuilder builder) {
            builder.loadThis().getField(commandsField).iterateArray(new InstructionBuilderCallback() {

                @Override
                public void doBuild(InstructionBuilder builder) {
                    // The command is on the stack; add the elements and invoke the method.
                    builder.loadArguments().invoke(method);
                    Class returnType = method.getReturnType();
                    if (returnType == void.class)
                        return;
                    final boolean wide = returnType == long.class || returnType == double.class;
                    if (wide)
                        builder.dupeWide();
                    else
                        builder.dupe();
                    if (returnType == float.class) {
                        builder.loadConstant(0f).compareSpecial("float");
                    }
                    if (returnType == long.class) {
                        builder.loadConstant(0l).compareSpecial("long");
                    }
                    if (returnType == double.class) {
                        builder.loadConstant(0d).compareSpecial("double");
                    }
                    Condition condition = returnType.isPrimitive() ? Condition.NON_ZERO : Condition.NON_NULL;
                    builder.when(condition, new WhenCallback() {

                        @Override
                        public void ifTrue(InstructionBuilder builder) {
                            builder.returnResult();
                        }

                        @Override
                        public void ifFalse(InstructionBuilder builder) {
                            if (wide)
                                builder.popWide();
                            else
                                builder.pop();
                        }
                    });
                }
            });
            builder.returnDefaultValue();
        }
    });
}
Also used : Condition(org.apache.tapestry5.plastic.Condition) WhenCallback(org.apache.tapestry5.plastic.WhenCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 19 with PlasticClass

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

the class ChainBuilderImpl method build.

@Override
@SuppressWarnings("unchecked")
public <T> T build(final Class<T> commandInterface, List<T> commands) {
    // Jump through some hoops to convert the list into an array of the proper type
    Object[] array = (Object[]) Array.newInstance(commandInterface, commands.size());
    final Object[] commandsArray = commands.toArray(array);
    ClassInstantiator<T> instantiator = proxyFactory.createProxy(commandInterface, new PlasticClassTransformer() {

        @Override
        public void transform(PlasticClass plasticClass) {
            PlasticField commandsField = plasticClass.introduceField(commandsArray.getClass(), "commands").inject(commandsArray);
            for (Method method : commandInterface.getMethods()) {
                if (!Modifier.isStatic(method.getModifiers())) {
                    implementMethod(plasticClass, method, commandsField);
                }
            }
            plasticClass.addToString(String.format("<Command chain of %s>", commandInterface.getName()));
        }
    });
    return instantiator.newInstance();
}
Also used : PlasticClass(org.apache.tapestry5.plastic.PlasticClass) PlasticClassTransformer(org.apache.tapestry5.plastic.PlasticClassTransformer) PlasticField(org.apache.tapestry5.plastic.PlasticField) Method(java.lang.reflect.Method)

Example 20 with PlasticClass

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

the class CDIAnnotationWorker method transform.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.tapestry5.services.transform.ComponentClassTransformWorker2
	 * #transform(org.apache.tapestry5.plastic.PlasticClass,
	 * org.apache.tapestry5.services.transform.TransformationSupport,
	 * org.apache.tapestry5.model.MutableComponentModel)
	 */
@Override
public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) {
    for (PlasticField field : plasticClass.getFieldsWithAnnotation(CDI.class)) {
        final CDI annotation = field.getAnnotation(CDI.class);
        Class<?> type = cache.forName(field.getTypeName());
        final Object injectionValue = cdiFactory.get(type);
        if (injectionValue != null) {
            field.inject(injectionValue);
            field.claim(annotation);
        }
    }
}
Also used : PlasticField(org.apache.tapestry5.plastic.PlasticField) CDI(com.flowlogix.web.services.annotations.CDI)

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