Problem with methods & classloader

  • Thread starter Andrew Thompson
  • Start date
A

Andrew Thompson

Ok,
OK...

my program can load a class
Excellent..

as Runnable r = classloader.....
and then i can call r.run(); ....

Why are you using a classloader? Is there a special
reason you do not import the class?
ok that is working but...

OK/but.. never a good combination.
I want to give the class a vector with data
and get a processed vector back...

You want to use one of it's methods?
but how can i call the methods out of the loaded class

...by using the instance that you have loaded,
(unless it's a static method, of course)..
and what i have to change in my loading the class

is Object r = classloader better, because i don't want everytime
only to load a runnable class

So import it, instantiate an object and call the method
with a vector.
I hope somebody can help me...

Note that help is best obtained on a groip further described here..
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
A

Andreas Solsbach

Ok,

my program can load a class

as Runnable r = classloader.....
and then i can call r.run(); ....

ok that is working but...

I want to give the class a vector with data
and get a processed vector back...

but how can i call the methods out of the loaded class
and what i have to change in my loading the class

is Object r = classloader better, because i don't want everytime
only to load a runnable class


I hope somebody can help me...

By Andreas
 
G

Gordon Beaton

Yes, the class cotains a processingfunction and
when i start my agentsystem i don't know how the class
will look like so the user gives me as an input
the class.... and my system can process the data...

Why don't you require the user to provide a class that implements an
interface known to your application?

/gordon
 
A

Andreas Solsbach

Why are you using a classloader? Is there a special
reason you do not import the class?
Yes, the class cotains a processingfunction and
when i start my agentsystem i don't know how the class
will look like so the user gives me as an input
the class.... and my system can process the data...

thats why i have to call one method out of the loaded class
to give my data as input, then it will be processed by the
methods in the class and i get processed data back with an return

....

so import will not work... how can i call the correct method??

Object r = loader.loadClass(String.valueOf(myAgent.
getBehaviourData(3)), false).newInstance();
Method[] m = r.getClass().getDeclaredMethods();


r.getClass().getMethod(String.valueOf(myAgent.getBehaviourData(4)));

for (i = 0; i < m.length; ++i) {
System.out.println(m);
}

i see the methods but can i say only processeddata = m[1].(Vector data)
to get my method and my return value??

Thnx for helping

Andreas
 
J

Jason Teagle

so import will not work... how can i call the correct method??

Are you saying you don't even know the /name/ of the method? You just need
to find the ONE method that takes a Vector as a paremeter? That's one
/extremely/ loose class definition - I'd recommend you at least get the user
to supply the name of the method too, then you can do this:

Object[] paramTypes = new Object[1];
paramTypes[0] = Vector.class ;

Method myMethod = r.getClass().getMethod(suppliedMethodName, paramTypes);

Object[] actualParams = new Object[1];
actualParams[0] = vector_data_to_process ;
myMethod.invoke(r, actualParams);


If you really need to dynamically find which method takes a vector, and
please tell me you can guarantee there is only one such method, then you can
do this:

Method[] publicMethods = r.getClass().getMethods();
for (int methodIndex = 0 ; methodIndex = publicMethods.length ;
methodIndex++)
{
Class[] paramTypes = publicMethods[methodIndex].getParameterTypes();
if (paramTypes.length == 1) // Exactly one parameter.
{
if (Vector.class.isAssignableFrom(paramTypes[0] ) ) // It's a
Vector or subclass of Vector.
{
Object[] actualParams = new Object[1];
actualParams[0] = vector_data_to_process ;
publicMethods[methodIndex].invoke(r, actualParams);

break ; // Unless there may be more methods and they
// all want to process this same data!
}
}
}
 
A

Andreas Solsbach

thnx to all my problem is solved...
i use now a interface and one methode the user have to define
everytime... ok the user have to implement my interface but this is ok

Bye Andreas



Jason said:
so import will not work... how can i call the correct method??


Are you saying you don't even know the /name/ of the method? You just need
to find the ONE method that takes a Vector as a paremeter? That's one
/extremely/ loose class definition - I'd recommend you at least get the user
to supply the name of the method too, then you can do this:

Object[] paramTypes = new Object[1];
paramTypes[0] = Vector.class ;

Method myMethod = r.getClass().getMethod(suppliedMethodName, paramTypes);

Object[] actualParams = new Object[1];
actualParams[0] = vector_data_to_process ;
myMethod.invoke(r, actualParams);


If you really need to dynamically find which method takes a vector, and
please tell me you can guarantee there is only one such method, then you can
do this:

Method[] publicMethods = r.getClass().getMethods();
for (int methodIndex = 0 ; methodIndex = publicMethods.length ;
methodIndex++)
{
Class[] paramTypes = publicMethods[methodIndex].getParameterTypes();
if (paramTypes.length == 1) // Exactly one parameter.
{
if (Vector.class.isAssignableFrom(paramTypes[0] ) ) // It's a
Vector or subclass of Vector.
{
Object[] actualParams = new Object[1];
actualParams[0] = vector_data_to_process ;
publicMethods[methodIndex].invoke(r, actualParams);

break ; // Unless there may be more methods and they
// all want to process this same data!
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top