01: //
02: // This file is part of the prose package.
03: //
04: // The contents of this file are subject to the Mozilla Public License
05: // Version 1.1 (the "License"); you may not use this file except in
06: // compliance with the License. You may obtain a copy of the License at
07: // http://www.mozilla.org/MPL/
08: //
09: // 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:
23: import java.lang.IllegalAccessException;
24: import java.io.Serializable;
25: import ch.ethz.jvmai.JoinPoint;
26: import java.lang.Object;
27: import java.lang.reflect.InvocationTargetException;
28:
29: ///////////////////////////////////////////////////////////////////////////////////////////
30: /// ADVICE EXECUTION
31: ///////////////////////////////////////////////////////////////////////////////////////////
32:
33: /** The <code>McutAdvice</code> encapsulates the transformation of the join-point
34: * data (e.g., target, stack parameters) to the types required to execute
35: * the <code>UserDefinedMCSignature</code> of this crosscut. The <code>execute</code>
36: * method does this job.
37: * <p>
38: * An <code>McutAdvice</code> object contains all the data needed
39: * for the advice method <em>invocation</em>:
40: * <ol>
41: * <li> An array of objects containing the arguments of the method
42: * being currently invoked (<code>stackArgs</code>). The <code>this</code>
43: * object (if existent, that is, if the invoked method is not static)
44: * is the first argument.
45: * <li> the number of valid arguments
46: * <li> the <code>UserDefinedMCSignature</code> object describing the static
47: * information about the advice action to be invoked.
48: * <ol>
49: * <p>
50: * Because of performance reasons, thi class expose a part of the
51: * inner structure of this class. In addition to the protected
52: * <code>stackArgs</code>,<code>stackArgsLength</code>, and <code>advice</code>,
53: * for which no setters/getters are provided, the construtor invokes
54: * the <code>allocStackArgs</code> template method to allocate
55: * the space for the <code>stackArgs</code> array. This way,
56: * subclasses have the chance to override this method and create
57: * an array in which the number of arguments (<code>stackArgsLength</code>)
58: * is equal to the actual length of the argument array. Having an
59: * array with no trailing nulls will make <code>execute</code>'s task
60: * more efficient (because execute will avoid array copying).
61: */
62: abstract class McutAdvice implements Serializable {
63: private final MethodCut methodCut;
64: private static final int UNKNOWN = -1;
65:
66: transient protected Object[] stackArgs = null;
67: transient protected int stackArgsLength = UNKNOWN;
68: transient protected MethodCutSignaturePattern advice;
69:
70: protected McutAdvice(MethodCut methodCut, JoinPoint joinPoint,
71: MethodCutSignaturePattern advice) {
72: this .advice = advice;
73: Object[] args = joinPoint.getArgs();
74: allocStackArgs(args.length + 1);
75:
76: stackArgs[0] = joinPoint.getThis();
77: for (int i = 0; i < args.length; i++)
78: stackArgs[i + 1] = args[i];
79: this .methodCut = methodCut;
80:
81: }
82:
83: // 2. Alllocate local params array
84: protected void allocStackArgs(int expectedLength) {
85: stackArgsLength = expectedLength;
86: stackArgs = new Object[stackArgsLength];
87: }
88:
89: abstract protected void execute() throws IllegalAccessException,
90: InvocationTargetException;
91: }
|