001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.aspect;
008:
009: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
010: import org.codehaus.aspectwerkz.AspectContext;
011:
012: import java.lang.reflect.Constructor;
013: import java.lang.reflect.InvocationTargetException;
014:
015: /**
016: * Implements the default aspect container strategy.
017: *
018: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
019: */
020: public class DefaultAspectContainerStrategy extends
021: AbstractAspectContainer {
022: /**
023: * The constructor for the aspect.
024: */
025: protected Constructor m_aspectConstructor = null;
026:
027: /**
028: * Creates a new aspect container strategy.
029: *
030: * @param aspectContext the cross-cutting info
031: */
032: public DefaultAspectContainerStrategy(
033: final AspectContext aspectContext) {
034: super (aspectContext);
035: }
036:
037: /**
038: * Creates a new aspect instance.
039: *
040: * @return the new aspect instance
041: */
042: protected Object createAspect() {
043: if (m_aspectConstructor == null) {
044: m_aspectConstructor = findConstructor();
045: }
046: try {
047: switch (m_constructionType) {
048: case ASPECT_CONSTRUCTION_TYPE_DEFAULT:
049: return m_aspectConstructor
050: .newInstance(EMPTY_OBJECT_ARRAY);
051: case ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT:
052: return m_aspectConstructor
053: .newInstance(ARRAY_WITH_SINGLE_ASPECT_CONTEXT);
054: default:
055: throw new RuntimeException(
056: "aspect ["
057: + m_aspectContext.getAspectClass()
058: .getName()
059: + "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)");
060: }
061: } catch (InvocationTargetException e) {
062: e.printStackTrace();
063: throw new WrappedRuntimeException(e.getTargetException());
064: } catch (Exception e) {
065: throw new WrappedRuntimeException(e);
066: }
067: }
068:
069: /**
070: * Grabs the correct constructor for the aspect.
071: *
072: * @return the constructor for the aspect
073: */
074: protected Constructor findConstructor() {
075: Constructor aspectConstructor = null;
076: Class aspectClass = m_aspectContext.getAspectClass();
077: Constructor[] constructors = aspectClass
078: .getDeclaredConstructors();
079: for (int i = 0; i < constructors.length; i++) {
080: Constructor constructor = constructors[i];
081: Class[] parameterTypes = constructor.getParameterTypes();
082: if (parameterTypes.length == 0) {
083: m_constructionType = ASPECT_CONSTRUCTION_TYPE_DEFAULT;
084: aspectConstructor = constructor;
085: } else if ((parameterTypes.length == 1)
086: && parameterTypes[0].equals(AspectContext.class)) {
087: m_constructionType = ASPECT_CONSTRUCTION_TYPE_ASPECT_CONTEXT;
088: aspectConstructor = constructor;
089: break;
090: }
091: }
092: if (m_constructionType == ASPECT_CONSTRUCTION_TYPE_UNKNOWN) {
093: throw new RuntimeException(
094: "aspect ["
095: + aspectClass.getName()
096: + "] does not have a valid constructor (either default no-arg or one that takes a AspectContext type as its only parameter)");
097: }
098: return aspectConstructor;
099: }
100: }
|