Problem with methods & classloader

Discussion in 'Java' started by Andrew Thompson, Oct 8, 2004.

  1. On Fri, 08 Oct 2004 13:12:47 +0200, Andreas Solsbach wrote:

    > 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
    Andrew Thompson, Oct 8, 2004
    #1
    1. Advertising

  2. 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
    Andreas Solsbach, Oct 8, 2004
    #2
    1. Advertising

  3. On Fri, 08 Oct 2004 13:58:22 +0200, Andreas Solsbach wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 8, 2004
    #3

  4. > 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
    Andreas Solsbach, Oct 8, 2004
    #4
  5. the line with behaviourdata(4) is marke with //
    :)
    Andreas Solsbach, Oct 8, 2004
    #5
  6. Andrew Thompson

    Jason Teagle Guest

    >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!
    }
    }
    }


    --
    --
    Jason Teagle
    Jason Teagle, Oct 8, 2004
    #6
  7. 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 Teagle wrote:
    >>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!
    > }
    > }
    > }
    >
    >
    Andreas Solsbach, Oct 8, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emmanuel PIC

    ClassLoader problem

    Emmanuel PIC, Jul 30, 2003, in forum: Java
    Replies:
    3
    Views:
    403
    Emmanuel PIC
    Aug 4, 2003
  2. Mark F

    Tomcat classloader problem

    Mark F, Mar 5, 2004, in forum: Java
    Replies:
    0
    Views:
    468
    Mark F
    Mar 5, 2004
  3. Benji
    Replies:
    1
    Views:
    451
    Chris Uppal
    Oct 6, 2005
  4. Edward
    Replies:
    1
    Views:
    942
    Edward
    Feb 14, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    299
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page