Bug: Nested classes/interfaces are not always loaded correctly

33 views
Skip to first unread message

Udo

unread,
Jan 21, 2014, 7:33:34 AM1/21/14
to java2...@googlegroups.com
Assume we have this class with a nested interface (in a file "ClassWithNestedInterface.java"):

public class ClassWithNestedInterface {

    public interface NestedInterface {

    }

}


In an other class NestedInterfaceUser (stored in file "NestedInterfaceUser.java") we are using the above (nested) interface:

public class NestedInterfaceUser {


    public static NestedInterfaceUser create() {

        return new NestedInterfaceUser();

    }


    public ClassWithNestedInterface.NestedInterface func() {

        return new ClassWithNestedInterface.NestedInterface() {

            @Override

            public String toString() {

                return "NestedInterfaceImplementation";

            }

        };

    }


    public NestedInterfaceUser() {

    }

}


When we run this code as a Java2Script application and try to create an instance of NestedInterfaceUser and call func the browser prompts the following error:

  1. GET file:///...removed_stufff.../ClassWithNestedInterface/NestedInterface.js net::ERR_FILE_NOT_FOUND j2slib.src.z.js:4455

    I.e. Java2Script tries to load the nested interface from a separate file "NestedInterface.js" inside a directory "ClassWithNestedInterface". This fails because there is no such file (or directory). Actually the code for "NestedInterface" is located in the file "ClassWithNestedInterface.js".


    We noticed the problem with the latest Java2Script upgrade (j2s-2.0.0-20131019-eclipse-4.3). After quite some effort we were able to isolate the problem in the small test code example given above. We re-ran the test on an older version of Java2Script and the test also failed with the old version. 

    Notice this does not happens with every nested class or interface. It looks like timing and/or class load order affects it. E.g. when the containing class was already loaded, e.g. because of some other dependencies, the nested class/interface is also already defined and the problem vanishes.


    Udo



    Zhou Renjian

    unread,
    Jan 21, 2014, 8:12:01 AM1/21/14
    to Java2Script
    Hi,

    For quick answer, there are are undocumented tips for inner classes. Try adding some lines of codes like these:

    ClazzLoader.jarClasspath (base + "util/AbstractList.js", [

    "java.util.AbstractList",

    "java.util.AbstractList.FullListIterator",

    "java.util.AbstractList.SimpleListIterator",

    "java.util.AbstractList.SubAbstractList",

    "java.util.AbstractList.SubAbstractListRandomAccess"

    ]);

    somewhere.

    If no ClazzLoader.jarClasspath is configured, requiring ClassWithNestedInterface.NestedInterface before requiring ClassWithNestedInterface (e.g. new ClassWithNestedInterface() which will trigger class being loaded), it will complaint class loading error.

    Hope it help.


    Regards,
    Zhou Renjian


    --
    You received this message because you are subscribed to the Google Groups "Java2Script" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to java2script...@googlegroups.com.
    To post to this group, send email to java2...@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/java2script/1e5ca255-c364-4af0-bccb-7fa443d20ad9%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    Udo

    unread,
    Jan 21, 2014, 5:09:19 PM1/21/14
    to java2...@googlegroups.com
    Thank you very much for that fast response!

    That solved the issue,

    Regards,

    Udo

    Zhou Renjian

    unread,
    Jun 23, 2016, 3:52:29 PM6/23/16
    to Java2Script
    Fixed in latest Java2Script build for Eclipse 4.5 and 4.6.
    Reply all
    Reply to author
    Forward
    0 new messages