• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Generics Warning

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sadly, the only way to get rid of that warning is by adding a @SuppressWarnings annotation to the method.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic