Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access instance that 'owns' a method in java?

in java, is it possible to access the instance to which a method belongs, given only the method?

for example:

public class ClassA {
    private ClassB instanceB = new ClassB();
    // ...
    private void sendMethod () {
        instanceB.receiveMethod(foo);
    }
    public void foo () {}
}

public class ClassB {
    public void receiveMethod (Method method) {
        Object o = foo.getInstanceOwner();  // just made that part up...
    }
}

my feeling is that methods belong to classes, not instances of a class, so the answer is no, but maybe there's some sneaky reflection technique i don't know about. i could always pass 'this' along with method foo, but that seems like extra baggage.

like image 339
ericsoco Avatar asked Feb 27 '26 23:02

ericsoco


1 Answers

Taken from

A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).

A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

You can call Method#invoke but you will need the instance of the object you want to call the method on, from the method doc:

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary. If the underlying method is static, then the specified obj argument is ignored. It may be null.

If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null.

If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur.

If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. However, if the value has the type of an array of a primitive type, the elements of the array are not wrapped in objects; in other words, an array of primitive type is returned. If the underlying method return type is void, the invocation returns null.

So the TL:DR is unless you have the actual object you want you call the method on, it is not possible.

like image 79
CarlosZ Avatar answered Mar 01 '26 12:03

CarlosZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!