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 public void doBuild(InstructionBuilder builder) 071 { 072 builder.loadThis().getField(delegateField); 073 builder.checkcast(delegateType).returnResult(); 074 } 075 }); 076 } 077 078 public void adviseMethod(Method method, org.apache.tapestry5.plastic.MethodAdvice advice) 079 { 080 assert method != null; 081 assert advice != null; 082 083 if (!allMethods.contains(method)) 084 throw new IllegalArgumentException(String.format("Method %s is not defined for interface %s.", method, 085 serviceInterface)); 086 087 plasticClass.introduceMethod(method).addAdvice(advice); 088 } 089 090 public void adviseAllMethods(org.apache.tapestry5.plastic.MethodAdvice advice) 091 { 092 for (Method m : serviceInterface.getMethods()) 093 { 094 adviseMethod(m, advice); 095 } 096 } 097 098 public Class getInterface() 099 { 100 return serviceInterface; 101 } 102 103 public T build() 104 { 105 return (T) transformation.createInstantiator().newInstance(); 106 } 107}