Created
June 14, 2019 21:08
-
-
Save Bill/2c5793c6bf410503906f7c26d9a1472d to your computer and use it in GitHub Desktop.
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 static net.jodah.typetools.TypeResolver.resolveRawArgument; | |
public class Core { | |
interface Handler<T> { | |
void process(T x); | |
} | |
void p1(final String s) {} | |
void p1(final int i) {} | |
<T> void register(final Handler<T> h) { | |
final Class<T> clazz = classForHandler(h); | |
System.out.println(clazz); | |
} | |
@SuppressWarnings("unchecked") | |
private static <T> Class<T> classForHandler(final Handler<T> h1) { | |
return (Class<T>)resolveRawArgument(Handler.class, h1.getClass()); | |
} | |
public static void main(final String[] args) { | |
final Core core = new Core(); | |
final Handler<String> h1 = core::p1; | |
final Handler<Integer> h2 = core::p1; | |
core.register(h1); | |
core.register(h2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment