Search in sources :

Example 21 with PlasticField

use of org.apache.tapestry5.plastic.PlasticField 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)

Example 22 with PlasticField

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

the class ComponentInstantiatorSourceImpl method implementComponentInterface.

private void implementComponentInterface(PlasticClass plasticClass) {
    plasticClass.introduceInterface(Component.class);
    final PlasticField resourcesField = plasticClass.introduceField(InternalComponentResources.class, "internalComponentResources").injectFromInstanceContext();
    plasticClass.introduceMethod(GET_COMPONENT_RESOURCES, new InstructionBuilderCallback() {

        public void doBuild(InstructionBuilder builder) {
            builder.loadThis().getField(resourcesField).returnResult();
        }
    });
}
Also used : InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) PlasticField(org.apache.tapestry5.plastic.PlasticField) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 23 with PlasticField

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

the class BlockInjectionProvider method provideInjection.

public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel) {
    if (!field.getTypeName().equals(BLOCK_TYPE_NAME)) {
        return false;
    }
    Id annotation = field.getAnnotation(Id.class);
    String blockId = getBlockId(field.getName(), annotation);
    FieldConduit<Object> conduit = createConduit(field, blockId);
    field.setConduit(conduit);
    // claim the field
    return true;
}
Also used : Id(org.apache.tapestry5.annotations.Id)

Example 24 with PlasticField

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

the class ThunkCreatorImpl method createInstantiator.

private <T> ClassInstantiator<T> createInstantiator(final Class<T> interfaceType) {
    return proxyFactory.createProxy(interfaceType, new PlasticClassTransformer() {

        @Override
        public void transform(PlasticClass plasticClass) {
            final PlasticField objectCreatorField = plasticClass.introduceField(ObjectCreator.class, "creator").injectFromInstanceContext();
            PlasticMethod delegateMethod = plasticClass.introducePrivateMethod(interfaceType.getName(), "delegate", null, null);
            delegateMethod.changeImplementation(new InstructionBuilderCallback() {

                @Override
                public void doBuild(InstructionBuilder builder) {
                    builder.loadThis().getField(objectCreatorField);
                    builder.invoke(CREATE_OBJECT);
                    builder.checkcast(interfaceType).returnResult();
                }
            });
            for (Method method : interfaceType.getMethods()) {
                plasticClass.introduceMethod(method).delegateTo(delegateMethod);
            }
            if (!plasticClass.isMethodImplemented(PlasticUtils.TO_STRING_DESCRIPTION)) {
                final PlasticField descriptionField = plasticClass.introduceField(String.class, "description").injectFromInstanceContext();
                plasticClass.introduceMethod(PlasticUtils.TO_STRING_DESCRIPTION, new InstructionBuilderCallback() {

                    @Override
                    public void doBuild(InstructionBuilder builder) {
                        builder.loadThis().getField(descriptionField).returnResult();
                    }
                });
            }
        }
    });
}
Also used : PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) PlasticClassTransformer(org.apache.tapestry5.plastic.PlasticClassTransformer) PlasticField(org.apache.tapestry5.plastic.PlasticField) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) Method(java.lang.reflect.Method) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 25 with PlasticField

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

the class ClojureBuilderImpl method build.

@Override
public <T> T build(final Class<T> interfaceType) {
    assert interfaceType != null;
    assert interfaceType.isInterface();
    Namespace annotation = interfaceType.getAnnotation(Namespace.class);
    if (annotation == null) {
        throw new IllegalArgumentException(String.format("Interface type %s does not have the Namespace annotation.", interfaceType.getName()));
    }
    final String namespace = annotation.value();
    ClassInstantiator<T> instantiator = proxyFactory.createProxy(interfaceType, new PlasticClassTransformer() {

        @Override
        public void transform(PlasticClass plasticClass) {
            for (final Method m : interfaceType.getMethods()) {
                bridgeToClojure(plasticClass, m);
            }
        }

        private void bridgeToClojure(final PlasticClass plasticClass, final Method method) {
            final MethodDescription desc = new MethodDescription(method);
            if (method.getReturnType() == void.class) {
                throw new IllegalArgumentException(String.format("Method %s may not be void when bridging to Clojure functions.", desc));
            }
            final Symbol symbol = mapper.mapMethod(namespace, method);
            tracker.run(String.format("Mapping %s method %s to Clojure function %s", interfaceType.getName(), desc.toShortString(), symbol.toString()), new Runnable() {

                @Override
                public void run() {
                    Symbol namespaceSymbol = Symbol.create(symbol.getNamespace());
                    REQUIRE.invoke(namespaceSymbol);
                    IFn clojureFunction = Clojure.var(symbol);
                    final PlasticField fnField = plasticClass.introduceField(IFn.class, method.getName() + "IFn").inject(clojureFunction);
                    plasticClass.introduceMethod(desc).changeImplementation(new InstructionBuilderCallback() {

                        @Override
                        public void doBuild(InstructionBuilder builder) {
                            bridgeToClojure(builder, desc, fnField);
                        }
                    });
                }
            });
        }

        private void bridgeToClojure(InstructionBuilder builder, MethodDescription description, PlasticField ifnField) {
            builder.loadThis().getField(ifnField);
            int count = description.argumentTypes.length;
            Class[] invokeParameterTypes = new Class[count];
            for (int i = 0; i < count; i++) {
                invokeParameterTypes[i] = Object.class;
                builder.loadArgument(i).boxPrimitive(description.argumentTypes[i]);
            }
            Method ifnMethod = null;
            try {
                ifnMethod = IFn.class.getMethod("invoke", invokeParameterTypes);
            } catch (NoSuchMethodException ex) {
                throw new RuntimeException(String.format("Unable to find correct IFn.invoke() method: %s", ExceptionUtils.toMessage(ex)), ex);
            }
            builder.invoke(ifnMethod);
            builder.castOrUnbox(description.returnType);
            builder.returnResult();
        }
    });
    return instantiator.newInstance();
}
Also used : Symbol(clojure.lang.Symbol) Method(java.lang.reflect.Method) Namespace(org.apache.tapestry5.clojure.Namespace) IFn(clojure.lang.IFn)

Aggregations

PlasticField (org.apache.tapestry5.plastic.PlasticField)15 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)7 ComponentResources (org.apache.tapestry5.ComponentResources)5 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)5 InstanceContext (org.apache.tapestry5.plastic.InstanceContext)5 Method (java.lang.reflect.Method)4 InstructionBuilder (org.apache.tapestry5.plastic.InstructionBuilder)4 InstructionBuilderCallback (org.apache.tapestry5.plastic.InstructionBuilderCallback)4 PlasticClassTransformer (org.apache.tapestry5.plastic.PlasticClassTransformer)3 PageActivationContext (org.apache.tapestry5.annotations.PageActivationContext)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 ReadOnlyComponentFieldConduit (org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit)2 ComputedValue (org.apache.tapestry5.plastic.ComputedValue)2 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)2 IFn (clojure.lang.IFn)1 Symbol (clojure.lang.Symbol)1 CDI (com.flowlogix.web.services.annotations.CDI)1 Stateful (com.flowlogix.web.services.annotations.Stateful)1 Annotation (java.lang.annotation.Annotation)1 EJB (javax.ejb.EJB)1