Search in sources :

Example 11 with Instantiator

use of org.apache.tapestry5.internal.services.Instantiator in project tapestry-5 by apache.

the class EmbeddedComponentAssemblerImpl method prescanMixins.

private String prescanMixins(boolean strictMixinParameters) {
    // Mixin id found to support informal parameters
    String supportsInformals = null;
    for (Map.Entry<String, Instantiator> entry : mixinIdToInstantiator.entrySet()) {
        String mixinId = entry.getKey();
        ComponentModel mixinModel = entry.getValue().getModel();
        updateParameterNameToQualified(mixinId, mixinModel, strictMixinParameters);
        if (supportsInformals == null && mixinModel.getSupportsInformalParameters())
            supportsInformals = mixinId;
    }
    // The component comes last and overwrites simple names from the others.
    updateParameterNameToQualified(null, componentModel, false);
    return supportsInformals;
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) Instantiator(org.apache.tapestry5.internal.services.Instantiator) Map(java.util.Map)

Example 12 with Instantiator

use of org.apache.tapestry5.internal.services.Instantiator in project tapestry-5 by apache.

the class EmbeddedComponentAssemblerImpl method createParameterBinderFromQualifiedParameterName.

private ParameterBinder createParameterBinderFromQualifiedParameterName(String qualifiedParameterName, String mixinId, String parameterName) {
    if (mixinId.equalsIgnoreCase(componentPsuedoMixinId)) {
        return createParameterBinderForComponent(qualifiedParameterName, parameterName);
    }
    if (!mixinIdToInstantiator.containsKey(mixinId)) {
        throw new TapestryException(String.format("Mixin id for parameter '%s' not found. Attached mixins: %s.", qualifiedParameterName, InternalUtils.joinSorted(mixinIdToInstantiator.keySet())), location, null);
    }
    ParameterBinder binder = parameterNameToBinder.get(qualifiedParameterName);
    if (binder != null) {
        return binder;
    }
    // Ok, so perhaps this is a qualified name for an informal parameter of the mixin.
    Instantiator instantiator = mixinIdToInstantiator.get(mixinId);
    assert instantiator != null;
    return bindInformalParameter(qualifiedParameterName, mixinId, parameterName, instantiator.getModel());
}
Also used : Instantiator(org.apache.tapestry5.internal.services.Instantiator) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 13 with Instantiator

use of org.apache.tapestry5.internal.services.Instantiator in project tapestry-5 by apache.

the class ComponentInstantiatorSourceImplTest method createComponent.

private Component createComponent(String classname) {
    InternalComponentResources resources = mockInternalComponentResources();
    replay();
    Instantiator inst = source.getInstantiator(classname);
    Component target = inst.newInstance(resources);
    verify();
    return target;
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) BasicComponent(org.apache.tapestry5.internal.t5internal.pages.BasicComponent) Component(org.apache.tapestry5.runtime.Component)

Example 14 with Instantiator

use of org.apache.tapestry5.internal.services.Instantiator 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)

Example 15 with Instantiator

use of org.apache.tapestry5.internal.services.Instantiator in project tapestry-5 by apache.

the class InternalComponentResourcesImplTest method render_informal_parameters_no_bindings.

@Test
public void render_informal_parameters_no_bindings() {
    ComponentPageElement element = mockComponentPageElement();
    Component component = mockComponent();
    Instantiator ins = mockInstantiator(component);
    MarkupWriter writer = mockMarkupWriter();
    TypeCoercer coercer = mockTypeCoercer();
    ComponentModel model = mockComponentModel();
    train_getModel(ins, model);
    replay();
    InternalComponentResources resources = new InternalComponentResourcesImpl(null, element, null, elementResources, null, null, ins, false);
    resources.renderInformalParameters(writer);
    verify();
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) ComponentModel(org.apache.tapestry5.model.ComponentModel) Instantiator(org.apache.tapestry5.internal.services.Instantiator) Component(org.apache.tapestry5.runtime.Component) MarkupWriter(org.apache.tapestry5.MarkupWriter) Test(org.testng.annotations.Test)

Aggregations

Instantiator (org.apache.tapestry5.internal.services.Instantiator)15 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)11 ComponentModel (org.apache.tapestry5.model.ComponentModel)11 Component (org.apache.tapestry5.runtime.Component)10 Test (org.testng.annotations.Test)9 InternalPropBinding (org.apache.tapestry5.internal.bindings.InternalPropBinding)4 Method (java.lang.reflect.Method)3 Binding (org.apache.tapestry5.Binding)3 MarkupWriter (org.apache.tapestry5.MarkupWriter)3 Map (java.util.Map)2 ComponentResources (org.apache.tapestry5.ComponentResources)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 EmbeddedComponentModel (org.apache.tapestry5.model.EmbeddedComponentModel)2 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)2 PlasticClassTransformer (org.apache.tapestry5.plastic.PlasticClassTransformer)2 PlasticField (org.apache.tapestry5.plastic.PlasticField)2 IFn (clojure.lang.IFn)1 Symbol (clojure.lang.Symbol)1 Namespace (org.apache.tapestry5.clojure.Namespace)1 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)1