KickJava   Java API By Example, From Geeks To Geeks.

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


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.IllegalArgumentException JavaDoc;
23 import java.lang.NullPointerException JavaDoc;
24 import java.lang.Class JavaDoc;
25 import java.lang.Error JavaDoc;
26 import java.lang.IndexOutOfBoundsException JavaDoc;
27 import java.lang.IllegalAccessException JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import ch.ethz.jvmai.JoinPoint;
30 import java.lang.InstantiationException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.Object JavaDoc;
33 import java.lang.System JavaDoc;
34 import java.lang.reflect.InvocationTargetException JavaDoc;
35
36 /** The execution of an advice method of aribitrary signature.
37    */

38   class DefaultMcutAdvice extends McutAdvice
39   {
40     private final MethodCut methodCut;
41     DefaultMcutAdvice(MethodCut methodCut, JoinPoint m, MethodCutSignaturePattern a)
42       {
43     super(methodCut, m,a);
44     this.methodCut = methodCut;
45       }
46     protected void execute() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
47       {
48     Object JavaDoc actualParams[] = new Object JavaDoc[stackArgsLength];
49     System.arraycopy(stackArgs,0,actualParams,0,stackArgsLength);
50     localActionImpl(methodCut, advice.methodObj, stackArgs[0], actualParams);
51       }
52
53   // implement create local action
54
private void localActionImpl(Object JavaDoc adviceThis,Method JavaDoc advMethod, Object JavaDoc localThis, Object JavaDoc[] localParam)
55     throws InvocationTargetException JavaDoc,IllegalAccessException JavaDoc
56     {
57
58
59       Object JavaDoc[] localPar = new Object JavaDoc[localParam.length -1];
60       System.arraycopy(localParam,1,localPar,0,localParam.length -1);
61
62       Class JavaDoc[] adviceSignature = advMethod.getParameterTypes();
63       Object JavaDoc[] actualParams = new Object JavaDoc[adviceSignature.length];
64       for (int i =0; i<adviceSignature.length; i++)
65     {
66
67       Object JavaDoc crtLocParam = localThis;
68       if (i>0)
69         {
70           try { crtLocParam=localPar[i-1]; }
71           catch (IndexOutOfBoundsException JavaDoc e)
72         {
73           // we can actually ignore this case. It happens
74
// if the advice signature looks like (XXX,REST)
75
// but the joinpointed method has no parameters.
76
// then localParams(0) does not exist.
77
// formally, i set crtLocParam to zero
78
crtLocParam=null;
79         }
80         }
81       if (Wildcard.class.isAssignableFrom(adviceSignature[i]))
82         {
83           Wildcard oscarWild;
84           try
85         {
86           oscarWild = (Wildcard)adviceSignature[i].newInstance();
87           if (i < localPar.length + 1 &&
88               (crtLocParam == null || oscarWild.isAssignableFrom(crtLocParam.getClass())))
89             oscarWild.setObject(crtLocParam);
90           else
91             {
92               Object JavaDoc rest = new Object JavaDoc[localPar.length - i +1];
93               System.arraycopy(localPar,i-1,rest,0,localPar.length -i +1);
94               oscarWild.setObject(rest);
95             }
96         }
97           // the Wilcard specification states that wildcard classes
98
// should have an emtpy default constructor. If they
99
// don't have;it it is a RUNES IMPLEMENTATION ERROR
100
catch (IllegalAccessException JavaDoc noConstructor)
101         {
102
103           throw new Error JavaDoc(noConstructor.toString() + "/" + adviceSignature[i]);
104         }
105           catch (InstantiationException JavaDoc wrongConstructor)
106         {
107           throw new Error JavaDoc(wrongConstructor.toString());
108         }
109
110           actualParams[i] = oscarWild;
111         }
112       else
113         {
114           actualParams[i] = crtLocParam;
115         }
116     }
117
118       // 3. create an action with the event stuff
119
try
120     {
121       advMethod.invoke(adviceThis, actualParams);
122     }
123       catch (IllegalArgumentException JavaDoc wrongArgs)
124     {
125       throw new Error JavaDoc("MethodCut.joinPointAction: " + wrongArgs + ". Actual arguments" +
126               Arrays.asList(actualParams) + " Expected arguments: " + advMethod);
127     }
128       catch (IllegalAccessException JavaDoc wrongMethod)
129     {
130       throw new IllegalAccessException JavaDoc("Advice method (" + advMethod + ") not visible");
131     }
132       catch(NullPointerException JavaDoc noReceiver)
133     {
134       throw new Error JavaDoc("MethodCut.joinPointAction:" + noReceiver.toString());
135     }
136     }
137
138   }
139
Popular Tags