001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // The Initial Developers of the Original Code are Angela Nicoara.
017: // All Rights Reserved.
018: //
019: // Contributor(s):
020: // $Id$
021: // =====================================================================
022: //
023: // (history at end)
024: //
025:
026: package ch.ethz.prose.crosscut;
027:
028: import java.lang.IllegalArgumentException;
029: import java.lang.NullPointerException;
030: import java.lang.Class;
031: import java.lang.Error;
032: import java.lang.IndexOutOfBoundsException;
033: import java.lang.IllegalAccessException;
034: import java.util.Arrays;
035: import java.lang.InstantiationException;
036: import java.lang.reflect.Method;
037: import java.lang.Object;
038: import java.lang.System;
039: import java.lang.reflect.InvocationTargetException;
040:
041: import ch.ethz.jvmai.JoinPoint;
042:
043: /**
044: * The execution of an advice method of aribitrary signature.
045: *
046: * @version $Revision$
047: * @author Angela Nicoara
048: * @author Gerald Linhofer
049: */
050: class DefaultCcutAdvice extends CcutAdvice {
051: private static final long serialVersionUID = 3978426940077912628L;
052:
053: private final ConstructorCut constructorCut;
054:
055: DefaultCcutAdvice(ConstructorCut constructorCut, JoinPoint m,
056: ConstructorCutSignaturePattern a) {
057: super (constructorCut, m, a);
058: this .constructorCut = constructorCut;
059: }
060:
061: protected void execute() throws IllegalAccessException,
062: InvocationTargetException {
063: Object actualParams[] = new Object[stackArgsLength];
064: System
065: .arraycopy(stackArgs, 0, actualParams, 0,
066: stackArgsLength);
067: localActionImpl(constructorCut, advice.methodObj, stackArgs[0],
068: actualParams);
069: }
070:
071: // implement create local action
072: private void localActionImpl(Object adviceThis, Method advMethod,
073: Object localThis, Object[] localParam)
074: throws InvocationTargetException, IllegalAccessException {
075:
076: Object[] localPar = new Object[localParam.length - 1];
077: System.arraycopy(localParam, 1, localPar, 0,
078: localParam.length - 1);
079:
080: Class[] adviceSignature = advMethod.getParameterTypes();
081: Object[] actualParams = new Object[adviceSignature.length];
082: for (int i = 0; i < adviceSignature.length; i++) {
083:
084: Object crtLocParam = localThis;
085: if (i > 0) {
086: try {
087: crtLocParam = localPar[i - 1];
088: } catch (IndexOutOfBoundsException e) {
089: // we can actually ignore this case. It happens
090: // if the advice signature looks like (XXX,REST)
091: // but the joinpointed method has no parameters.
092: // then localParams(0) does not exist.
093: // formally, i set crtLocParam to zero
094: crtLocParam = null;
095: }
096: }
097: if (Wildcard.class.isAssignableFrom(adviceSignature[i])) {
098: Wildcard oscarWild;
099: try {
100: oscarWild = (Wildcard) adviceSignature[i]
101: .newInstance();
102: if (i < localPar.length + 1
103: && (crtLocParam == null || oscarWild
104: .isAssignableFrom(crtLocParam
105: .getClass())))
106: oscarWild.setObject(crtLocParam);
107: else {
108: Object rest = new Object[localPar.length - i
109: + 1];
110: System.arraycopy(localPar, i - 1, rest, 0,
111: localPar.length - i + 1);
112: oscarWild.setObject(rest);
113: }
114: }
115: // the Wilcard specification states that wildcard classes
116: // should have an emtpy default constructor. If they
117: // don't have;it it is a RUNES IMPLEMENTATION ERROR
118: catch (IllegalAccessException noConstructor) {
119:
120: throw new Error(noConstructor.toString() + "/"
121: + adviceSignature[i]);
122: } catch (InstantiationException wrongConstructor) {
123: throw new Error(wrongConstructor.toString());
124: }
125:
126: actualParams[i] = oscarWild;
127: } else {
128: actualParams[i] = crtLocParam;
129: }
130: }
131:
132: // 3. create an action with the event stuff
133: try {
134: advMethod.invoke(adviceThis, actualParams);
135: } catch (IllegalArgumentException wrongArgs) {
136: throw new Error("MethodCut.joinPointAction: " + wrongArgs
137: + ". Actual arguments"
138: + Arrays.asList(actualParams)
139: + " Expected arguments: " + advMethod);
140: } catch (IllegalAccessException wrongMethod) {
141: throw new IllegalAccessException("Advice method ("
142: + advMethod + ") not visible");
143: } catch (NullPointerException noReceiver) {
144: throw new Error("MethodCut.joinPointAction:"
145: + noReceiver.toString());
146: }
147: }
148:
149: }
150:
151: //======================================================================
152: //
153: //$Log$
154: //
|