One pattern of ensuring that a Jython base class implemented in Java can call an @ExposedMethod that has potentially been overridden in Python is to have a pair of methods on the base class:

Then the derived class overrides the super class method in the standard fashion, i.e.

This is done the use of the rest directive in the derived template - e.g. a typical implementation is:

   1   base_class: PyDefaultDict
   2   want_dict: true
   3   ctr:
   4   incl: dict
   5   rest:
   6       public PyObject __missing__(PyObject key) {
   7           PyType self_type=getType();
   8           PyObject impl=self_type.lookup("__missing__");
   9           if (impl!=null) {
  10               return impl.__get__(this,self_type).__call__(key);
  11           }
  12           return super.__missing__(key);
  13       }

So the base class now defines an @ExposedMethod:

   1      @ExposedMethod
   2      final PyObject defaultdict___missing__(PyObject key) {
   3          if (defaultFactory == Py.None) {
   4              throw Py.KeyError(key);
   5          }
   6          return defaultFactory.__call__();
   7      }

and the public method calls the @ExposedMethod directly like:

   1      public PyObject __missing__(PyObject key) {
   2          return defaultdict___missing__(key);
   3      }

So if any method in the base class calls the public method it will result in standard Java dynamic method dispatch to a Py*Derived class if the instance in question has derived from the base class and that will call the overridden python method if it exists or default back to the base classes implementation via super.

MethodDispatch (last edited 2014-08-13 03:47:03 by IndraTalip)