1 8 package org.codehaus.aspectwerkz.aspect; 9 10 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 11 import org.codehaus.aspectwerkz.AspectContext; 12 13 import java.lang.reflect.Constructor ; 14 import java.lang.reflect.InvocationTargetException ; 15 16 21 public class DefaultAspectContainerStrategy extends AbstractAspectContainer { 22 25 protected Constructor m_aspectConstructor = null; 26 27 32 public DefaultAspectContainerStrategy(final AspectContext aspectContext) { 33 super(aspectContext); 34 } 35 36 41 protected Object createAspect() { 42 if (m_aspectConstructor == null) { 43 m_aspectConstructor = findConstructor(); 44 } 45 try { 46 switch (m_constructionType) { 47 case ASPECT_CONSTRUCTION_TYPE_DEFAULT: 48 return m_aspectConstructor.newInstance(EMPTY_OBJECT_ARRAY); 49 case ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT: 50 return m_aspectConstructor.newInstance(ARRAY_WITH_SINGLE_ASPECT_CONTEXT); 51 default: 52 throw new RuntimeException ( 53 "aspect [" 54 + m_aspectContext.getAspectClass().getName() 55 + 56 "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)" 57 ); 58 } 59 } catch (InvocationTargetException e) { 60 e.printStackTrace(); 61 throw new WrappedRuntimeException(e.getTargetException()); 62 } catch (Exception e) { 63 throw new WrappedRuntimeException(e); 64 } 65 } 66 67 72 protected Constructor findConstructor() { 73 Constructor aspectConstructor = null; 74 Class aspectClass = m_aspectContext.getAspectClass(); 75 Constructor [] constructors = aspectClass.getDeclaredConstructors(); 76 for (int i = 0; i < constructors.length; i++) { 77 Constructor constructor = constructors[i]; 78 Class [] parameterTypes = constructor.getParameterTypes(); 79 if (parameterTypes.length == 0) { 80 m_constructionType = ASPECT_CONSTRUCTION_TYPE_DEFAULT; 81 aspectConstructor = constructor; 82 } else if ((parameterTypes.length == 1) && parameterTypes[0].equals(AspectContext.class)) { 83 m_constructionType = ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT; 84 aspectConstructor = constructor; 85 break; 86 } 87 } 88 if (m_constructionType == ASPECT_CONSTRUCTION_TYPE_UNKNOWN) { 89 throw new RuntimeException ( 90 "aspect [" 91 + aspectClass.getName() 92 + 93 "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)" 94 ); 95 } 96 return aspectConstructor; 97 } 98 } | Popular Tags |