File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generic Factory to create instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic Factory to create instance" Watch "Generic Factory to create instance" New topic
Author

Generic Factory to create instance

vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
My purpose is to create a generic factory. The code belowed works, but what annoying me is (1) the warning of unchecked conversion, and (2) I must create and initialize any instance of HashMap.
Map<String, Set<Object>> m = new HashMap<String, Set<Object>>();

Maybe it is not a factory since I already create any instance. Are there ways to get around this?
Thanks



public class InstanceCreation<T> {
public static<T> T create(Class<T> cls) throws Exception{
T instance = cls.newInstance();
return instance;
}
public static void main(String[] argv){
Map<String, Set<Object>> m = new HashMap<String, Set<Object>>();
try{

Map<String, Set<Object>> map = create(m.getClass());
map.put("hello", new HashSet<Object>());
System.out.println(map.keySet());
}catch(Exception ex){
ex.printStackTrace();
}
}
}

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,

According to me,


Now if compiler allows us following:
Map m = new HashMap(); ---> m.getClass() ---> HashMap

but in the method create you are using:
Class<T> --> <T> --> ???
passing only HashMap will give nothing, So as per compiler point of view it should be

HashMap< ...>

point here is that T and <T> are different..!!

T ---> HashMap

and
<T> is <String,Set<Object>>

so overall you are returning type T from create method, that lead us to
Map<String, Set<Object>> map = create(m.getClass() ===> new HashMap();

Thats why you are getting the Unchecked Warning..

I think you should try

T<T> instanse = cls.newInstance();

I dont have JAVA5 on my system, So I can not compile this program for you

Please correct me If I am wrong


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

I am sorry, Please ignore the my earlier post:
Check this:

Hi,

According to me,


Now if compiler allows us following:
Map<String, Set<Object>> m = new HashMap<String, Set<Object>>(); ---> m.getClass() ---> HashMap<String, Set<Object>>

but in the method create you are using:
Class<T> --> <T> --> <String, Set<Object>>


That is why it is asking you to define m with type parameter...
if you change :

public static<T> T create(Class cls) throws Exception{
T instance = cls.newInstance();
return instance;
}

It should not ask...

point here is that T and <T> are different..!!

T ---> HashMap

and
<T> is <String,Set<Object>>


so overall you are returning type T from create method, that lead us to
Map<String, Set<Object>> map = create(m.getClass() ===> new HashMap();

Thats why you are getting the Unchecked Warning..

I think you should try

T<T> instanse = cls.newInstance();

I dont have JAVA5 on my system, So I can not compile this program for you

Please correct me If I am wrong
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

The problem is that Class objects don't have any information on generics. Even worse (for some), generics don't exist at runtime at all - it's a compile time mechanism.

So although you pass the class of the map, all it knows is that it is java.util.HashMap - the type it stores is "forgotten" once you have compiled the code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Factory to create instance
 
Similar Threads
Generics Implementation in Java SE 5
Hairy about generics (Challenge)
Help me see if possible Generic DAO Factory
converting a string color to an object color
Generic Singleton Class