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 likeString.length()
, a one-parameter method bound to an instance like"a,b,c".equals(Object)
, or a static method with one argument likeObjects.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"
Similar Kata:
Stats:
Created | Jan 24, 2022 |
Published | Jan 24, 2022 |
Warriors Trained | 119 |
Total Skips | 69 |
Total Code Submissions | 154 |
Total Times Completed | 5 |
Java Completions | 5 |
Total Stars | 4 |
% of votes with a positive feedback rating | 100% of 2 |
Total "Very Satisfied" Votes | 2 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 2 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 4 kyu |