001// Copyright 2006, 2007, 2008, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.ioc.internal.services;
016
017import java.lang.reflect.Method;
018import java.util.Map;
019
020import org.apache.tapestry5.ioc.services.Builtin;
021import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
022import org.apache.tapestry5.ioc.services.StrategyBuilder;
023import org.apache.tapestry5.ioc.util.StrategyRegistry;
024import org.apache.tapestry5.plastic.ClassInstantiator;
025import org.apache.tapestry5.plastic.InstructionBuilder;
026import org.apache.tapestry5.plastic.InstructionBuilderCallback;
027import org.apache.tapestry5.plastic.MethodDescription;
028import org.apache.tapestry5.plastic.PlasticClass;
029import org.apache.tapestry5.plastic.PlasticClassTransformer;
030import org.apache.tapestry5.plastic.PlasticField;
031
032public class StrategyBuilderImpl implements StrategyBuilder
033{
034    private final PlasticProxyFactory proxyFactory;
035
036    public StrategyBuilderImpl(@Builtin
037    PlasticProxyFactory proxyFactory)
038    {
039        this.proxyFactory = proxyFactory;
040    }
041
042    @Override
043    public <S> S build(StrategyRegistry<S> registry)
044    {
045        return createProxy(registry.getAdapterType(), registry);
046    }
047
048    @Override
049    public <S> S build(Class<S> adapterType, Map<Class, S> registrations)
050    {
051        StrategyRegistry<S> registry = StrategyRegistry.newInstance(adapterType, registrations);
052
053        return build(registry);
054    }
055
056    private <S> S createProxy(final Class<S> interfaceType, final StrategyRegistry<S> registry)
057    {
058        ClassInstantiator instantiator = proxyFactory.createProxy(interfaceType, new PlasticClassTransformer()
059        {
060            @Override
061            public void transform(PlasticClass plasticClass)
062            {
063                final PlasticField registryField = plasticClass.introduceField(StrategyRegistry.class, "registry")
064                        .inject(registry);
065
066                for (final Method method : interfaceType.getMethods())
067                {
068                    plasticClass.introduceMethod(new MethodDescription(method), new InstructionBuilderCallback()
069                    {
070                        @Override
071                        public void doBuild(InstructionBuilder builder)
072                        {
073                            Class returnType = method.getReturnType();
074
075                            builder.loadThis().getField(registryField);
076
077                            // Argument 0 is the selector used to find the adapter and should be an object reference,
078                            // not a primitive.
079
080                            builder.loadArgument(0);
081
082                            // Use the StrategyRegistry to get the adapter to re-invoke the method on
083                            builder.invoke(StrategyRegistry.class, Object.class, "getByInstance", Object.class)
084                                    .checkcast(interfaceType);
085
086                            // That leaves the correct adapter on top of the stack. Get the
087                            // selector and the rest of the arguments in place and invoke the method.
088
089                            builder.loadArguments().invoke(interfaceType, returnType, method.getName(),
090                                    method.getParameterTypes());
091
092                            builder.returnResult();
093                        }
094                    });
095                }
096
097                plasticClass.addToString(String.format("<Strategy for %s>", interfaceType.getName()));
098            }
099        });
100
101        return interfaceType.cast(instantiator.newInstance());
102    }
103}