1 4 package com.tc.aspectwerkz.aspect; 5 6 7 import com.tc.aspectwerkz.AspectContext; 8 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 9 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.InvocationTargetException ; 12 import java.util.Map ; 13 14 22 public class DefaultAspectContainerStrategy extends AbstractAspectContainer { 23 24 26 29 protected Constructor m_aspectConstructor = null; 30 31 34 public DefaultAspectContainerStrategy(Class aspectClass, ClassLoader aopSystemClassLoader, String uuid, String qualifiedName, Map parameters) { 35 super(aspectClass, aopSystemClassLoader, uuid, qualifiedName, parameters); 36 } 37 38 43 protected Object createAspect(AspectContext aspectContext) { 44 if (m_aspectConstructor == null) { 45 m_aspectConstructor = findConstructor(); 46 } 47 try { 48 switch (m_constructionType) { 49 case ASPECT_CONSTRUCTION_TYPE_DEFAULT: 50 return m_aspectConstructor.newInstance(EMPTY_OBJECT_ARRAY); 51 case ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT: 52 return m_aspectConstructor.newInstance(new Object []{aspectContext}); 53 default: 54 throw new Error ("should not happen"); 55 } 56 } catch (InvocationTargetException e) { 57 e.printStackTrace(); 58 throw new WrappedRuntimeException(e.getTargetException()); 59 } catch (Exception e) { 60 throw new WrappedRuntimeException(e); 61 } 62 } 63 64 69 protected Constructor findConstructor() { 70 Constructor aspectConstructor = null; 71 Class aspectClass = getAspectClass(); 72 Constructor [] constructors = aspectClass.getDeclaredConstructors(); 73 for (int i = 0; i < constructors.length; i++) { 74 Constructor constructor = constructors[i]; 75 Class [] parameterTypes = constructor.getParameterTypes(); 76 if (parameterTypes.length == 0) { 77 m_constructionType = ASPECT_CONSTRUCTION_TYPE_DEFAULT; 78 aspectConstructor = constructor; 79 } else if ((parameterTypes.length == 1) && parameterTypes[0].equals(AspectContext.class)) { 80 m_constructionType = ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT; 81 aspectConstructor = constructor; 82 break; 83 } 84 } 85 if (m_constructionType == ASPECT_CONSTRUCTION_TYPE_UNKNOWN) { 86 throw new RuntimeException ( 87 "aspect [" 88 + aspectClass.getName() 89 + 90 "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)" 91 + " to be used with container=\"com.tc.aspectwerkz.aspect.DefaultAspectContainerStrategy\"" 92 ); 93 } 94 return aspectConstructor; 95 } 96 } | Popular Tags |