3

I want to test if two lambda methods are equal; For example:

public class App {

    @FunctionalInterface
    public interface TestFunctionInterface {
        void get();
    }

    public static void main(String[] args) throws Throwable {
        TestFunctionInterface t1 = App::print1;
        TestFunctionInterface t2 = App::print1;
        TestFunctionInterface t3 = App::print2;
        System.out.println(t1.equals(t2));
        System.out.println(!t1.equals(t3));
    }

    private static void print1() {
    }

    private static void print2() {
    }
}

output:

false

true

My aim is to validate that the two lambda function are the same method.

i.e. having a test that returns true.

In this way, can I get method info from the FunctionalInterface?

Thanks

Docteur
  • 1,235
  • 18
  • 34
inothing
  • 31
  • 2
  • 1
    also related http://stackoverflow.com/questions/19845213/how-to-get-the-methodinfo-of-a-java-8-method-reference – Radiodef Apr 24 '15 at 10:25
  • 2
    [This answer](http://stackoverflow.com/a/21879031/2711488) shows how you could get the target method in a specific case but [here](http://stackoverflow.com/a/26656912/2711488) you can learn why this is a bad idea. Simply said, the target method of the implementation isn’t always the method reference’s target as specified in the source. And there might be multiple different synthetic target methods for the same actual target… – Holger Apr 24 '15 at 10:42

0 Answers0