• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting and calling methods in runtime  RSS feed

 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • 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
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Good for you, comrade!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!