java - Return generic list -


i want load generic list. therefore want use function :

public static <t> list<t> load() {     fileinputstream filestream = null;     list<t> toreturn = new arraylist<t>();     string fileextension = toreturn.getclass().getcomponenttype().getsimplename();     ... } 

if can see want type ("t"), search files these extensions. if call :

storageutil.<myclass>load(); 

i exception instead of getting "myclass" fileextension. what's wrong on code?

the problem generics in java don't work way - can't find type of t @ execution time due type erasure. if need type @ execution time, can add class<t> parameter:

public static <t> list<t> load(class<t> clazz) {     ... use class.newinstance() create new instance, etc } 

see java generics faq entry type erasure more information.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -