1

I created a class (class Special) whose method (Special.method()) will react differently depending on the method that calls Special.method(). so, lets say method X in some class calls Special.method(), if certain annotation is present in method X, the calc process invoked inside Special.method() will be different when such annotations didnt exist in the calling method. Also, since I'll be using third party library, it's not guaranteed that same thread will be used when calling Special.method() and method X.

I want to know ways to get reference to an instance method in Java 7

public class MyClass{
    public void myMethod(){
        ....
    }
}

I know I can do this

MyClass.class.getMethod(methodName);

but this technique is prone to error since it relies on String input (i.e. when method name is changed, etc). Is there a more reliable way to refer to a method?

Thanks

Architucas
  • 129
  • 10
  • To which method? How do you want to describe it? – Pshemo Dec 27 '15 at 03:20
  • in this case, to myMethod method. I need an alternative to MyClass.class.getMethod("myMethod"); – Architucas Dec 27 '15 at 03:22
  • Is it possible that there will be more methods? In that case how do you want to describe which one you need? – Pshemo Dec 27 '15 at 03:23
  • yes, it is possible that there will be more methods. but I'm fine with making one reference variable for each method. Although, I'd prefer if there's a clever way to refer to the methods dynamically. Thanks – Architucas Dec 27 '15 at 03:24
  • There really isn't, unfortunately. The new functional interfaces introduced in Java 8 can help, but mostly it's reflection or bust. Especially for the way you describe your problem. – markspace Dec 27 '15 at 03:37

2 Answers2

1

Java doesn't support method references without some fairly gnarly reflection. Java has functional interfaces that can work like references, but due to their targeted usage, they all take or return at least one value. There's no interface for methods which take no arguments and return void like you have in your example.

 // Assignment context
 Predicate<String> p = String::isEmpty;

Will declare a method reference to the String#isEmpty() method, which returns a boolean. That and similar interfaces exist in the java.lang.function package.

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html


Regarding your edit: If you want to find the caller of your method, see here:

How do I find the caller of a method using stacktrace or reflection?

Community
  • 1
  • 1
markspace
  • 10,621
  • 3
  • 25
  • 39
  • "There's no method for `void` methods" is little confusing. Could you elaborate it? Either I misunderstood you, or something like `Consumer getter = MyClass::getMethod()` solves that problem. – Pshemo Dec 27 '15 at 03:48
  • Yes, I mean that there's no interface for a method which takes no arguments and returns nothing. I'll try to edit. In your example, `Consumer` represents a method that takes a `String` argument. – markspace Dec 27 '15 at 03:49
  • *"There's no interface for methods which take no arguments and return void like you have in your example."* [Yes, there is.](http://stackoverflow.com/a/43542697/157247) – T.J. Crowder Apr 21 '17 at 12:26
0

The currently-accepted answer is incorrect: There is a functional interface compatible with your myMethod (e.g., accepting no parameters, with a void return type), it's just not in java.util.function, it's in java.lang: Runnable:

public class MyClass{
    public void myMethod(){
        System.out.println("myMethod was called");
    }
}
class Example
{
    public static void main (String[] args)
    {
        MyClass c = new MyClass();
        Runnable r = c::myMethod;                // <===
        r.run();                                 // <===
    }
}

Live on IDEOne

They just didn't duplicate it in java.util.function.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875