001// Copyright 2008, 2009, 2010, 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 org.apache.tapestry5.beanmodel.services.PlasticProxyFactoryImpl;
018import org.apache.tapestry5.commons.services.PlasticProxyFactory;
019import org.apache.tapestry5.commons.util.CollectionFactory;
020import org.apache.tapestry5.ioc.AnnotationAccess;
021import org.apache.tapestry5.plastic.InstructionBuilder;
022import org.apache.tapestry5.plastic.InstructionBuilderCallback;
023import org.apache.tapestry5.plastic.MethodDescription;
024import org.apache.tapestry5.plastic.PlasticClass;
025import org.apache.tapestry5.plastic.PlasticClassTransformation;
026import org.apache.tapestry5.plastic.PlasticField;
027
028import java.lang.reflect.Method;
029import java.util.Arrays;
030import java.util.Set;
031
032@SuppressWarnings("all")
033public class AspectInterceptorBuilderImpl<T> extends AbtractAspectInterceptorBuilder<T>
034{
035    private final Class<T> serviceInterface;
036
037    private final Set<Method> allMethods = CollectionFactory.newSet();
038
039    private final PlasticClassTransformation transformation;
040
041    private final PlasticClass plasticClass;
042
043    public AspectInterceptorBuilderImpl(AnnotationAccess annotationAccess, PlasticProxyFactory plasticProxyFactory,
044                                        Class<T> serviceInterface, T delegate, String description)
045    {
046        super(annotationAccess);
047
048        this.serviceInterface = serviceInterface;
049
050        final Class<? extends Object> delegateType = delegate.getClass();
051        transformation = plasticProxyFactory.createProxyTransformation(serviceInterface, (Class<? extends T>) delegateType);
052        plasticClass = transformation.getPlasticClass();
053
054        plasticClass.addToString(description);
055
056        allMethods.addAll(Arrays.asList(serviceInterface.getMethods()));
057
058        final PlasticField delegateField = plasticClass.introduceField(serviceInterface, "delegate").inject(delegate);
059
060        for (Method method : allMethods)
061        {
062            plasticClass.introduceMethod(method).delegateTo(delegateField);
063        }
064        
065        // TAP5-2235
066        final String delegateTypeName = delegateType.getName();
067        MethodDescription getDelegateMethodDescription = 
068                new MethodDescription(delegateTypeName, PlasticProxyFactoryImpl.INTERNAL_GET_DELEGATE);
069        plasticClass.introduceMethod(getDelegateMethodDescription, new InstructionBuilderCallback()
070        {
071            @Override
072            public void doBuild(InstructionBuilder builder)
073            {
074                builder.loadThis().getField(delegateField);
075                builder.checkcast(delegateType).returnResult();
076            }
077        });
078    }
079
080    @Override
081    public void adviseMethod(Method method, org.apache.tapestry5.plastic.MethodAdvice advice)
082    {
083        assert method != null;
084        assert advice != null;
085
086        if (!allMethods.contains(method))
087            throw new IllegalArgumentException(String.format("Method %s is not defined for interface %s.", method,
088                    serviceInterface));
089
090        plasticClass.introduceMethod(method).addAdvice(advice);
091    }
092
093    @Override
094    public void adviseAllMethods(org.apache.tapestry5.plastic.MethodAdvice advice)
095    {
096        for (Method m : serviceInterface.getMethods())
097        {
098            adviseMethod(m, advice);
099        }
100    }
101
102    @Override
103    public Class getInterface()
104    {
105        return serviceInterface;
106    }
107
108    @Override
109    public T build()
110    {
111        return (T) transformation.createInstantiator().newInstance();
112    }
113}