Skip to content

Instantly share code, notes, and snippets.

@NaanProphet
Created June 3, 2016 22:05
Show Gist options
  • Save NaanProphet/ae513558c97f410891fa2e863c504ae2 to your computer and use it in GitHub Desktop.
Save NaanProphet/ae513558c97f410891fa2e863c504ae2 to your computer and use it in GitHub Desktop.
Java obtain generic type at runtime
// This statement is magic
// If you declare it: Set<String> getMyGeneric; it returns java.lang.String
final Class<?> genericClazz = (Class<?>)((ParameterizedType) getClass().getDeclaredField("getMyGeneric").getGenericType()).getActualTypeArguments()[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment