//
// This file is part of the prose package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is prose.
//
// The Initial Developer of the Original Code is Angela Nicoara. Portions
// created by Angela Nicoara are Copyright (C) 2004 Angela Nicoara.
// All Rights Reserved.
//
// Contributor(s):
// $Id: DefaultCcutAdvice.java,v 1.2 2008/11/18 11:36:07 anicoara Exp $
// =====================================================================
//
package ch.ethz.prose.crosscut;
import java.lang.IllegalArgumentException;
import java.lang.NullPointerException;
import java.lang.Class;
import java.lang.Error;
import java.lang.IndexOutOfBoundsException;
import java.lang.IllegalAccessException;
import java.util.Arrays;
import java.lang.InstantiationException;
import java.lang.reflect.Method;
import java.lang.Object;
import java.lang.System;
import java.lang.reflect.InvocationTargetException;
import ch.ethz.jvmai.JoinPoint;
/**
* The execution of an advice method of aribitrary signature.
*
* @version $Revision: 1.2 $
* @author Angela Nicoara
* @author Gerald Linhofer
*/
class DefaultCcutAdvice extends CcutAdvice {
private static final long serialVersionUID = 3978426940077912628L;
private final ConstructorCut constructorCut;
DefaultCcutAdvice(ConstructorCut constructorCut, JoinPoint m, ConstructorCutSignaturePattern a) {
super(constructorCut, m,a);
this.constructorCut = constructorCut;
}
protected void execute() throws IllegalAccessException, InvocationTargetException {
Object actualParams[] = new Object[stackArgsLength];
System.arraycopy(stackArgs,0,actualParams,0,stackArgsLength);
localActionImpl(constructorCut, advice.methodObj, stackArgs[0], actualParams);
}
// implement create local action
private void localActionImpl(Object adviceThis,Method advMethod, Object localThis, Object[] localParam)
throws InvocationTargetException,IllegalAccessException {
Object[] localPar = new Object[localParam.length -1];
System.arraycopy(localParam,1,localPar,0,localParam.length -1);
Class[] adviceSignature = advMethod.getParameterTypes();
Object[] actualParams = new Object[adviceSignature.length];
for (int i =0; i<adviceSignature.length; i++) {
Object crtLocParam = localThis;
if (i>0) {
try { crtLocParam=localPar[i-1]; }
catch (IndexOutOfBoundsException e) {
// we can actually ignore this case. It happens
// if the advice signature looks like (XXX,REST)
// but the joinpointed method has no parameters.
// then localParams(0) does not exist.
// formally, i set crtLocParam to zero
crtLocParam=null;
}
}
if (Wildcard.class.isAssignableFrom(adviceSignature[i])) {
Wildcard oscarWild;
try {
oscarWild = (Wildcard)adviceSignature[i].newInstance();
if (i < localPar.length + 1 &&
(crtLocParam == null || oscarWild.isAssignableFrom(crtLocParam.getClass())))
oscarWild.setObject(crtLocParam);
else
{
Object rest = new Object[localPar.length - i +1];
System.arraycopy(localPar,i-1,rest,0,localPar.length -i +1);
oscarWild.setObject(rest);
}
}
// the Wilcard specification states that wildcard classes
// should have an emtpy default constructor. If they
// don't have;it it is a RUNES IMPLEMENTATION ERROR
catch (IllegalAccessException noConstructor) {
throw new Error(noConstructor.toString() + "/" + adviceSignature[i]);
}
catch (InstantiationException wrongConstructor) {
throw new Error(wrongConstructor.toString());
}
actualParams[i] = oscarWild;
}
else {
actualParams[i] = crtLocParam;
}
}
// 3. create an action with the event stuff
try {
advMethod.invoke(adviceThis, actualParams);
}
catch (IllegalArgumentException wrongArgs) {
throw new Error("MethodCut.joinPointAction: " + wrongArgs + ". Actual arguments" +
Arrays.asList(actualParams) + " Expected arguments: " + advMethod);
}
catch (IllegalAccessException wrongMethod) {
throw new IllegalAccessException("Advice method (" + advMethod + ") not visible");
}
catch(NullPointerException noReceiver) {
throw new Error("MethodCut.joinPointAction:" + noReceiver.toString());
}
}
}