Class BMethod


  • public class BMethod
    extends java.lang.Object
    A wrapper for a method of a BlueJ class. Allows an extension to invoke a method on an object that is on the BlueJ object bench. When values representing types are returned, there are two cases: In the case that the returned value is of primitive type (int etc.), it is represented in the appropriate Java wrapper type (Integer etc.). In the case that the returned value is an object type then an appropriate BObject will be returned, allowing the returned object itself to be placed on the BlueJ object bench.
    Author:
    Clive Miller, University of Kent at Canterbury, 2002, Damiano Bolla, University of Kent at Canterbury 2003
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getDeclaringClass()
      Returns the class's name that declares this method.
      int getModifiers()
      Returns the modifier of this method.
      java.lang.String getName()
      Returns the name of this method.
      java.lang.Class<?>[] getParameterTypes()
      Returns the types of the parameters of this method.
      java.lang.Class<?> getReturnType()
      Returns the return type of this method.
      java.lang.Object invoke​(BObject onThis, java.lang.Object[] params)
      Invokes this method on the given object.
      boolean matches​(java.lang.String methodName, java.lang.Class<?>[] parameter)
      Tests if this method matches against the given signature.
      java.lang.String toString()
      Returns a string representing the return type, name and signature of this method
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • matches

        public boolean matches​(java.lang.String methodName,
                               java.lang.Class<?>[] parameter)
        Tests if this method matches against the given signature. Pass a zero length parameter array if the method takes no arguments.
        Parameters:
        methodName - the name of the method to compare this method with.
        parameter - an array of Class objects representing the signature to compare this method with.
      • getDeclaringClass

        public java.lang.String getDeclaringClass()
        Returns the class's name that declares this method. Similar to Reflection API.
      • getParameterTypes

        public java.lang.Class<?>[] getParameterTypes()
        Returns the types of the parameters of this method. Similar to Reflection API.
        Returns:
        An array of Class objects representing the return type of this method.
      • getName

        public java.lang.String getName()
        Returns the name of this method. Similar to Reflection API.
      • getReturnType

        public java.lang.Class<?> getReturnType()
        Returns the return type of this method. Similar to Reflection API.
        Returns:
        A Class object representing the return type of this method.
      • getModifiers

        public int getModifiers()
        Returns the modifier of this method. The Modifier class can be used to decode the modifiers.
        Returns:
        An int value representing the modifiers which can be decoded with java.lang.reflect.Modifier.
      • invoke

        public java.lang.Object invoke​(BObject onThis,
                                       java.lang.Object[] params)
                                throws ProjectNotOpenException,
                                       PackageNotFoundException,
                                       InvocationArgumentException,
                                       InvocationErrorException
        Invokes this method on the given object. Note that this method should not be called from the JavaFX (GUI) thread.

        The arguments passed in the initargs array may have any type, but the type will determine exactly what is passed to the method:

        • String - the String will be passed directly to the method
        • BObject - the object will be passed directly to the method, though it must be on the object bench for this to work
        • Anything else - toString() is called on the object and the result is treated as a Java expression, which is evaluated and passed to the method.

        An attempt is made to ensure that the argument types are suitable for the method. InvocationArgumentException will be thrown if the arguments are clearly unsuitable, however some cases will generate an InvocationErrorException instead. In such cases no expression arguments will be evaluated.

        If the method invoked is public static void main(String [] args), then the invocation will, as a side-effect, reset the VM used by BlueJ to run user code in this project, and clear the object bench. This behaviour matches the effect of invoking a main method through the BlueJ GUI.

        Parameters:
        onThis - a BObject object on which the method call should be applied, null if a static method.
        params - an array of Object objects containing the arguments, or null if there are none
        Returns:
        The resulting Object object. It can be a wrapper for a primitive type or a BObject
        Throws:
        ProjectNotOpenException - if the project to which this object belongs has been closed by the user.
        PackageNotFoundException - if the package to which this object belongs has been deleted by the user.
        InvocationArgumentException - if the params don't match the object's arguments.
        InvocationErrorException - if an error occurs during the invocation.
      • toString

        public java.lang.String toString()
        Returns a string representing the return type, name and signature of this method
        Overrides:
        toString in class java.lang.Object