This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Generics Warning

 
aanal jethalia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a class which has a static method that takes a String classname and returns the instance of type class. My method looks like:

public static <T extends Object> T getClassInstance(String className) throws Exception{
T instanceToReturn = null;

try {
Class c = Class.forName(className);
instanceToReturn = (T)c.newInstance();
} catch (InstantiationException e) {
throw e;
} catch (IllegalAccessException e) {
throw e;
} catch (ClassNotFoundException e) {
throw e;
}
return instanceToReturn;
}

I get the following warning:

Type safety: The cast from Object to T is actually checking against the erased type Object. Should I be worried about this warning? How can I avoid it?

I am very new to Generics, so am not sure if this is the correct way to use it. Any help would be great.

Thanks
Aanal
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sadly, the only way to get rid of that warning is by adding a @SuppressWarnings annotation to the method.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there's anything sad about it. Ultimately, you get a warning because within that method, there's really no way to know that the String argument passed in will be compatible with the type T which must be inferred from the return type. If someone calls this method with

String x = getClassInstance("java.lang.Integer");

it will fail, period. Whereas

String x = getClassInstance("java.lang.String");

works fine. Only the calling code can determine if this will work; thus, it's in the calling code that the decision should be made as to whether to cast or not. Best to just drop the generic type T, and declare the method to return an Object. That's the only thing you can guarantee from within the method. Let the caller decide whether a cast is appropriate.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a different remark on your code:

Why are you catching all those exceptions if you're immediately re-throwing them? You don't need the try-catch block, your code looks cleaner and shorter if you leave it off:

Or you could make it even shorter:
 
aanal jethalia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrap the Exception with my application specific Exception and throw it again. Thats why I catch and throw.

Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic