mmrsohn / jythonroid

Automatically exported from code.google.com/p/jythonroid
0 stars 0 forks source link

import from * problem #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
1.>>>from android.app import * will not work 
2.>>>from android.app import Activity works fine

by compareing the jython running in sunjvm and dalvik, i find that in
dalvik it is the JavaImportHelper that handling the import of the package.
and it's said in the comment that:

    /**
     * Try to add the java package.
     * <p>
     * This is handy in cases where the package scan cannot run, or when
the initial classpath does not contain all .jar
     * files (such as in J2EE containers).
     * <p>
     * There is some self-healing in the sense that a correct, explicit
import of a java class will succeed even if
     * sys.modules already contains a Py.None entry for the corresponding
java package.
     * 
     * @param packageName The dotted name of the java package
     * @param fromlist A tuple with the from names to import. Can be null
or empty.
     * 
     * @return <code>true</code> if a java package was doubtlessly
identified and added, <code>false</code>
     * otherwise.
     */

and the exception is throwed by the 
                String parentPackageName = packageName;
                if (isLoadedPackage(packageName, packages)) {
                    packageAdded = addPackage(packageName, packageAdded);
                }

Original issue reported on code.google.com by juanzhew...@gmail.com on 14 Apr 2008 at 11:04

GoogleCodeExporter commented 9 years ago
i have spent the whole night dealing whith this problem, and it still 
unresolved.
the jython runs on sun jvm will not call JavaImportHelper at all, and it will 
call
this class only when you input some sentence like >>>import
somthingdonotcontaininjreorcodes
so i traced to the place that calls JavaImportHelper:imp.java
     * 
     * @param mod a previously loaded module
     * @param parentNameBuffer
     * @param name the name of the module to load
     * @return null or None
     */
    private static PyObject import_next(PyObject mod,
            StringBuffer parentNameBuffer, String name, String outerFullName,
PyObject fromlist) {
        if (parentNameBuffer.length() > 0) {
            parentNameBuffer.append('.');
        }
        parentNameBuffer.append(name);

        String fullName = parentNameBuffer.toString().intern();

        PyObject modules = Py.getSystemState().modules;
        PyObject ret = modules.__finditem__(fullName);
        if (ret != null) {
            return ret;
        }
        if (mod == null) {
            ret = find_module(fullName.intern(), name, null);
        } else {
            ret = mod.impAttr(name.intern());
        }
        if (ret == null || ret == Py.None) {
            if (JavaImportHelper.tryAddPackage(outerFullName, fromlist)) {
                ret = modules.__finditem__(fullName);
            }
            return ret;
        }
        if (modules.__finditem__(fullName) == null) {
            modules.__setitem__(fullName, ret);
        } else {
            ret = modules.__finditem__(fullName);
        }
        return ret;
    }
so i doing some bugfix to make sure that the ret will not be null or Py.None

Original comment by juanzhew...@gmail.com on 14 Apr 2008 at 2:35

GoogleCodeExporter commented 9 years ago
trace into #ret = find_module(fullName.intern(), name, null);#

        path = path == null ? sys.path : path;
        for (int i = 0; i < path.__len__(); i++) {
            PyObject p = path.__getitem__(i);
            // System.err.println("find_module (" + name + ", " + moduleName +
            // ") Path: " + path);
            PyObject importer = getPathImporter(sys.path_importer_cache,
                    sys.path_hooks, p);
            if (importer != Py.None) {
                PyObject findModule = importer.__getattr__("find_module");
                loader = findModule.__call__(new PyObject[] { new PyString(
                        moduleName) });
                if (loader != Py.None) {
                    return loadFromLoader(loader, moduleName);
                }
            }
            ret = loadFromSource(name, moduleName, p);
            if (ret != null) {
                return ret;
            }
        }

when i=1, the jython in sun jvm will make ret to be PyJavaClass.
there must be the place where the bug raise.

Original comment by juanzhew...@gmail.com on 14 Apr 2008 at 2:39

GoogleCodeExporter commented 9 years ago
i have reported the java.lang.Package.getPackages() bug to the android team, see
http://code.google.com/p/android/issues/detail?id=680&sort=-id&colspec=ID%20Type
%20Version%20Security%20Status%20Owner%20Summary

Original comment by juanzhew...@gmail.com on 16 Apr 2008 at 1:40

GoogleCodeExporter commented 9 years ago
How to make import * run in case of J2EE ?. Iam using JavaImportHelper , but
apparently that doesnt handle the import * statements. We are stuck with this. 
Any
help would be greatly appreciated.

Original comment by anveshre...@gmail.com on 21 Aug 2009 at 5:15