Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheriting methods from Java in PythonJavaClass #485

Closed
cmacdonald opened this issue Feb 9, 2020 · 5 comments
Closed

Inheriting methods from Java in PythonJavaClass #485

cmacdonald opened this issue Feb 9, 2020 · 5 comments

Comments

@cmacdonald
Copy link
Contributor

cmacdonald commented Feb 9, 2020

How do I inherit fields and methods from a Java parent class into a PythonJavaClass?

Consider the following Java base class:

package org.jnius;
public abstract class AbstractParent {
    int value=0;
    protected AbstractParent(){}
    public void doOne() { value = 1; }
    public abstract void doTwo();
}
class TestAbstract(with_metaclass(MetaJavaClass, PythonJavaClass)):
            __javaclass__ = 'org/jnius/AbstractParent'
            __javainterfaces__ = []
            
            @java_method('()Z')
            def doTwo(self):
                print("hello")

I can call doTwo(), but I cannot access doOne() from Python, nor value, i.e. neither of the following two statements work:

value = JavaField('()I;', static=False)
doOne = JavaMethod('()Z')

Moreover, we can't mix Python classes that are children of PythonJavaClass and JavaClass.

It seems that PythonJavaClass (c.f. resolve_fields() in jnius_export_class.pxi) only examines self.__class__.__dict__ to find methods to add to the proxy class, while I could contend that, similar to autoclass(), it should walk the interfaces and superclasses.

@tshirtman
Copy link
Member

I'm not sure i follow but

  • we don't actually have a way to subclass/inherit from python at all, maybe it's possible in the JVM, but it doesn't seem to be in the android version of java, which is where we'd want it the most, i did spent quite some time looking into this without success, that was years ago, but i don't think things changed, i'd love to be proven wrong though.
  • i don't think you should be able to instanciate an abstract class, i'm not sure what happens there, but it's probably conceptually wrong.

@cmacdonald
Copy link
Contributor Author

cmacdonald commented Apr 4, 2020

we don't actually have a way to subclass/inherit from python

Yes, I missed this. I think all needed is a documentation clarification.

@tshirtman
Copy link
Member

i think that was solved by your doc update PR, closing, let me know if you feel more is needed.

@xloem
Copy link

xloem commented Dec 19, 2020

Hey, could sombody link to the documentation PR here, so googlers trying to do this can find more information?

I talked with tshirtman briefly in discord, and he linked me to https://github.com/tshirtman/longface/blob/master/parse.py which apparently was the last effort to solve this. He's not aware of a way to do this with Android's JNI.

It looks to me like it might be possible by making java classes for all the abstract classes that export the abstract methods as native, and then make python classes to inherit from that by default call the superclass implementations of the abstract methods. Might send it straight into the python-for-android repository's in-tree 'android' recipe if anybody ever succeeded at that.

@xloem
Copy link

xloem commented Aug 23, 2022

@tshirtman android now provides for loading dex classes at runtime, which can be used to subclass java classes at runtime. beeware is pursuing this. ref: beeware/rubicon-java#25 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants