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.CrossCuttingInfo; 11 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 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 crossCuttingInfo the cross-cutting info 31 */ 32 public DefaultAspectContainerStrategy(final CrossCuttingInfo crossCuttingInfo) { 33 super(crossCuttingInfo); 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_CROSS_CUTTING_INFO: 50 return m_aspectConstructor.newInstance(arrayWithSingleCrossCuttingInfo); 51 default: 52 throw new RuntimeException( 53 "aspect [" 54 + m_aspectPrototype.getClass().getName() 55 + "] does not have a valid constructor (either default no-arg or one that takes a CrossCuttingInfo type as its only parameter)"); 56 } 57 } catch (InvocationTargetException e) { 58 e.printStackTrace(); 59 throw new WrappedRuntimeException(e.getTargetException()); 60 } catch (Exception e) { 61 throw new WrappedRuntimeException(e); 62 } 63 } 64 65 /*** 66 * Grabs the correct constructor for the aspect. 67 * 68 * @return the constructor for the aspect 69 */ 70 protected Constructor findConstructor() { 71 Constructor aspectConstructor = null; 72 Class aspectClass = m_infoPrototype.getAspectClass(); 73 Constructor[] constructors = aspectClass.getDeclaredConstructors(); 74 for (int i = 0; i < constructors.length; i++) { 75 Constructor constructor = constructors[i]; 76 Class[] parameterTypes = constructor.getParameterTypes(); 77 if (parameterTypes.length == 0) { 78 m_constructionType = ASPECT_CONSTRUCTION_TYPE_DEFAULT; 79 aspectConstructor = constructor; 80 } else if ((parameterTypes.length == 1) && parameterTypes[0].equals(CrossCuttingInfo.class)) { 81 m_constructionType = ASPECT_CONSTRUCTION_TYPE_CROSS_CUTTING_INFO; 82 aspectConstructor = constructor; 83 break; 84 } 85 } 86 if (m_constructionType == ASPECT_CONSTRUCTION_TYPE_UNKNOWN) { 87 throw new RuntimeException( 88 "aspect [" 89 + aspectClass.getName() 90 + "] does not have a valid constructor (either default no-arg or one that takes a CrossCuttingInfo type as its only parameter)"); 91 } 92 return aspectConstructor; 93 } 94 }