Search in sources :

Example 6 with Instantiator

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

the class InternalBaseTestCase method mockInstantiator.

protected final Instantiator mockInstantiator(Component component) {
    Instantiator ins = newMock(Instantiator.class);
    expect(ins.newInstance(EasyMock.isA(InternalComponentResources.class))).andReturn(component);
    return ins;
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) Instantiator(org.apache.tapestry5.internal.services.Instantiator)

Example 7 with Instantiator

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

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

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

the class ComponentAssemblerImpl method addRootComponentMixins.

private void addRootComponentMixins(PageAssembly assembly, ComponentPageElement element) {
    for (String className : instantiator.getModel().getMixinClassNames()) {
        assembly.weight++;
        Instantiator mixinInstantiator = instantiatorSource.getInstantiator(className);
        ComponentModel model = instantiator.getModel();
        element.addMixin(InternalUtils.lastTerm(className), mixinInstantiator, model.getOrderForMixin(className));
    }
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) Instantiator(org.apache.tapestry5.internal.services.Instantiator)

Example 10 with Instantiator

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

the class EmbeddedComponentAssemblerImpl method addMixin.

private void addMixin(String className, String... order) {
    Instantiator mixinInstantiator = instantiatorSource.getInstantiator(className);
    String mixinId = InternalUtils.lastTerm(className);
    if (mixinIdToInstantiator.containsKey(mixinId))
        throw new TapestryException(String.format("Mixins applied to a component must be unique. Mixin '%s' has already been applied.", mixinId), location, null);
    mixinIdToInstantiator.put(mixinId, mixinInstantiator);
    mixinsIdToOrderConstraints.put(mixinId, order);
}
Also used : Instantiator(org.apache.tapestry5.internal.services.Instantiator) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

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