Help coderanch get a
new server
by contributing to the fundraiser
  • 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Is there a way to generate a .class object from Class<?>, without hardcoding the cast?

Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my problem:

Right now, I'm able to read in the name of a class as a String (i.e., com.blah.blah.blah,XXX) from a .properties file, and convert it to a Class<?> instance via Class.forName(). I've got a utility class that has a method that will generate an object of a specified class (the signature is <T> T getObject(final Class<T> obj). I'd like to pass the Class<?> object to the method, but I need to cast the result to get the right type. Is there a way to generate a .class object from an instance of Class<?>, without hard-coding the casting? Thanks in advance.

He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic