Skip to content

Instantly share code, notes, and snippets.

@onlyeat3
Created August 16, 2017 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onlyeat3/a602d8e0c5cde7eb3d4a12b3d39f04c1 to your computer and use it in GitHub Desktop.
Save onlyeat3/a602d8e0c5cde7eb3d4a12b3d39f04c1 to your computer and use it in GitHub Desktop.
获取lambda表达式的元信息
@FunctionalInterface
public interface A<T, R> extends Function<T, R>, Serializable {
}
public class Appender<T> {
private T t;
public <R> R append(A<T, R> a) throws ReflectiveOperationException {
a.apply(this.t);
Method methods = a.getClass().getDeclaredMethod("writeReplace");
methods.setAccessible(true);
SerializedLambda lambda = (SerializedLambda) methods.invoke(a);
System.out.println("lambdaClassName:" + lambda.getImplClass());
System.out.println("lambdaMethodName:" + lambda.getImplMethodName());
return (R) new Object();
}
public T getT() {
return t;
}
public Appender setT(T t) {
this.t = t;
return this;
}
}
public class Entity {
private Integer id;
public Integer getId() {
return id;
}
}
public class MainTest {
public static void main(String[] args) throws ReflectiveOperationException {
Appender<Entity> a = Wrapper.builder()
.build()
.wrap(Entity.class);
a.setT(new Entity());
a.append(Entity::getId);
}
}
import lombok.Builder;
/**
* @author Frank
*/
@Builder
public class Wrapper {
public <T> Appender<T> wrap(Class<T> clazz) throws IllegalAccessException, InstantiationException {
return new Appender<>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment