Hello all. I have done a little research about native javascript
objects manipulation in java code.
First let me describe the situation.
For one side, java.util classes rely on a java.lang.Object prototype.
For example, HashMap can be used only with objects that implement
hashCode(), equals(), etc methods. If we remove these methods from
java.lang.Object prototype then the java toolkit (like java.util) will
not work.
In the other side it is often necessary to work with native javascript
objects. (Mainly when working with javascript native toolkits, but
also when we want to use a javascript native object instead of
java.util.HashMap for performance reason).
My principal problem with java2script is that, since j2slib
"contaminates" Object.prototype with those methods in
java.lang.Object, there is no way in a java2script program of
instantiating a pure "non-contaminated" javascript Object (not even
natively). (please correct me about this if I'm wrong)
Well, my approach for resolving this was to modify j2slib.z (java/lang/
Object.js and java/lang/ClassExt.js) so java.lang.Object extends not
from Object but from a subclass. I named this Object subclass,
J2sObject. I also make a little modification in the compiler so "new
Object()" java statement is translated into "new J2sObject()".
This way, Object.prototype is clean, all java Objects extends from
J2sObject (which prototype has java.lang.Object required methods). Now
a javascript statement like "var obj = {}" will create a truly empty
javascript object. A java statement like new Object() will keep
creating "dirty" objects.
What I exactly have done is:
in j2slib.z : define J2sObject class and replace each occurrence of
Object with J2sObject
in net.sf.j2s.core.astvisitors.ASTTypeVisitor: about line 224, A
little hack so "new Object()" java expression is being translated to
"new J2sObject()" javascript expression
I have uploaded nativeObject.zip in this group files (http://
groups.google.com/group/java2script/files) containing mentioned files
and also a test case that reflect the differences. The .java file has
a "//cancerbero_sgx" comment in the changed line. I'm testing this
with java2script sources for eclipse 3.3 (I didn't have the time for
trying eclipse 3.5). All my java2script programs seems to work as
spected with this change, but nevertheless I'm not sure about all the
consequences... I would like to hear what's your opinion about this
proposed solution that will allow:
* instantiate and manipulate non-contaminated native javascript
objects in j2s programs.
* since Object.prototype is not touched, interoperability bugs between
java2script and other javascript toolkits will be reduced (IMHO this
is one of java2script weakest aspects)
Well, I think that was all. Let me think what's your opinion.
Greetings from Uruguay!