Method CLASS

A method member defined on a type.

A method can be either static or instance-scoped, with the latter representing a piece of logic shared among the instances of that class. When calling a method through reflection, an instance method requires specifying the invocation target, namely the instance against which the method is to be performed.

There are two manners in which a method can be called by reflection. The first is to call with dynamic dispatching. This approach shows semantic behaver similar to that of method calling in source code: while the Method instance may be obtained from a certain class A, the method to be called on against an instance b may be a different one.

  class Machine {
    void run(){
      Console.println("machining running...");
    }
  }
  class Car : Machine {
    void run(){
      Console.println("car running...");
    }
  }

  Method m = typeof(Machine).getMethod("run")[0];
  Car c = new Car();
  m.call(new var[]{c}); // car running...


To call without dynamic dispatching, use callExact() instead. In this case the exact logic represented by the Method instance will be invoked against the given object. This usually means a more narrow condition on which the call would be allowed. Most prominently, the object must be of a derived type of the method's defining type, and the method must be visible throughout the hierarchy.

Parent Class

Parent Interfaces

All Members


TypeNameSignature
methodbindpublic Function bind(var)
methodcallpublic var call(Any[])
methodcallExactpublic var callExact(Any[])
methodgetAttributespublic Attribute[] getAttributes()
methodgetKindpublic MemberKind getKind()
methodgetNamepublic String getName()
methodgetParameterspublic Parameter[] getParameters()
methodgetReturnTypepublic Type getReturnType()
methodisStaticpublic bool isStatic()
methodtoStringpublic String toString()

Methods


public Function bind(var target)

Bind this method to an instance to get a callable Function object. This has a similar effect to getting the Function directly from an object with dot operator.

  class Car {
    void run(){ }
  }

  Car car = new Car;
  Function f0 = car.run;
  // The following has a similar effect
  Function f1 = car.getType().getMethod("run")[0].bind(car);

  // In this case, the two are actually the same object. But this is not always the case.
  // For example, if the method is overloaded, the dot operator will result in a method
  // group object, while bind() always wraps a single method.
  Console.println(f0 == f1);


The binding is based on the same algorithm as dynamic dispatching.

Parameters

  • target The instance to bind to. If this is a static method, this argument is disregarded.

Returns

  • A Function object that can be invoked by function-call syntax.

public var call(var[] args)

Call the method with dynamic dispatching.

A call by dynamic dispatching behaves semantically similar to in-source method invocation through method call operator, i.e. fun(). The most significant implication is that the method to be actually invoked is not necessarily the one represented by this Method object. Instead, reflection API's internal logic will determine the appropriate method implementation in the context of given instance target. More concretely, there are several situations in consideration.

(1) If the method's defining type (to be referred as "defining type") is same to the instance's runtime type (to be referred as "instance type"), the method will be called as is.
(2) If instance type inherits from defining type, the actual method implementation of same signature on instance type will be called. This implies the call will fault if the method is private.
(3) If defining type inherits from instance type, the actual method implementation of same signature on instance type, should it exist, will be called.
(4) In all other cases ReflectedInvocationException will be thrown.

Parameters

  • args The argument array. If this is an instance method, the first one must be the instance target, whose runtime type is subject to various restrictions.

Returns

  • The result returned by the method. If a method returns void, the only proper way of dealing with it is to either ignore the result or return it if the calling function's return type is also void.

Throws

  • System.Reflection.ReflectedInvocationException This exception can be thrown for a variety of reasons. If a pre-check failed on the type of arguments or calling legality (such as attempting to call an abstract method), the exception contains the error information directly. If the method being invoked threw, this exception was created as a wrapper and the caller must call getCause() to get the original exception.

public var callExact(var[] args)

Call the method without dynamic dispatching.

Since the exact method must be invoked, the instance object must be either of the same type as the method's defining type, or a subtype derived from the defining type. In the case of latter, it's also required that the method be visible to the class to which the object belongs. In all other cases ReflectedInvocationException will be thrown.

Parameters

  • args The argument array. If this is an instance method, the first one must be the instance target, whose runtime type is subject to various restrictions.

Returns

  • The result returned by the method. If a method returns void, the only proper way of dealing with it is to either ignore the result or return it if the calling function's return type is also void.

Throws

  • System.Reflection.ReflectedInvocationException This exception can be thrown for a variety of reasons. If a pre-check failed on the type of arguments or calling legality (such as attempting to call an abstract method), the exception contains the error information directly. If the method being invoked threw, this exception was created as a wrapper and the caller must call getCause() to get the original exception.

public Attribute[] getAttributes()

(INHERITED DOC)

Get all the attributes annotated on the member definition.

The result array contains each instance of Attribute placed at the type definition.

Returns

  • The attributes annotated on this member. In case of members which do not have attributes, this returns a 0-sized Attribute array.

public MemberKind getKind()

A method's kind is METHOD.

Returns


public String getName()

Get the name of this method. Note there could be multiple methods possessing the same name due to class inheritance and method overloading.

Returns

  • The name of this method

public Parameter[] getParameters()

Get parameters of this method. The implicit first argument for instance-scoped methods is not included.

Returns


public Type getReturnType()

Get the return type of this method.

Returns

  • The return type.

public bool isStatic()

Whether this method is static or instance-scoped.

Returns

  • True if the method is static; false instance-scoped.

public String toString()

Get a string representation of this method member.

Returns

  • A string in form of [METHOD|method-signature]. method-signature contains method name and each parameter's type. Example: [METHOD|fun(int, MyModule.MyClass)]