KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > crosscut > MethodCutSignaturePattern


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
package ch.ethz.prose.crosscut;
22 import java.lang.IllegalAccessException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.lang.InstantiationException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.Error JavaDoc;
27 import java.lang.Class JavaDoc;
28 import ch.ethz.inf.util.Logger;
29 import java.lang.SecurityException JavaDoc;
30 import java.lang.ArrayIndexOutOfBoundsException JavaDoc;
31 ///////////////////////////////////////////////////////////////////////////////////////////
32
/// ADVICE DEFINITION (STATIC PART)
33
///////////////////////////////////////////////////////////////////////////////////////////
34

35   /** Every Crosscut contains a method to be executed when a join-point is hit.
36    * In the case of <code>MethodCut</code> objects, the advice method has three
37    * roles:
38    * <ul>
39    * <li> The pattern role: through a special signature, define matches for the
40    * target class, and for the parameters of the methods belonging to the target.
41    *
42    * <li> The method finder role: determine which method o of the crosscut
43    * must be ivoked upon a hit join-point.
44    *
45    * <li> The optimization role: to cathegorize the signature of the advice
46    * method, such that the subsequent execution of the advice method is efficient.
47    * The categorization is currently performed according to the <code>SIGNATURE__XXX</code>
48    * constants.
49    * </ul>
50    */

51   class MethodCutSignaturePattern extends SignaturePattern implements Serializable JavaDoc
52   {
53     private final MethodCut methodCut;
54
55
56
57     /**
58      * Create an <code>UserDefinedMCSignature</code> that encapsulates the
59      * method to be executed.
60      * <p>
61      * Postcondition: methodObj is properly initialized
62      * <p>
63      * Implementation:
64      * <ul>
65      * <li> if <code>crsc</code> (i.e., a subclass of FunctionalCrossscut)
66      * <em>defines</em> exaclty one method, this method is
67      * the advice method.
68      *
69      * <li> If <code>crsc</code> defines several methods, the one whose
70      * name corresponds to <code>crsc.ADVICE_NAME()</code> is the advice method
71      * </ul>
72      * @throws MissingInformationExeception crsc does not define a method at all,
73      * or if it defines several, none of them matches <code>crsc.ADVICE_NAME()</code>.
74      *
75      */

76     protected MethodCutSignaturePattern(MethodCut crsc) throws MissingInformationException
77       {
78     this.methodCut = crsc;
79     initFromMethod(crsc.getClass(),"METHOD_ARGS",REST.class,MethodCut.class);
80       }
81
82
83
84
85
86     /** Returns <code>true</code> if the signature of
87      * <code>methoObj</code>, seen as a pattern,
88      * matches the signature (the parameter types) for
89      * <code>actualMehtod</code>.
90      */

91     protected boolean matchesParameters(Method JavaDoc actualMethod)
92       {
93       if (signatureCathegory == SIGNATURE__EMPTY)
94           return true;
95       else
96           return
97           doMatchLength(actualMethod) &&
98           doMatchSignature(actualMethod);
99       }
100
101     private Class JavaDoc[] subarray(Class JavaDoc[] arr, int index)
102       {
103     Class JavaDoc[] result = new Class JavaDoc[arr.length - index];
104     System.arraycopy(arr,index,result,0,result.length);
105     return result;
106       }
107
108     private boolean doMatchSignature(Method JavaDoc actualMethod) throws Error JavaDoc
109       {
110     boolean signatureOK = true;
111     Class JavaDoc[] params = actualMethod.getParameterTypes();
112     for(int j=1; j< signature.length && signatureOK; j++)
113       {
114         int adviceParamPos = j;
115         int actualParamPos = j - 1;
116
117         try
118           {
119         if (isAssignable(subarray(params,actualParamPos), signature[adviceParamPos]))
120           signatureOK = true;
121         else if (isAssignable(params[actualParamPos], signature[adviceParamPos]))
122           {
123             // if the current advice Param is the last one, the length must concide
124
signatureOK = true;
125             if (adviceParamPos + 1 == signature.length)
126               {
127             signatureOK = ( signature.length == params.length + 1 );
128               }
129           }
130         else
131           signatureOK = false;
132
133           }
134         catch(ArrayIndexOutOfBoundsException JavaDoc e)
135           {
136         signatureOK = false;
137           }
138       }
139     return signatureOK;
140       }
141
142     private boolean doMatchLength(Method JavaDoc actualMethod)
143       {
144     Class JavaDoc[] params = actualMethod.getParameterTypes();
145     return ( (signature.length ==1 && params.length == 0) || signature.length >1);
146       }
147
148
149     public String JavaDoc toString()
150       {
151     return methodObj.toString();
152       }
153   }
154
Popular Tags