View Javadoc

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 java.lang.reflect.Constructor;
11  
12  import org.codehaus.aspectwerkz.exception.DefinitionException;
13  import org.codehaus.aspectwerkz.DeploymentModel;
14  
15  /***
16   * Abstract base class for the mixin container implementations.
17   *
18   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19   */
20  public abstract class AbstractMixinFactory implements MixinFactory {
21  
22      protected final Class m_mixinClass;
23      protected final DeploymentModel m_deploymentModel;
24      protected Constructor m_defaultConstructor;
25      protected Constructor m_perClassConstructor;
26      protected Constructor m_perInstanceConstructor;
27  
28      /***
29       * Creates a new mixin factory.
30       *
31       * @param mixinClass
32       * @param deploymentModel
33       */
34      public AbstractMixinFactory(final Class mixinClass, final DeploymentModel deploymentModel) {
35          m_mixinClass = mixinClass;
36          m_deploymentModel = deploymentModel;
37          try {
38              if (m_deploymentModel.equals(DeploymentModel.PER_CLASS)) {
39                  m_perClassConstructor = m_mixinClass.getConstructor(new Class[]{Class.class});
40              } else if (m_deploymentModel.equals(DeploymentModel.PER_INSTANCE)) {
41                  m_perInstanceConstructor = m_mixinClass.getConstructor(new Class[]{Object.class});
42              } else if (m_deploymentModel.equals(DeploymentModel.PER_JVM)) {
43                  m_defaultConstructor = m_mixinClass.getConstructor(new Class[]{});
44              } else {
45                  throw new DefinitionException(
46                          "deployment model for [" + m_mixinClass.getName() + "] is not supported [" +
47                          m_deploymentModel + "]"
48                  );
49              }
50          } catch (NoSuchMethodException e1) {
51              try {
52                  m_defaultConstructor = m_mixinClass.getConstructor(new Class[]{});
53              } catch (NoSuchMethodException e2) {
54                  throw new DefinitionException(
55                          "mixin [" + m_mixinClass.getName() +
56                          "] does not have a constructor that matches with its deployment model or a non-argument default constructor"
57                  );
58              }
59          }
60      }
61  
62      /***
63       * Creates a new perJVM mixin instance.
64       *
65       * @return the mixin instance
66       */
67      public abstract Object mixinOf();
68  
69      /***
70       * Creates a new perClass mixin instance.
71       *
72       * @param klass
73       * @return the mixin instance
74       */
75      public abstract Object mixinOf(Class klass);
76  
77      /***
78       * Creates a new perInstance mixin instance.
79       *
80       * @param instance
81       * @return the mixin instance
82       */
83      public abstract Object mixinOf(Object instance);
84  }