We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


Posted By

leazen on 07/04/06


Tagged

generics instrospection


Versions (?)


Get parametrized type in runtime


Published in: Java 


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

  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 

You need to login to post a comment.