/* * Copyright 2007 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2.Seq; import java.io.IOException; import java.io.ObjectOutput; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import Sequenic.P2.StringFormater; import Sequenic.T2.Pool; import Sequenic.T2.Msg.T2Error; import Sequenic.T2.export.Visitor; /** * This represents a test-step where we call a method. */ public class CALL_METHOD extends TraceStep { public Method method; // When it is null then the method is static. Else it is // the receiver of the call. public MkValStep receiver; public MkValStep[] params; /** * @param m The method to call. If the method is static, set r to null. * @param r MkVal step producing the receiver object. * @param ps MkVal steps produducing the arguments for r. */ public CALL_METHOD(Method m, MkValStep r, MkValStep[] ps) { method = m; receiver = r; params = ps; } public String getName() { return method.getName(); } public ExecResult exec(Class CUT, Pool pool, Object targetObj, int stepNr, List classinvs, ReportersPool reporters) { method.setAccessible(true); SlotsInjector.injectAtTestStep(targetObj, this); ExecResult result = new ExecResult(CUT,this,targetObj); // Construct the arguments first: try { result.args = MkValStep.MkArguments(CUT, pool, params); // If receiver null, then it is a static method, which has no receiver, // else construct the receiver: if (receiver != null) result.receiverObj = receiver.exec(CUT,pool); } catch (InvocationTargetException e) { result.argumentFailure = e.getCause() ; //result.checkException(e) ; } catch (Exception e) { // System.out.println(Show.show(receiver)) ; // e.printStackTrace() ; throw new T2Error(T2Error.BUG, "Fail to invoke method " + this, e); } if (result.argumentFailure == null) { // If arguments are sucessfully constructed, call the method: Class D = method.getDeclaringClass(); // also casting receiver to D: try { result.returnedObj = method.invoke(D.cast(result.receiverObj), result.args); result.execClassInv(classinvs, targetObj); } catch (InvocationTargetException e) { result.checkException(e) ; } catch (Exception e) { throw new T2Error(T2Error.BUG, "Fail to invoke method " + this, e); } } // Report, unless we have a NULLreporter: reporters.reportStep(result, stepNr) ; return result; } @Override public String toString() { String result = "CALL_METHOD, " + method.getName() ; String paramsTxt = "" ; for (int i=0; i