Created
August 16, 2017 04:52
-
-
Save onlyeat3/a602d8e0c5cde7eb3d4a12b3d39f04c1 to your computer and use it in GitHub Desktop.
获取lambda表达式的元信息
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
@FunctionalInterface | |
public interface A<T, R> extends Function<T, R>, Serializable { | |
} |
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
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; | |
} | |
} |
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
public class Entity { | |
private Integer id; | |
public Integer getId() { | |
return id; | |
} | |
} |
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
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); | |
} | |
} |
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 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