Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Problem importing extended java classes. #856

Open
abrunyate opened this issue Jun 30, 2018 · 3 comments
Open

Problem importing extended java classes. #856

abrunyate opened this issue Jun 30, 2018 · 3 comments

Comments

@abrunyate
Copy link

Consider the files foo.java, t1.py,t2.py.

//foo.java
package foo;
public class foo {
  public foo(String S) { 
    }
  }

The file t2 extends the defined java class:

#t2.py
from foo import foo

class bar(extends=foo.foo):
    @super({S: java.lang.String})
    def __init__(self, S):
      pass

and the resulting class is used from t1:

#t1.py
from t2 import bar
f=bar("bas")

The initialization of bar fails with

Exception in thread "main" RuntimeError: foo.foo: method <init>()V not found
	at org.python.ImportLib.importPythonModule(ImportLib.java:260)
	at org.python.ImportLib.__import__(ImportLib.java:176)
	at python.t1.module$import(t1.py:2)
	at python.t1.main(t1.py)
@abrunyate
Copy link
Author

It looks like this is caused by the compiler not realizing that foo.class is not a python module. If so it should be easy to fix, but is a different bug than I wanted to illustrate, which involved subclassing an Android view. I'll look at my application again to see if there're indeed two issues at play here.

@freakboy3742
Copy link
Member

Does the same error occur if you use a different name for the module, class, and method? The test suite already contains a bunch of examples of importing from a Java class; I can only assume that there's a namespace collision happening somewhere.

@abrunyate
Copy link
Author

The name of the class within the java package doesn't matter.

It seems that the subclassing isn't the issue, and the subclass definition can be removed from the example. As far as I can tell what happens is that if the main python module t1 has from t2 import bar
and the python module t2 attempts to import the java package the class files are imported as python modules. The importer tries to call the nonexistent unparameterized constructor of the corresponding java class, possibly here in ImportLib.java:

             // Create an instance of the class; if it isn't a module, we haven't been able to import.
             try {
                 java.lang.reflect.Constructor constructor = java_class.getConstructor();
                 python_module = (org.python.types.Module) constructor.newInstance();
             } catch (ClassCastException cce) {
                throw new java.lang.ClassNotFoundException("Found " + import_name + " class, but class is not a module");
             }

The exception is not caught, and the importer doesn't fall back to native import because it's expecting the process to fail with ClassNotFound.

I don't know the code, really, so this is only a guess. Things aren't set up for development right now; I'll tinker with this in a few days and report back.

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

No branches or pull requests

2 participants