package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.sql.Time; /** * This class represents a Method of the Main Test Class. It provides * methods that give information on the Method's return type, name, and * the types of the Method's parameters. * MethodData also provides a method to invoke the Method it represents. */ // Author: Brian Laird // Version History: // Nov 19, 2000 - comments/pseudocode added // Nov 30, 2000 - (Mike Hebron) changed class description // Feb 07, 2001 - (Apel Yahinian) Updated constructor, created model protected value // Mar. 11, 2001 - (Wes Strickland) Added code to run() to print to log when a method is invoked public class MethodData implements Runnable, MethodDataInterface { /** * Creates a new instance of MethodData *

* Pre-conditions: none * Post-conditions: the parameter name is returned * @return a string containing the name of the parameter */ public MethodData(Method newWrapped, ETAMainModel newmodel) { // SET wrapped to newWrapped wrapped = newWrapped; model = newmodel; } /** * Returns the name of the parameter *

* Pre-conditions: none * Post-conditions: the parameter name is returned * @return a string containing the name of the parameter */ public String getName() { // RETURN getName of wrapped returns str return wrapped.getName(); } /** * Returns the modifier of the parameter *

* Pre-conditions: none * Post-conditions: the parameter modifier is returned * @return a string containing the modifier of the parameter */ public int getModifiers() { // RETURN getModifiers of wrapped returns i return wrapped.getModifiers(); } /** * Returns the returnType of the parameter *

* Pre-conditions: none * Post-conditions: the parameter return type is returned * @return a string containing the return type of the parameter */ public String getReturnType() { //CALL getReturnType on wrapped return cla //CALL classToName of ETAMainModel returns str //RETURN str return ETAMainModel.classToName(wrapped.getReturnType()); } /** * Returns the declaring class of the parameter *

* Pre-conditions: none * Post-conditions: the parameter declaring class is returned * @return a string containing the declaring class of the parameter */ public String getDeclaringClass() { //CALL getDeclaringClass on wrapped returns cla //CALL classToName of ETAMainModel with cla returns str //RETURN str return ETAMainModel.classToName(wrapped.getDeclaringClass()); } /** * Returns array of parameter type names of the parameter *

* Pre-conditions: none * Post-conditions: the array of parameter type names is returned * @return an array of String containing the names of the Parameter types */ public String[] getParameterTypeNames() { //CALL getParameterTypes of wrapped returns cla[] //CALL getLength of cla[] returns idx //CONSTRUCT arry of String with idx //FOR index = 0 to idx //SET arry[index] to classToName on cla[index] returns str //ENDFOR //RETURN arry[] Class[] cla = null; //The Class type of the parameters that the wrapped method takes String[] arry = null; // Stores the names of the parameter Class types for return cla = wrapped.getParameterTypes(); arry = new String[Array.getLength(cla)]; for(int index = 0; index < Array.getLength(cla); index++) arry[index] = ETAMainModel.classToName(cla[index]); return arry; } /** * Creates and runs a new thread. *

* Pre-conditions: none * Post-conditions: A new thread is created */ public void invokeMethod() { ReturnThread freeThread; // free thread from the ThreadFreeList //CALL getFreeThread of ThreadFreeList returns freeThread //CALL setRunnable of freeThread with this //CALL start of freeThread freeThread = ThreadFreeList.getThreadFreeList().getFreeThread(); freeThread.setRunnable(this); freeThread.start(); } /** * Adds a user created primitive into the object pool and parameter combo * boxes based on passed Strings *

* Pre-conditions: none * Post-conditions: primitive has been created and put into the object pool */ public void createPrimitive(String val, String typeName) { //CALL forName of Class with typeName returns cla //CALL hashCode of cla returns hshcde //CALL getAPrimitiveValueFactory of PrimitiveValueFactory with hshcde returns fctryObj //CALL getValue of PrimitiveValueFactory with val returns valStr //CALL getNumber of this returns id //CONCATENATE "user created" AND typeName AND id INTO name //CALL addParameterData on this with fctryObj and name } /** * Adds a user created primitive into the object pool and parameter combo * boxes based on passed Strings *

* Pre-conditions: none * Post-conditions: primitive has been created and put into the object pool */ public void run() { ParameterDataInterface[] pdiArray; // array of selected PDIs Object[] objArray; // Object equivalent of pdiArray Object retObj; // Object returned from invoking a method Object mainTestObj; // Main Test Object String classMethod; // string containing class and method name of invoked method // CALL setEnabled of methodRow with "false" // CALL getParameters of methodRow returns pdiArray // CONSTRUCT objArray as NEW Array of Objects with length pdiArray.length methodRow.setEnabled(false); pdiArray = methodRow.getParameters(); objArray = new Object[pdiArray.length]; /* convert ParameterDataInterfaces to Objects*/ // FOR ndx = 0 to pdiArray.length - 1 // SET objArray[ndx] to getValueObject of pdiArray[ndx] // ENDFOR for (int ndx = 0; ndx < pdiArray.length; ndx++) { objArray[ndx] = ((ParameterData) pdiArray[ndx]).getValueObject(); } // CALL getMainTestObject of model returns mainTestObj mainTestObj = model.getMainTestObject(); // TRY // CALL invokeMethod of wrapped with mainTestObj and objArray returns retObj // CALL addReturnedData of model with retObj, classMethod // CALL setEnabled of methodRow with "true" try { retObj = wrapped.invoke(mainTestObj, objArray); // parse class and method name // remove parameter list // remove modfiers, package classMethod = wrapped.toString(); classMethod = classMethod.substring(0, classMethod.indexOf("(")); int lastDot = classMethod.lastIndexOf('.'); classMethod = classMethod.substring(classMethod.lastIndexOf(' ', lastDot - 1) + 1); if (wrapped.getReturnType() != Void.TYPE) { model.addReturnedData(retObj, classMethod); } // create string array to hold names of parameters used to invoke method // print to log the name and parameters of the invoked method String[] ParameterNames = new String[10]; ETA.log.print("[" + (new Time(System.currentTimeMillis())).toString().substring(0, 5) + "] Invoked Method " + classMethod); if (pdiArray.length > 0) { ETA.log.print("("); ParameterNames[0] = pdiArray[0].getDescriptiveName(); ETA.log.print(ParameterNames[0]); } if (pdiArray.length > 1) { for (int ndx = 1; ndx < pdiArray.length; ndx++) { ETA.log.print(", "); ParameterNames[ndx] = pdiArray[ndx].getDescriptiveName(); ETA.log.print(ParameterNames[ndx]); } ETA.log.print(")"); } ETA.log.println(""); // CATCH IllegalAccessException // CONSTRUCT String msg with "The ETA was not allowed to access this constructor." // CALL println of err in ETA with msg // CALL printStackTrace of IllegalAccessException with err in ETA //ENDCATCH } catch (IllegalAccessException exc) { ETA.err.println("The ETA was not allowed to access this constructor."); exc.printStackTrace(ETA.err); // CATCH InvocationTargetException // CALL printStackTrace of InvocationTargetException // ENDCATCH } catch (InvocationTargetException exc) { exc.printStackTrace(); // The method may have thrown an exception } catch (Exception exc) { exc.printStackTrace(); } methodRow.setEnabled(true); } /** * Sets attribute methodRow to methodRowInterface *

* Pre-conditions: none * Post-conditions: methodRow is set to methodRowInterface */ public void setMethodRowInterface(MethodRowInterface methodRowInterface) { //SET methodRow to methodRowInterface methodRow = methodRowInterface; } /** * attribute used for reflection */ protected Method wrapped; /** * MethodRowInterface used to access MethodRow */ protected MethodRowInterface methodRow; /** * etamainmodel model */ protected ETAMainModel model; }