• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

casting and calling methods in runtime

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I want to cast a class and call its method in runtime. Like
suppose i got Annotation class like
Annotation[] a = am.getClassAnnotations(et.getClass());
From this class i can get the name of class in which i have to cast like
for(int i=0;i<a.length;i++)
{
String className = a[i].annotationType().getName();
}
I can also know the method name like this
Method[] m = a[i].annotationType().getDeclaredMethods();
But now i wanna call these methods
I could do like
Entity e = (Entity)a[i];
e.name();
But i dont know the name of class during compile time.
Any suggestion how to achieve this.
Thanks and regards,
kundan
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Kundan

Look, aaah, I do not know if I am really getting what you want to do. I guess you can invoke the methods of a class by means of using reflection.

You simply need to use the java.lang.reflect.Method class and invoking its "invoke" method. But I guess you already know that.

May I suggest the use of the BeanShell API?

It comes with an excellent java interpreter capable of executing java code made out of strings built at runtime.

That might help you and release you from the resposability of writing all that reflection code.

Regards,
Edwin Dalorzo.
[ April 04, 2006: Message edited by: Edwin Dalorzo ]
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edwin,
Thanks Pal, I will study about bean shell script. OOppps to be very frank i forgot about invoke method...actually not but called it with wrong parameter and then push myself to try other stuff. But after getting your reply ,tried again and succeed.
Thanks again
kundan
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good for you, comrade!
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic