• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Dynamic casting using getMethod.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need the help. How I do dynamic casting using reflection ? I must to pass only the class name.

My code is it:

...
Class classe = null;
// father of TransactionProcessor
Module md = null;
Method method = null;
try{
classe = Class.forName(classname);
// getInstnce is the SingleTon class and return TransactionProcessor type
method = classe.getMethod("getInstance", null);
//md = ( TransactionProcessor ) method.invoke(null,null); //I CANT DO IT because it is a code generic.
md = ( ??? ) method.invoke(null,null);
md.run();
}catch(){};
I know that I can get the type of return using:
method.getReturnType().getName();
but I can do the dynamica casting ?
Thanks
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Fernanda,
Welcome to JavaRanch. Please do not cross post topics. Choose one forum per topic. Thank you for your cooperations.
Closing this. Please respond here.
 
This tiny ad is wafer thin:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic