Using j2s-2.0.0-v20160624-eclipse-4.5
This example is in an anonymous function, but the problem is the same without that complication:
Test_6 x = new Test_6() {
String zz = "zz";
String[] xx0 = { "x1", "x2" };
{
System.out.println(" xx0[0]=" + xx0[0]);
}
};
should report:
xx0[0]=x1
but it instead throws an error that xx0 is undefined.
The transpiled code looks like this:
c$.$Test_4$1$ = function () {
if (Clazz.isClassDefined('test.Test_4$1')) return test.Test_4$1;
Clazz.pu$h();
c$ = Clazz.decorateAsClass(function () {
Clazz.prepareCallback(this, arguments);
this.zz = 'zz';
this.xx0 = null;
{
System.out.println(Clazz.castObjectAs('xx0[0]=' + this.xx0[0], 'String'));
}
Clazz.instantialize(this, arguments);
}, test, 'Test_4$1', test.Test_6);
Clazz.prepareFields(c$, function () {
this.xx0 = Clazz.newArray( - 1, [
'x1',
'x2'
]);
{
System.out.println('xx0[0]=' + this.xx0[0], 'String'));
}
});
var rc = c$;
c$ = Clazz.p0p();
return rc;
};
A simple solution would be to remove all inline anonymous code blocks prior to running Clazz.initialize, and to have those only in the prepareFields call:
c$.$Test_4$1$ = function () {
if (Clazz.isClassDefined('test.Test_4$1')) return test.Test_4$1;
Clazz.pu$h();
c$ = Clazz.decorateAsClass(function () {
Clazz.prepareCallback(this, arguments);
this.zz = 'zz';
this.xx0 = null;
Clazz.instantialize(this, arguments);
}, test, 'Test_4$1', test.Test_6);
Clazz.prepareFields(c$, function () {
this.xx0 = Clazz.newArray( - 1, [
'x1',
'x2'
]);
{
System.out.println(Clazz.castObjectAs('xx0[0]=' + this.xx0[0], 'String'));
}
});
var rc = c$;
c$ = Clazz.p0p();
return rc;
};
But, alternatively, I do not understand why there is any preliminary code in the first place. Why not execute the same as Java, just processing all at the same time? Why would this not work, as long as it is executed at the correct time? I think perhaps it is because early on in J2S development the initializing sequence was out of order.
c$.$Test_4$1$ = function () {
if (Clazz.isClassDefined('test.Test_4$1')) return test.Test_4$1;
Clazz.pu$h();
c$ = Clazz.decorateAsClass(function () {
Clazz.prepareCallback(this, arguments);
Clazz.instantialize(this, arguments);
}, test, 'Test_4$1', test.Test_6);
Clazz.prepareFields(c$, function () {
this.zz = 'zz';
this.xx0 = Clazz.newArray( - 1, [
'x1',
'x2'
]);
{
System.out.println(Clazz.castObjectAs('xx0[0]=' + this.xx0[0], 'String'));
}
});
var rc = c$;
c$ = Clazz.p0p();
return rc;
};
Bob Hanson
Using j2s-2.0.0-v20160624-eclipse-4.5
This example is in an anonymous function, but the problem is the same without that complication:
should report:
xx0[0]=x1
but it instead throws an error that xx0 is undefined.
The transpiled code looks like this:
A simple solution would be to remove all inline anonymous code blocks prior to running Clazz.initialize, and to have those only in the prepareFields call:
But, alternatively, I do not understand why there is any preliminary code in the first place. Why not execute the same as Java, just processing all at the same time? Why would this not work, as long as it is executed at the correct time? I think perhaps it is because early on in J2S development the initializing sequence was out of order.
Bob Hanson