You can check actual type arguments on runtime but you can read this only via reflection API from field, method, constructor etc. Sample below:
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
public class TypesSample {
public List<String> sampleList;
public static void main(String[] args) throws Exception {
Field f = TypesSample.class.getField("sampleList");
ParameterizedType paramType = (ParameterizedType)f.getGenericType();
Type typeArgument = paramType.getActualTypeArguments()[0];
System.out.println(paramType.toString() + " with argument : " + typeArgument.toString());
}
}
it says : java.util.List<java.lang.String> with argument : class java.lang.String
You won't get actual parameter type from object reference because of type erasure.
//edit:
This is what you can do for your situation:
abstract class GenericDAO<T> {
public void add(Collection<T> many) {
Type typeArg = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
System.out.println("This is a DAO for " + typeArg);
}
}
// create DAOs that define what type parameter is
class IntegerDAO extends GenericDAO<Integer> {}
class StringDAO extends GenericDAO<String> {}
and then :
GenericDAO<Integer> integerDAO = new IntegerDAO();
integerDAO.add(Arrays.asList(1,2,3));
GenericDAO<String> stringDAO = new StringDAO();
stringDAO.add(Arrays.asList("A","B","C"));
says:
This is a DAO for class java.lang.Integer
This is a DAO for class java.lang.String
But you need to explicitly declare what T is by extending generic class.