KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > aspect > AbstractMixinFactory


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 JavaDoc;
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 JavaDoc m_mixinClass;
23     protected final DeploymentModel m_deploymentModel;
24     protected Constructor JavaDoc m_defaultConstructor;
25     protected Constructor JavaDoc m_perClassConstructor;
26     protected Constructor JavaDoc m_perInstanceConstructor;
27
28     /**
29      * Creates a new mixin factory.
30      *
31      * @param mixinClass
32      * @param deploymentModel
33      */

34     public AbstractMixinFactory(final Class JavaDoc 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 JavaDoc[]{Class JavaDoc.class});
40             } else if (m_deploymentModel.equals(DeploymentModel.PER_INSTANCE)) {
41                 m_perInstanceConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{Object JavaDoc.class});
42             } else if (m_deploymentModel.equals(DeploymentModel.PER_JVM)) {
43                 m_defaultConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{});
44             } else {
45                 throw new DefinitionException(
46                         "deployment model for [" + m_mixinClass.getName() + "] is not supported [" +
47                         m_deploymentModel + "]"
48                 );
49             }
50         } catch (NoSuchMethodException JavaDoc e1) {
51             try {
52                 m_defaultConstructor = m_mixinClass.getConstructor(new Class JavaDoc[]{});
53             } catch (NoSuchMethodException JavaDoc 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 JavaDoc mixinOf();
68
69     /**
70      * Creates a new perClass mixin instance.
71      *
72      * @param klass
73      * @return the mixin instance
74      */

75     public abstract Object JavaDoc mixinOf(Class JavaDoc klass);
76
77     /**
78      * Creates a new perInstance mixin instance.
79      *
80      * @param instance
81      * @return the mixin instance
82      */

83     public abstract Object JavaDoc mixinOf(Object JavaDoc instance);
84 }
Popular Tags