Hi There,

Let me address your problem. You get strings from a database for
a> Class Name (say "c" )  &
b> Method name (say "m" )

Now you want to invoke method "m" on an object of class "c" . But the thing
is that you don't have an instance of this class. So here is what you can do
:-

Class classObject = Class.forName( "c" );


Object objInstance = classObject.newInstance();

Now to invoke any method on an object on of the two things are necesary...
1> Either you know what parameters the methos will intake, in this case you
could have easily done something like

    Class[] parameterList = new Class[n];
<<block to assign values to this array >>

    Method m = classObject.getMethod("m", parameterList);

Here 'n' is the number of parameters. You populate it and pass it.

Now you can invoke this method as follows..

Object[] parameters = new Object[n];
<<block to assign values to this array >>

Object result = m.invoke(objInstance, parameters);

Thus your task is accomplished...

2> You don't know the exact method signature but your have some logic to
meet any signature,

Then you can get the list of parameters types as

Class[] pClasses = m.getParameterTypes();

Based upon this array you can form an array of parameter objects using your
logic and invoke the method as in the previous case........



I hope that this will help you.


Regards,

Daljit Singh Dhillon.




=======================
  It's never too late to begin
=======================
----- Original Message -----
From: Thanasis Papakostas <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, June 26, 2000 3:41 PM
Subject: Urgent problem-Please help!


> Hi,
>     I 'm working on a project and I have encountered a problem. "I have
> to call a method of a class."
>     It seems quite simple till here. However the name of the class and
> the method will be known at run-time.
>     They will be retrieved from a database as Strings. The communication
> beween the classes is handled by CORBA.
>     The Reflection and Introspection APIs aren't suitable here because I
> don't even know the name of the class at
>     deployment time in order to get a reflection of the constructors,
> methods and Fields. My actual question is
>     "Can I call a method whose name is stored in a String? If Yes HOW?"
>
>     Thanks in advance
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to