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.definition;
9   
10  import org.codehaus.aspectwerkz.expression.ExpressionInfo;
11  import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
12  import org.codehaus.aspectwerkz.transform.ReflectHelper;
13  
14  import java.lang.reflect.Method;
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  /***
20   * Holds the meta-data for an interface + implementation introduction.
21   * 
22   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
23   */
24  public class IntroductionDefinition {
25      /***
26       * The deployment model for the introduction.
27       */
28      private final String m_deploymentModel;
29  
30      /***
31       * The introduced methods info list.
32       */
33      private final List m_methodsToIntroduce = new ArrayList();
34  
35      /***
36       * The interface classes name.
37       */
38      private final List m_interfaceClassNames = new ArrayList();
39  
40      /***
41       * The name of the interface introduction.
42       */
43      private final String m_name;
44  
45      /***
46       * The introduction expressions.
47       */
48      private ExpressionInfo[] m_expressionInfos;
49  
50      /***
51       * The attribute for the introduction.
52       */
53      private String m_attribute = "";
54  
55      /***
56       * Construct a new Definition for introduction.
57       * 
58       * @param mixinClass the mixin class
59       * @param expressionInfo the expression info
60       * @param deploymentModel introduction deployment model
61       */
62      public IntroductionDefinition(final Class mixinClass,
63                                    final ExpressionInfo expressionInfo,
64                                    final String deploymentModel) {
65          m_name = mixinClass.getName();
66          m_expressionInfos = new ExpressionInfo[] {
67              expressionInfo
68          };
69          List interfaceDeclaredMethods = collectInterfaces(mixinClass);
70          List sortedMethodList = ReflectHelper.createInterfaceDefinedSortedMethodList(mixinClass, interfaceDeclaredMethods);
71          for (Iterator iterator = sortedMethodList.iterator(); iterator.hasNext();) {
72              m_methodsToIntroduce.add(JavaMethodInfo.getMethodInfo((Method) iterator.next()));
73          }
74          m_deploymentModel = deploymentModel;
75      }
76  
77      /***
78       * Returns the methods to introduce.
79       * 
80       * @return the methods to introduce
81       */
82      public List getMethodsToIntroduce() {
83          return m_methodsToIntroduce;
84      }
85  
86      /***
87       * Returns the deployment model.
88       * 
89       * @return the deployment model
90       */
91      public String getDeploymentModel() {
92          return m_deploymentModel;
93      }
94  
95      /***
96       * Returns the name of the introduction.
97       * 
98       * @return the name
99       */
100     public String getName() {
101         return m_name;
102     }
103 
104     /***
105      * Returns the expressions.
106      * 
107      * @return the expressions array
108      */
109     public ExpressionInfo[] getExpressionInfos() {
110         return m_expressionInfos;
111     }
112 
113     /***
114      * Returns the class name of the interface.
115      * 
116      * @return the class name of the interface
117      */
118     public String getInterfaceClassName() {
119         return (String) m_interfaceClassNames.get(0);
120     }
121 
122     /***
123      * Returns the class name of the interface.
124      * 
125      * @return the class name of the interface
126      */
127     public List getInterfaceClassNames() {
128         return m_interfaceClassNames;
129     }
130 
131     /***
132      * Returns the attribute.
133      * 
134      * @return the attribute
135      */
136     public String getAttribute() {
137         return m_attribute;
138     }
139 
140     /***
141      * Sets the attribute.
142      * 
143      * @param attribute the attribute
144      */
145     public void setAttribute(final String attribute) {
146         m_attribute = attribute;
147     }
148 
149     /***
150      * Adds a new expression info.
151      * 
152      * @param expression a new expression info
153      */
154     public void addExpressionInfo(final ExpressionInfo expression) {
155         final ExpressionInfo[] tmpExpressions = new ExpressionInfo[m_expressionInfos.length + 1];
156         java.lang.System.arraycopy(m_expressionInfos, 0, tmpExpressions, 0, m_expressionInfos.length);
157         tmpExpressions[m_expressionInfos.length] = expression;
158         m_expressionInfos = new ExpressionInfo[m_expressionInfos.length + 1];
159         java.lang.System.arraycopy(tmpExpressions, 0, m_expressionInfos, 0, tmpExpressions.length);
160     }
161 
162     /***
163      * Adds an array with new expression infos.
164      * 
165      * @param expressions an array with new expression infos
166      */
167     public void addExpressionInfos(final ExpressionInfo[] expressions) {
168         final ExpressionInfo[] tmpExpressions = new ExpressionInfo[m_expressionInfos.length + expressions.length];
169         java.lang.System.arraycopy(m_expressionInfos, 0, tmpExpressions, 0, m_expressionInfos.length);
170         java.lang.System.arraycopy(expressions, 0, tmpExpressions, m_expressionInfos.length, expressions.length);
171         m_expressionInfos = new ExpressionInfo[m_expressionInfos.length + expressions.length];
172         java.lang.System.arraycopy(tmpExpressions, 0, m_expressionInfos, 0, tmpExpressions.length);
173     }
174 
175     /***
176      * Collects the interfaces from all the base class mixins and the methods in the mixin interfaces
177      * 
178      * @param mixinClass
179      * @return list of methods declared in given class interfaces
180      */
181     private List collectInterfaces(final Class mixinClass) {
182         List interfaceDeclaredMethods = new ArrayList();
183         Class[] interfaces = mixinClass.getInterfaces();
184         for (int i = 0; i < interfaces.length; i++) {
185             m_interfaceClassNames.add(interfaces[i].getName());
186             interfaceDeclaredMethods.addAll(ReflectHelper.createCompleteSortedMethodList(interfaces[i]));
187         }
188         Class superClass = mixinClass.getSuperclass();
189         if (superClass != null) {
190             interfaceDeclaredMethods.addAll(collectInterfaces(superClass));
191         }
192         return interfaceDeclaredMethods;
193     }
194 }