Last active
August 29, 2015 14:25
-
-
Save danieldietrich/9f65eeec3d3f822c852f to your computer and use it in GitHub Desktop.
What the lambda? #WTL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class JavaslangGoodies { | |
static <T, R> MethodType getType(SerializableFunction<T, R> function) throws Exception { | |
// FIXED, DON'T CHECK FOR CAPTURED LAMBDA PARAMETERS | |
return getLambdaSignature(function); | |
} | |
static SerializedLambda getSerializedLambda(Serializable lambda) throws Exception { | |
final Method method = lambda.getClass().getDeclaredMethod("writeReplace"); | |
method.setAccessible(true); | |
return (SerializedLambda) method.invoke(lambda); | |
} | |
static MethodType getLambdaSignature(Serializable lambda) throws Exception { | |
// FIXED, WAS: .getImplMethodSignature(); | |
final String signature = getSerializedLambda(lambda).getInstantiatedMethodType(); | |
return MethodType.fromMethodDescriptorString(signature, lambda.getClass().getClassLoader()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.lang.invoke.MethodType; | |
import java.lang.invoke.SerializedLambda; | |
import java.lang.reflect.Method; | |
import java.util.function.Function; | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
SerializableFunction<Integer, Integer> add1 = i -> i + 1; | |
System.out.println(JavaslangGoodies.getType(add1)); // (Integer)Integer | |
SerializableFunction<Integer, Integer> add2 = Test::add; | |
System.out.println(JavaslangGoodies.getType(add2)); // (Integer)Integer | |
SerializableFunction<Integer, Integer> add3 = add1::apply; | |
System.out.println(JavaslangGoodies.getType(add3)); // (Object)Object | |
} | |
static Integer add(Integer i) { | |
return i + 1; | |
} | |
interface SerializableFunction<T, R> extends Function<T, R>, Serializable { | |
default int arity() { | |
return 1; | |
} | |
} | |
static class JavaslangGoodies { | |
static <T, R> MethodType getType(SerializableFunction<T, R> function) throws Exception { | |
final MethodType methodType = getLambdaSignature(function); | |
return methodType.dropParameterTypes(0, methodType.parameterCount() - function.arity()); | |
} | |
static SerializedLambda getSerializedLambda(Serializable lambda) throws Exception { | |
final Method method = lambda.getClass().getDeclaredMethod("writeReplace"); | |
method.setAccessible(true); | |
return (SerializedLambda) method.invoke(lambda); | |
} | |
static MethodType getLambdaSignature(Serializable lambda) throws Exception { | |
final String signature = getSerializedLambda(lambda).getImplMethodSignature(); | |
return MethodType.fromMethodDescriptorString(signature, lambda.getClass().getClassLoader()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment