forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdas.java
More file actions
54 lines (45 loc) · 1.62 KB
/
Copy pathLambdas.java
File metadata and controls
54 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal.asm;
import io.jooby.SneakyThrows;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
public class Lambdas {
// getting the SerializedLambda
private static SerializedLambda getSerializedLambda(Object function)
throws NoSuchMethodException {
for (Class<?> clazz = function.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method replaceMethod = clazz.getDeclaredMethod("writeReplace");
replaceMethod.setAccessible(true);
Object serializedForm = replaceMethod.invoke(function);
if (serializedForm instanceof SerializedLambda) {
return (SerializedLambda) serializedForm;
}
} catch (NoSuchMethodException e) {
// fall through the loop and try the next class
} catch (Exception t) {
throw SneakyThrows.propagate(t);
}
}
return null;
}
// getting the synthetic static lambda method
public static Method getLambdaMethod(ClassLoader loader, Object function) throws Exception {
SerializedLambda lambda = getSerializedLambda(function);
if (lambda != null) {
String implClassName = lambda.getImplClass().replace('/', '.');
Class<?> implClass = loader.loadClass(implClassName);//Class.forName(implClassName);
String lambdaName = lambda.getImplMethodName();
for (Method m : implClass.getDeclaredMethods()) {
if (m.getName().equals(lambdaName)) {
return m;
}
}
}
return null;
}
}