Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the MethodInfo of a Java 8 method reference?

Please have a look at the following code:

Method methodInfo = MyClass.class.getMethod("myMethod"); 

This works, but the method name is passed as a string, so this will compile even if myMethod does not exist.

On the other hand, Java 8 introduces a method reference feature. It is checked at compile time. It is possible to use this feature to get method info?

printMethodName(MyClass::myMethod); 

Full example:

@FunctionalInterface private interface Action {      void invoke(); }  private static class MyClass {      public static void myMethod() {     } }  private static void printMethodName(Action action) { }  public static void main(String[] args) throws NoSuchMethodException {     // This works, but method name is passed as a string, so this will compile     // even if myMethod does not exist     Method methodInfo = MyClass.class.getMethod("myMethod");      // Here we pass reference to a method. It is somehow possible to     // obtain java.lang.reflect.Method for myMethod inside printMethodName?     printMethodName(MyClass::myMethod); } 

In other words I would like to have a code which is the equivalent of the following C# code:

    private static class InnerClass     {         public static void MyMethod()         {             Console.WriteLine("Hello");         }     }      static void PrintMethodName(Action action)     {         // Can I get java.lang.reflect.Method in the same way?         MethodInfo methodInfo = action.GetMethodInfo();     }      static void Main()     {         PrintMethodName(InnerClass.MyMethod);     } 
like image 247
Rafal Avatar asked Nov 07 '13 19:11

Rafal


People also ask

How do you replace lambda expression with method reference?

If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference. In the first two cases, the method reference is equivalent to lambda expression that supplies the parameters of the method e.g. System.

What are three ways for method reference?

Types of Method ReferencesStatic Method Reference. Instance Method Reference of a particular object. Instance Method Reference of an arbitrary object of a particular type. Constructor Reference.


1 Answers

No, there is no reliable, supported way to do this. You assign a method reference to an instance of a functional interface, but that instance is cooked up by LambdaMetaFactory, and there is no way to drill into it to find the method you originally bound to.

Lambdas and method references in Java work quite differently than delegates in C#. For some interesting background, read up on invokedynamic.

Other answers and comments here show that it may currently be possible to retrieve the bound method with some additional work, but make sure you understand the caveats.

like image 85
Mike Strobel Avatar answered Sep 24 '22 09:09

Mike Strobel