As a follow-up question strongly related to Holgers' solution, why does uncommenting the override break the working code below?
public static interface StringFunction<N extends Number> extends Function<String, N> {
// @Override
// N apply(String t);
}
This works only, if the comments above are not removed:
public static <N extends Number> StringFunction<N> create(Class<N> type) throws Throwable {
MethodType methodType = MethodType.methodType(type, String.class);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle = lookup.findConstructor(type,
MethodType.methodType(void.class, String.class));
StringFunction<N> f = (StringFunction<N>) LambdaMetafactory.metafactory(lookup, "apply",
MethodType.methodType(StringFunction.class),
methodType.generic(), handle, methodType).getTarget().invokeExact();
return f;
}
public static void main(String[] args) throws Throwable {
System.out.println(create(Byte.class).apply("1"));
System.out.println(create(Short.class).apply("2"));
System.out.println(create(Integer.class).apply("3"));
System.out.println(create(Long.class).apply("4"));
}
The runtime complains about:
Exception in thread "main" java.lang.AbstractMethodError:
Method LambdaFun$$Lambda$1.apply(Ljava/lang/String;)Ljava/lang/Number; is abstract
at LambdaFun$$Lambda$1/856419764.apply(Unknown Source)
at LambdaFun.main(LambdaFun.java:28)