I tried to do:
public class HelloWorld {
public static void main(String... args){
final String string = "a";
final Supplier<?> supplier = string::isEmpty;
System.out.println(supplier);
}
}
I get:
HelloWorld$$Lambda$1/471910020@548c4f57
I would like to get the string isEmpty. How can I do this?
EDIT: the code of the method I created is this one:
public class EnumHelper {
private final static String values = "values";
private final static String errorTpl = "Can't find element with value `{0}` for enum {1} using getter {2}()";
public static <T extends Enum<T>, U> T getFromValue(T enumT, U value, String getter) {
@SuppressWarnings("unchecked")
final T[] elements = (T[]) ReflectionHelper.callMethod(enumT, values);
for (final T enm: elements) {
if (ReflectionHelper.callMethod(enm, getter).equals(value)) {
return enm;
}
}
throw new InvalidParameterException(MessageFormat.format(errorTpl, value, enumT, getter));
}
}
The problem is I can't pass as parameter T::getValue, since getValue is not static. And I can't pass someEnumElem::getValue, since the get() will return the value of that element. I could use inside the for loop:
Supplier<U> getterSupllier = enm:getValue;
if (getterSupllier.get().equals(value)) {
[...]
}
but in this way getValue is fixed, I can't pass it as parameter. I could use some third-party library to do an eval(), but I really don't want to open that Pandora vase :D
EDIT 2: Function does work with no parameters methods, but only in Java 11. Unluckily I'm stuck with Java 8.
"Checking elements for HelloWorld$$Lambda$1/471910020@548c4f57", but"Checking elements for isEmpty". That would be great...toStringof some kind and an identity for lambdas or methods references, the designers did not do that on purpose IIRC.enums. I created a generic method that from an enum, a value and a getter name, it returns theenumelement with that value, returned by the getter specified. The only finesse is that I want to extract the name of the getter from the getter itself, not passing a string, like "getValue". This is because if the getter change in theenum, the compiler informs me I have to change it also when I invoke this method.Functionrather than a getter name? Then, instead of trying to find out the getter name, just to search for the right method to invoke, callapplyon the function, which will already invoke the right method.