Package ch.ethz.prose.crosscut

Source Code of ch.ethz.prose.crosscut.DefaultMcutAdvice

//
//  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 Andrei Popovici. Portions
//  created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
//  All Rights Reserved.
//
//  Contributor(s):
//  $Id: DefaultMcutAdvice.java,v 1.3 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 ch.ethz.jvmai.JoinPoint;
import java.lang.InstantiationException;
import java.lang.reflect.Method;
import java.lang.Object;
import java.lang.System;
import java.lang.reflect.InvocationTargetException;

/**
* The execution of an advice method of arbitrary signature.
*/
class DefaultMcutAdvice extends McutAdvice {
  private static final long serialVersionUID = 3256720663157880374L;
  private final MethodCut methodCut;

  DefaultMcutAdvice(MethodCut methodCut, JoinPoint m, MethodCutSignaturePattern a) {
    super(methodCut, m,a);
    this.methodCut = methodCut;
  }
 
  protected void execute() throws IllegalAccessException, InvocationTargetException {
    Object actualParams[] = new Object[stackArgsLength];
    System.arraycopy(stackArgs,0,actualParams,0,stackArgsLength);
    localActionImpl(methodCut, 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 empty 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());
    }
  }

}
TOP

Related Classes of ch.ethz.prose.crosscut.DefaultMcutAdvice

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.