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