I am working on a programme dynamically invoking a method from the
input. My program will receive a string including the class name and
the method name, For example, "classA.methodA". The parameters for the
method will also be passed into my programme. Then I should invoke the
method "methodA" in class "classA". The classA is written by others and
I don't know how it is implemented.
Now my problem comes.If the authors of the classes in which I will
invoke the methods use stand way of "ClassA a = new ClassA()" to
instantiate the class. It will be easy for me, since I can easily make
a new instance to complete the task. But some authors like to use
"factory method" to instantiate the class, it seems impossible for my
programme to know where are the "factory method". Factory methods can
be in any names without restriction.
So I wonder if there are some possible ways to complete my task? Thanks
for any suggestions!
PS: I am not working on Java very long and English is my second
language. So if there is anything unclear, please think free to point
it out. Thanks again.
Benji - 01 Nov 2005 10:19 GMT
> I am working on a programme dynamically invoking a method from the
> input. My program will receive a string including the class name and
> the method name, For example, "classA.methodA". The parameters for the
> method will also be passed into my programme. Then I should invoke the
> method "methodA" in class "classA". The classA is written by others and
> I don't know how it is implemented.
So, am I to understand that if I say "String.length()", you're going to
create a new String object via String's constructor, and call .length on
it? I'm not sure I see the purpose of this, but ok.
In that case, your quest is impossible. There is no way of knowing the
"correct" way of creating an object.
If what you really want is to directly interpret java code that is passed
in as a string, e.g. be able to execute "new Integer(5)", and get an
Integer value, there is already a project to do this:
http://www.beanshell.org/

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Sharp Tool - 01 Nov 2005 11:09 GMT
> I am working on a programme dynamically invoking a method from the
> input. My program will receive a string including the class name and
[quoted text clipped - 13 lines]
> So I wonder if there are some possible ways to complete my task? Thanks
> for any suggestions!
First of all, why dont you know what others are doing?
Resolve your communication problem.
If you guys don't want to communicate then maybe write an Interface for each
class.
And send the interface to everyone who plans to use the class.
For now, you could try instantiating ClassA using the new operator and test
if (it returns an instance of that class) else (use the factory method).
Sharp Tool
Oliver Wong - 01 Nov 2005 23:14 GMT
>I am working on a programme dynamically invoking a method from the
> input. My program will receive a string including the class name and
[quoted text clipped - 13 lines]
> So I wonder if there are some possible ways to complete my task? Thanks
> for any suggestions!
In theory, what you want to do is impossible. There is nothing in the
Java Language Specification or .class file format or anywhere else that
contains the information you need to determine whether a method is a factory
method or not.
In practice, you might be able to get by by looking at the methods, and
check if the return type is "classA" and, if so, just assume that it is a
factory method. Of course, those factory methods might require parameters,
and if you have no idea what the semantics are of those parameters, there's
no way for you to be sure what values are valid to pass in. You could just
pass in random values until an exception isn't thrown, and then just assume
you have a valid instance of classA...
As you can see, you need to make a lot of assumptions to solve this
problem, which is why the problem as it is originally stated is not possible
to solve. You need more information. If you could be given an instance of
classA in addition to the name of the method to invoke on it, then your task
would be a lot easier.
- Oliver
ChrisWSU - 02 Nov 2005 14:16 GMT
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#forName(java.lang.String)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getMethod(java.lang
.String,%20java.lang.Class...)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#newInstance()
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html#invoke(jav
a.lang.Object,%20java.lang.Object...)
this also might be useful
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
should be all you need, good luck