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
Type | Name | Signature |
---|---|---|
method | bind | public Function bind(var) |
method | call | public var call(Any[]) |
method | callExact | public var callExact(Any[]) |
method | getAttributes | public Attribute[] getAttributes() |
method | getKind | public MemberKind getKind() |
method | getName | public String getName() |
method | getParameters | public Parameter[] getParameters() |
method | getReturnType | public Type getReturnType() |
method | isStatic | public bool isStatic() |
method | toString | public String toString() |
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
Returns
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
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
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
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
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
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
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
public bool isStatic()
Whether this method is static or instance-scoped.
Returns
public String toString()
Get a string representation of this method member.
Returns
[METHOD|method-signature]
. method-signature
contains method name and each parameter's type. Example: [METHOD|fun(int, MyModule.MyClass)]