• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generic Factory to create instance

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic