Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Generics, factories and reflection

Thread view: 
Martin - 19 Sep 2006 12:41 GMT
Hi.

I have a problem with a factory method. This method should create
instances of the collection framework. The simplest implementation
looks like this:

--------------------------------------------------------------
public static List<E> createList() {
   return new ArrayList<E>();
}
List<String> l1 = createList();
--------------------------------------------------------------

But in my special case, I want to (and have to) pass the implementing
class as an argument. So the factory could create instances of
ArrayList but also of LinkedList. The following method compiles without
warnings and errors:

--------------------------------------------------------------
public static List<E> createList(Class<? extends List<E>> listImpl) {
   try {
       List<E> l = listImpl.newInstance();
       return l;
   }
   catch (Exception dontBotherMe) {
       return null;
   }
}
--------------------------------------------------------------

But how can I call this method?

List<String> l2 = createList(ArrayList.class)

does not work, because "Class<ArrayList>" does not match
"Class<List<E>>".
Thomas Hawtin - 19 Sep 2006 13:12 GMT
> public static List<E> createList() {
public static <E> List<E> createList() {

You need to declare the generic parameter.

> But in my special case, I want to (and have to) pass the implementing
> class as an argument. So the factory could create instances of
[quoted text clipped - 6 lines]
>         List<E> l = listImpl.newInstance();
>         return l;

I strongly suggest you don't use Class.newInstance. That method is evil.
Much better to write an abstract factory interface. Also, important for
this use, Class represents an erased type. In any case, creating a
LinkedList is likely to be a bad idea.

>     }
>     catch (Exception dontBotherMe) {
>         return null;
>     }
> }

Nice error handling...

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.