Dynamic method invocation

Discussion in 'Python' started by jythonuser, Jun 23, 2009.

  1. jythonuser

    jythonuser Guest

    Hi
    I am new to jythong and was wondering if/how I can do the following -

    a) register a java object with a given name with jython interpreter
    using set method
    b) call methods on the java object - but the methods may not exist on
    the object, so I would like to call from jython a generic method that
    I have defined on the object as opposed to using java reflection.
    Thus the java class is more like a proxy for invocation.

    As an example, I register an instance of java class Bar with name
    foo. I want to be able to call from jython -
    foo.method1 (args)
    I don't want to define all the methods that I can call from jython on
    class foo but have a generic method that the above jython code will
    invoke. I want to use a model where the methods that can be called on
    foo are discovered dynamically once they are invoked on java class.
    Clearly Java won't let me do this during method invocation so I am
    wondering if I can register some sort of proxy object with jython that
    will let me then delegate right call down to the java object.

    Thanks a bunch
    jythonuser, Jun 23, 2009
    #1
    1. Advertising

  2. jythonuser schrieb:
    > Hi
    > I am new to jythong and was wondering if/how I can do the following -
    >
    > a) register a java object with a given name with jython interpreter
    > using set method
    > b) call methods on the java object - but the methods may not exist on
    > the object, so I would like to call from jython a generic method that
    > I have defined on the object as opposed to using java reflection.
    > Thus the java class is more like a proxy for invocation.
    >
    > As an example, I register an instance of java class Bar with name
    > foo. I want to be able to call from jython -
    > foo.method1 (args)
    > I don't want to define all the methods that I can call from jython on
    > class foo but have a generic method that the above jython code will
    > invoke. I want to use a model where the methods that can be called on
    > foo are discovered dynamically once they are invoked on java class.
    > Clearly Java won't let me do this during method invocation so I am
    > wondering if I can register some sort of proxy object with jython that
    > will let me then delegate right call down to the java object.


    It's very unclear to me what you want, but to me, it looks as if you
    have some misconceptions about jython.

    There is no "registration" of java-objects. If you have a java-object,
    you can assign it to a name, and simply invoke any method on it
    (internally, that will mean using reflection, but that's below the
    covers for you)

    foo = SomeJavaClass()
    foo.some_method()

    As long as some_method is defined on SomeJavaClass, this will work. And
    there is also no type-information needed, so this will work in Jython,
    but not in java:


    ---- java ----

    class A {
    public void foo() {};
    }

    class B { // NOT A SUBCLASS OF A!!
    public void foo() {};
    }


    ---- jython ----

    if some_condition:
    bar = A()
    else:
    bar = B()

    bar.foo()



    Regarding the "generic method thing", I guess this will work:


    foo = SomeJavaObject()

    m = getattr(foo, "method_name_that_might_not_be_defined", None)

    if m:
    m(argument)


    Diez
    Diez B. Roggisch, Jun 23, 2009
    #2
    1. Advertising

  3. jythonuser

    jythonuser Guest

    Sorry for being a little vague. The last part of your response seems
    like what I would need. So here are more details on what I am trying
    to do.

    I have an extensible command shell in java where commmand providers
    plug in to supply new commands. I have a java class called MyShell
    which has execCommand method on it. Thus MyShell class does not have
    a concrete method on it for each command that it supports (e.g. grep,
    ls, cat etc) but a generic execCommand method that takes the name of
    the command and command params as arguments.

    clas MyShell () {
    Object execCommand (cmdName, params) {
    Object o = // find command object from command name
    return o.invoke (cmdName, params);
    }
    |

    Now I want to use this in jython. So I could do something like
    shell = MyShell()
    shell.exec ("grep", grep_args)

    What I would like to do is -
    shell.grep (grep_args)

    That way my users don't have to use some gorpy exec/invoke syntax for
    each command but think that shell has all the commands defined on it.
    So based on your example I am hoping that I can use getattr on "some"
    object that returns me "some" method that I can call.

    Does this make sense and is there a way to do this in jython?
    jythonuser, Jun 26, 2009
    #3
  4. jythonuser

    jythonuser Guest

    Actually let me add that in jython I don't need to always use
    MyShell. It may be ok to wrapper it with some Jython class which then
    delegates to MyShell. So something like

    shell = MyJythonShell()
    shell.grep (grep_args)

    So MyJythonShell is defined in Jython which calls MyShell.

    Am wondering how to go about all this.
    jythonuser, Jun 26, 2009
    #4
    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. Jonathan Shore
    Replies:
    1
    Views:
    350
  2. Generic Usenet Account

    Dynamic invocation of web services?

    Generic Usenet Account, Jun 17, 2004, in forum: XML
    Replies:
    1
    Views:
    457
    Zahid Faizal
    Jun 23, 2004
  3. Marc E
    Replies:
    2
    Views:
    493
    Marc E
    Jul 19, 2006
  4. ash
    Replies:
    8
    Views:
    420
    Roedy Green
    Mar 19, 2008
  5. Sebastian
    Replies:
    2
    Views:
    464
    Sebastian
    Feb 8, 2012
Loading...

Share This Page