Get parametrized type in runtime


/ Published in: Java
Save to your folder(s)

This code will only work for immediate specialization not something like class B extends A.


Copy this code and paste it in your HTML
  1. class A<T> {
  2.  
  3. Class<T> specializedClass;
  4.  
  5. public A() {
  6. this.specializedClass = (Class<T>) ((ParameterizedType) this.getClass()
  7. .getGenericSuperclass()).getActualTypeArguments()[0];
  8. }
  9.  
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.