Missing field initialization in super classes

25 views
Skip to first unread message

Udo

unread,
May 15, 2012, 4:46:51 PM5/15/12
to Java2Script
It looks like in some situations the member fields of super classes
are not initialized.

Here an example:


The class Foo initializes a field with an object:

public class Foo {
public Object field = new Object();

public Foo() {
}
}

As expected running the following code will output "foo.field
initialized.":

Foo foo = new Foo();

if (foo.field != null) {
System.out.println("foo.field initialized.");
} else {
throw new RuntimeException("foo.field not initialized");
}



The class Bar uses Foo as its base class:

public class Bar extends Foo {

public Bar(Object... args) {
}
}


Surprisingly the following code throws java.lang.RuntimeException:
bar.field NOT initialized when running in Java2Script emulation:

Bar bar = new Bar();

if (bar.field != null) {
System.out.println("bar.field initialized.");
} else {
throw new RuntimeException("bar.field NOT initialized");
}

(The same code executed as a "real" Java application outputs the
expected "bar.field initialized.").

It looks like the member field in base class Foo is not initialized
when a derived object is created. Maybe this is related to the
parameter of Bar's constructor (Object... args).

You can see the bug "live" here: http://www.abego.org/j2s/J2SMissingSuperFieldInitializationBug/index.html

You get the sources to reproduce the bug here:
https://github.com/abego/Java2Script-contrib/tree/master/J2SMissingSuperFieldInitializationBug


Udo

Zhou Renjian

unread,
May 16, 2012, 2:15:47 AM5/16/12
to java2...@googlegroups.com
It seems it is a big bug for arguments Object... args. And it may not be fixed sooner. Try to avoid it or use new Object[0] as arguments.

Regards,
Zhou Renjian



On Wed, May 16, 2012 at 4:46 AM, Udo <udo.bo...@googlemail.com> wrote:
Object... args

Udo

unread,
May 16, 2012, 4:13:00 AM5/16/12
to java2...@googlegroups.com
And it may not be fixed sooner.
:(

Is there really no chance to fix that?

I am currently working on a project with a (quite big) existing Java application. Parts of it should be converted to JavaScript. The Java application is productive since nearly 10 years now and will co-exist with the JavaScript application. Both should be based on the same sources. As the Java application is productive and working well changes (like introducing workaround code) should be avoided. So I would very much appreciate if this "big bug" would be fixed.

Udo


On Wednesday, May 16, 2012 8:15:47 AM UTC+2, Zhou Renjian wrote:
It seems it is a big bug for arguments Object... args. And it may not be fixed sooner. Try to avoid it or use new Object[0] as arguments.

Regards,
Zhou Renjian



On Wed, May 16, 2012 at 4:46 AM, Udo wrote:
Object... args

Zhou Renjian

unread,
May 17, 2012, 1:17:00 PM5/17/12
to java2...@googlegroups.com
I just made a quick fix (without too much tests).

http://dev.zhourenjian.com/j2s/j2s.core.3.6.zip
http://dev.zhourenjian.com/j2s/j2s.core.3.5.zip

Download and unzip *.jar, replace the *.jar file in plugins folder and restart Eclipse.

Hope it works and fixes your problems.

Regards,
Zhou Renjian



--
You received this message because you are subscribed to the Google Groups "Java2Script" group.
To view this discussion on the web visit https://groups.google.com/d/msg/java2script/-/ZFA2gWa-lf4J.

To post to this group, send email to java2...@googlegroups.com.
To unsubscribe from this group, send email to java2script...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/java2script?hl=en.

Udo

unread,
May 17, 2012, 6:36:26 PM5/17/12
to java2...@googlegroups.com
I just made a quick fix (without too much tests).
Wow, that was fast...

I can confirm that the new version fixes the issue as I reported it, and the test case now runs fine.

However: the original code that failed and that made me create the test case, still fails. So it looks the bug is not fixed completely.

Some background: I created the test case as "the minimal test case that shows the failure" by removing all stuff from the original code that did not make the failure go away. Some stuff that was removed this way is now causing the problem in the original code. 

I am not sure how to proceed. I don't want to hurry you into any quick fixes that may not really fix the cause of the problem. If this needs more time it needs more time and if you don't have enough time to fix it we have to wait. 

One the other hand I could try to provide you with a new (minimal) test case showing how the problem occurs even when using the latest version. Creating such a minimal test cast is always quite time consuming so I would only do it when you think you would give it a second try.

What do you suggest?

But anyway: many thanks for your excellent and fast support.


Udo



To unsubscribe from this group, send email to java2script+unsubscribe@googlegroups.com.

Zhou Renjian

unread,
May 17, 2012, 8:03:55 PM5/17/12
to java2...@googlegroups.com
For quick fix without too much tests, I mean that this build is fixed in a correct way but is not tested a lots.

For compiler bug-fix, we should create a lot of testcases (you already provide one, other test cases will help). As there are lots of code combinations, we need to design those testcases to ensure that almost all branches is run into. Besides the new test cases, we still need to run all other existed testcases or existed projects to make sure that bug-fix does not introduce new bugs. As I did not design a lot of testcases and run over existed projects, so I call it a quick fix.

If you provide more testcases that Java2Script compiler fails, it would help. Thanks in advance.

Regards,
Zhou Renjian



To view this discussion on the web visit https://groups.google.com/d/msg/java2script/-/JYw3-q3jKUYJ.

To post to this group, send email to java2...@googlegroups.com.
To unsubscribe from this group, send email to java2script...@googlegroups.com.

Udo

unread,
May 19, 2012, 10:21:09 AM5/19/12
to java2...@googlegroups.com
Hi Renjian,

here another test case on field initialization that make Java2Script fail, even in the "fixed" version you provided on May 17th. It is very similar to the previous one, but adds another inheritance level and a new field.

public class Foo {

public Object field = new Object();

public Foo() {

}

}


public class Bar extends Foo {

public Bar(Object... args) {

}

}


public class Moo_WithOwnField extends Bar {

public Object mooField = new Object();

public static Moo_WithOwnField createMoo() {

return new Moo_WithOwnField();

}

}



Creating a Moo_WithOwnField instance will not initialize the "field" in Foo. 

I created a set of tests using the JavaScript unit test framework QUnit[1] that also includes the above test case. You can get the sources from https://github.com/abego/Java2Script-contrib/tree/master/J2STest

The running tests are also available online at: http://www.abego.org/j2s/J2STest/index.html . You will see one failing test.

Udo


Zhou Renjian

unread,
May 20, 2012, 11:31:48 AM5/20/12
to java2...@googlegroups.com
Hi Udo,

Thanks for your testcase. I made another fix.

Please download plugin files again:

Regards,
Zhou Renjian



To view this discussion on the web visit https://groups.google.com/d/msg/java2script/-/yhacM4ZS9VYJ.

To post to this group, send email to java2...@googlegroups.com.
To unsubscribe from this group, send email to java2script...@googlegroups.com.

Udo

unread,
May 22, 2012, 5:22:30 PM5/22/12
to java2...@googlegroups.com
Hi Renjian,

I tried your new version and must report: it doesn't fix the bug. The test case "Field Initializationindirectly inherited field and own field initialization (incl. vararg constructor)" and the actual program still break. I was working with the 3.6 version (http://dev.zhourenjian.com/j2s/j2s.core.3.6.zip).

Are you sure you uploaded the latest revision? When I compare this download with the previous one I cannot see any difference.

Udo

Zhou Renjian

unread,
May 22, 2012, 8:54:07 PM5/22/12
to java2...@googlegroups.com
Hi Udo,


For 3.6, net.sf.j2s.core_2.0.0.jar should be 250043, Early version given file's size is 249,585.

I check http://dev.zhourenjian.com/j2s/j2s.core.3.6.zip myself, the file size is OK, different from the early version.

If it is not fixing the bug, I may miss something. I will try to fix it again some hours later.

Regards,
Zhou Renjian



To view this discussion on the web visit https://groups.google.com/d/msg/java2script/-/XlCiCmfhgW0J.

To post to this group, send email to java2...@googlegroups.com.
To unsubscribe from this group, send email to java2script...@googlegroups.com.

Udo

unread,
May 23, 2012, 2:40:25 AM5/23/12
to java2...@googlegroups.com
I verified the sizes of the jar files and their match the ones you gave. So the jar files have different sizes. But when comparing the content of "net.sf.j2s.core_2.0.0.jar Folder" also included in the zip I find no differences.

If it is not fixing the bug, I may miss something.

It should be easy for you to verify if your fix works by using my test suite. All tests should pass, currently the last one fails. Here the link to the sources:  https://github.com/abego/Java2Script-contrib/tree/master/J2STest 

Udo

Zhou Renjian

unread,
May 23, 2012, 3:01:03 PM5/23/12
to java2...@googlegroups.com
It seems that I added another constructor to your testcase class while fixing bug and the bug was hidden so I thought it is fixed.

I made another bug fix for 3.6 only: http://dev.zhourenjian.com/j2s/core.3.6.zip

Regards,
Zhou Renjian



To view this discussion on the web visit https://groups.google.com/d/msg/java2script/-/Qd_zJjCaYa4J.

To post to this group, send email to java2...@googlegroups.com.
To unsubscribe from this group, send email to java2script...@googlegroups.com.

Udo

unread,
May 23, 2012, 7:50:05 PM5/23/12
to java2...@googlegroups.com
Hi Renjian,

I can confirm this last revision fixes the bug. Both the tests and the actual application now run as expected.

Thank you very much for your effort. 

Udo
Reply all
Reply to author
Forward
0 new messages