Beta

Get the method reference from the lambda

Description:

Together with the Stream-API Java 8 introduced Method Reference Expressions (see §15.13 of the Java language specification) to provide a convenient way of passing method references to functions and avoid creating anonymous classes like it was done before e.g. with UI event handlers.

The problem is that the lambda object created from the method reference seems to have lost all information about the class and method it was created from. You'll have to find a way to get this information somehow.

Task

Given a lambda which was created by a method reference (e.g. String::length) determine the name of the class and method and return just that string which you would need in the code to recreate this method reference. Methods which are bound to instances should only return the general reference to the class method (see example 3).

Input

  • a method reference created by a method reference expression. For the scope of this Kata we will limit the methods to be of type Function<T, R> with one parameter. This means the method is either a parameterless instance method like String.length(), a one-parameter method bound to an instance like "a,b,c".equals(Object), or a static method with one argument like Objects.requireNonNull(Object). Constructors will not be tested.

Output

  • a String for the method reference like it would appear in Java source code. Only the simple name of the class is needed, no package names or outer class names.

Examples

  • String::length should return "String::length"
  • Objects::requireNonNull should return "Objects::requireNonNull"
  • "abc"::equals should return "String::equals"
  • OuterClass.InnerClass::method should return "InnerClass::method"
Fundamentals

More By Author:

Check out these other kata created by Madjosz

Stats:

CreatedJan 24, 2022
PublishedJan 24, 2022
Warriors Trained119
Total Skips69
Total Code Submissions154
Total Times Completed5
Java Completions5
Total Stars4
% of votes with a positive feedback rating100% of 2
Total "Very Satisfied" Votes2
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments2
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • Madjosz Avatar
Ad