Skip to content

Instantly share code, notes, and snippets.

@Bill
Created June 14, 2019 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bill/2c5793c6bf410503906f7c26d9a1472d to your computer and use it in GitHub Desktop.
Save Bill/2c5793c6bf410503906f7c26d9a1472d to your computer and use it in GitHub Desktop.
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