1 /*************************************************************************************** 2 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. * 3 * http://aspectwerkz.codehaus.org * 4 * ---------------------------------------------------------------------------------- * 5 * The software in this package is published under the terms of the LGPL license * 6 * a copy of which has been included with this distribution in the license.txt file. * 7 **************************************************************************************/ 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 /*** 17 * Implements the default aspect container strategy. 18 * 19 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a> 20 */ 21 public class DefaultAspectContainerStrategy extends AbstractAspectContainer { 22 /*** 23 * The constructor for the aspect. 24 */ 25 protected Constructor m_aspectConstructor = null; 26 27 /*** 28 * Creates a new aspect container strategy. 29 * 30 * @param aspectContext the cross-cutting info 31 */ 32 public DefaultAspectContainerStrategy(final AspectContext aspectContext) { 33 super(aspectContext); 34 } 35 36 /*** 37 * Creates a new aspect instance. 38 * 39 * @return the new aspect instance 40 */ 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 /*** 68 * Grabs the correct constructor for the aspect. 69 * 70 * @return the constructor for the aspect 71 */ 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 }