Skip to main content

Getting a Java Type Parameter class at runtime

Certain use cases like deserializing a collection requires knowing the type of the declared field. As the common understanding goes, Type Parameters are erased at build time. So How do we get them?

Its a common misconception that "Type Parameters are erased at build time" - Instead what is actually happening is - Generic collections are converted to raw types. The class definition is NOT modified at build/run time. The Type Parameter(s) can be accessed from the class definition through reflection.

For example:

Lets say here is a Bean

public class Document {
 private List<Model> models;
        ..
}

Given doc, an instance of Document, at runtime, getting the type of doc.models by traditional reflection, can only get that its type is java.util.List

//somewhere in runtime
Document doc = new Document();

 Field f = bean.getClass().getDeclaredField(field);
 f.setAccessible(true);
 assert f.getClass() == List.class

To get the type from parameter, Assuming we know that it is a ParameterizedType (like List<T>)

private <T> Class getListType(T bean, String field) throws NoSuchFieldException {
 Class childType;
 Field f = bean.getClass().getDeclaredField(field);
 f.setAccessible(true);
 childType = (Class) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0];
 return childType;
}

//somewhere in runtime
Document doc = new Document();
assert getListType(doc, "models") == Model.class


The same logic can be applied to get multi parameter types (like Map)

 keyType = (Class) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[0];
 valueType = (Class) ((ParameterizedType) f.getGenericType()).getActualTypeArguments()[1];

Popular posts from this blog

Appcache manifest file issues/caveats

Application cache (appcache) is a powerful feature in HTML5. However, it does come with baggage. Many (see links below) advocated ferociously against it due to tricky issues it comes with. For someone who is just testing waters, these issues may throw them off grid. Knowing them before hand helps reduce some unpredictable effects.

Being a Vegetarian

I am a Proud Vegetarian. I don't eat Meat or Eggs. People say its hard here in US to be one. I beg to differ. The mere fact that I am hail and healthy these 4 years is a definitive proof. Apart from being bullied and trash talked by The Meat-Eaters, There is really nothing that makes this choice of mine any more than a debatable issue at a lunch or dinner. Other things aside, I am writing this blog having watched a PETA Video. Before you click on the play button, I ask you - If you are a vegetarian : Dont watch it. If you are not : Dare to watch it till the end. If you think going veg is just a fashion, think again . Even if you just want to do it for Fashion . Do it. Go Vegetarian. And Feel better asking the waiter for a Vegetarian Entrée in your next lunch.

classpath*: making your Modular Spring Resources

Spring gives multiple options to load XML resources for building contexts. the reference documentation does explain this feature quite well. However, I am taking my shot at explaining the different practical scenarios ( by order of growing modularisation) For Example, A simplest Spring based web Context Loader can be configured with resources like this <context-param> <param-name>contextConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> You just need to put applicationContext.xml in WEB-INF/ folder of your webapp. However, Typically an application is n-tiered. You can also have multiple files setup and in relative paths. like <param-value> context-files/applicationContext.xml context-files/dao.xml context-files/service.xml </param-value>