Skip to content

Commit ba4e0d3

Browse files
committed
Transpiler fix for Foo a = null; b = a.staticCall() not recognized as
static
1 parent 9b6848e commit ba4e0d3

File tree

3 files changed

+164
-127
lines changed

3 files changed

+164
-127
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,11 @@ private void addMethodInvocation(SimpleName javaQualifier, List<?> arguments, IM
12041204
}
12051205
} else {
12061206
appendFinalMethodQualifier(expression, declaringClass, bname,
1207-
(isStatic && !isPrivate ? FINAL_ESCAPECACHE : FINAL_CACHE));
1207+
(isStatic && !isPrivate ? FINAL_ESCAPECACHE : FINAL_CACHE)
1208+
| (isStatic ? FINAL_STATIC : 0));
12081209
buffer.append(".");
12091210
}
1211+
12101212
// keep a pointer, because we may rewrite this
12111213
int ptLog = (doLog ? buffer.length() : 0);
12121214

@@ -1247,10 +1249,11 @@ private void addMethodInvocation(SimpleName javaQualifier, List<?> arguments, IM
12471249

12481250
String j2sName = getFinalDotQualifiedNameForMethod(javaQualifier, mBinding,
12491251
(expression == null ? METHOD_NULLEXPRESSION : 0) | METHOD_ISQUALIFIED
1250-
| (lambdaArity >= 0 ? LAMBDA_METHOD : 0));
1252+
| (lambdaArity >= 0 ? LAMBDA_METHOD : 0) | (isStatic ? FINAL_STATIC : 0));
12511253

12521254
String finalMethodNameWith$Params = getFinalMethodNameWith$Params(j2sName, declaringClassJavaClassName,
12531255
mBinding, null, true, METHOD_NOTSPECIAL);
1256+
12541257
if (lambdaArity >= 0) {
12551258
// The problem here is that we cannot apply a method from an interface
12561259
// because those methods are not present in JavaScript.
@@ -1323,8 +1326,12 @@ private void appendFinalMethodQualifier(Expression qualifier, ITypeBinding decla
13231326
}
13241327
} else if (qualifier instanceof Name && !isVariableBinding(qualifier)) {
13251328
buffer.append(getFinalJ2SClassNameQualifier((Name) qualifier, declaringClass, null, flags));
1329+
} else if ((flags & FINAL_STATIC) != 0) {
1330+
// ensure even if field.method(), as long as method is static, we use Class.method()
1331+
// otherwise a null value for field will throw an exception.
1332+
buffer.append(getFinalJ2SClassNameQualifier(null, declaringClass, null, flags));
13261333
} else {
1327-
// xxxx.field.foo()
1334+
// xxxx.field.foo() -- but only if foo is not static
13281335
// (x ? y : z).foo()
13291336
// xxx.this.foo()
13301337
qualifier.accept(this);
@@ -1593,7 +1600,7 @@ public boolean visit(WhileStatement node) {
15931600
private static final int FIELD_DECL_STATIC_NONDEFAULT = 1;
15941601
private static final int FIELD_DECL_STATIC_DEFAULTS = 2;
15951602
private static final int FIELD_DECL_NONSTATIC_ALL = 3;
1596-
1603+
15971604
private void addAnonymousFunctionWrapper(boolean isOpen) {
15981605
buffer.append(
15991606
isOpen ? (buffer.lastIndexOf(")") >= buffer.length() - 3 ? ";" : "") + "\r\n(function(){" : "})()\r\n");
@@ -4543,6 +4550,9 @@ private String getFinalNameForNode(SimpleName node, char lastBufferChar, IVariab
45434550

45444551
private static final int FINAL_LAMBDA = 32;
45454552

4553+
private static final int FINAL_STATIC = 64;
4554+
4555+
45464556
/**
45474557
* Provide access to C$.$clinit$ when a static method is called or a static
45484558
* field is accessed.
@@ -4565,9 +4575,9 @@ String getFinalJ2SClassNameQualifier(Name methodQualifier, ITypeBinding declarin
45654575

45664576
if (declaringJavaClassName == null)
45674577
declaringJavaClassName = getJavaClassNameQualified(declaringJavaClass);
4568-
boolean doEscape = ((flags & FINAL_ESCAPE) == FINAL_ESCAPE);
4578+
boolean isStatic = ((flags & FINAL_STATIC) == FINAL_STATIC);
4579+
boolean doEscape = isStatic || ((flags & FINAL_ESCAPE) == FINAL_ESCAPE);
45694580
boolean doCache = ((flags & FINAL_CACHE) == FINAL_CACHE);
4570-
45714581
String name = removeBracketsAndFixNullPackageName(declaringJavaClassName);
45724582
doEscape &= !NameMapper.isClassKnown(name);
45734583
if (!doEscape) {
@@ -4585,7 +4595,6 @@ String getFinalJ2SClassNameQualifier(Name methodQualifier, ITypeBinding declarin
45854595
// lambda classes will always be defined at this point. No need to cache them
45864596
if (name.indexOf("$lambda") >= 0)
45874597
return getFinalJ2SClassName(name, FINAL_P);
4588-
45894598
return getFinalClazzLoadI$Reference(declaringJavaClass, name, doCache);
45904599
}
45914600

sources/net.sf.j2s.java.core/src/test/Test_Class.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ String hello() {
270270

271271
}
272272

273+
public static void testStatic() {
274+
// TODO Auto-generated method stub
275+
276+
}
277+
273278
}
274279

275280
class Test_class1 {

sources/net.sf.j2s.java.core/src/test/Test_Static.java

Lines changed: 143 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,135 +4,158 @@
44

55
class Test_Static extends Test_ implements Test_int3, Test_int2, Test_int1 {
66

7-
/**
8-
* Note that a static block in a main class is executed prior to running main().
9-
* So you can set the arguments here as thisApplet.__Info.args, for example.
10-
* Setting j2sHeadless = true prevents all the AWT/Swing classes from being loaded
11-
* but also means you cannot use them later. It really means headless just like in Java
12-
*
13-
* @j2sNative
14-
*
15-
* j2sHeadless = true;
16-
*
17-
* thisApplet.__Info.args = ["1","2","3"];
18-
*
19-
*/
20-
static {
21-
// JS only
22-
}
23-
24-
private static byte[] bytearray = {1,2,127};
7+
// /**
8+
// * Note that a static block in a main class is executed prior to running main().
9+
// * So you can set the arguments here as thisApplet.__Info.args, for example.
10+
// * Setting j2sHeadless = true prevents all the AWT/Swing classes from being loaded
11+
// * but also means you cannot use them later. It really means headless just like in Java
12+
// *
13+
// * @j2sNative
14+
// *
15+
// * j2sHeadless = true;
16+
// *
17+
// * thisApplet.__Info.args = ["1","2","3"];
18+
// *
19+
// */
20+
// static {
21+
// // JS only
22+
// }
2523

26-
private final static Integer iI = 11;
27-
private final static Integer iI1 = new Integer(13);
28-
private final static Float fF2 = 12f;
29-
private final static Float fF1 = new Float(12);
30-
31-
private final static String st = "testing";
32-
static {
33-
assert (iI == 11);
34-
}
35-
private static String st1 = st;
36-
private static boolean b = false;
37-
private static int y;
38-
private static char c = 'c';
39-
private static int innerI = Test_Class_Inner.i;
40-
static {
41-
System.out.println(Test_int1.int1);
42-
System.out.println(Test_int2.int2);
43-
System.out.println(Test_int3.int3);
44-
45-
System.out.println(innerI);
46-
assert(innerI == 5);
47-
int x = 3;
48-
y = Test_Boolean.i_;
49-
Test_Array.y = 3;
50-
y = Test_Array.y;
51-
y = x;
52-
Test_Boolean.main(null);
53-
t = "setFromStaticInitializer";
54-
s = "setFromStaticInitializer";
55-
}
56-
57-
public static String s, t = null;
58-
59-
private void test1() {};
60-
private void test() {
61-
test1();
62-
};
63-
64-
private static byte[] bytes = new byte[3];
65-
public static byte[] getByteArray() {
66-
return bytes;
24+
private static String testStatic() {
25+
return "no kidding!";
6726
}
27+
static Test_Static test = null;
28+
static Test_Class testc = null;
29+
30+
// private static byte[] bytearray = {1,2,127};
31+
//
32+
// private final static Integer iI = 11;
33+
// private final static Integer iI1 = new Integer(13);
34+
// private final static Float fF2 = 12f;
35+
// private final static Float fF1 = new Float(12);
36+
//
37+
// private final static String st = "testing";
38+
// static {
39+
// assert (iI == 11);
40+
// }
41+
// private static String st1 = st;
42+
// private static boolean b = false;
43+
// private static int y;
44+
// private static char c = 'c';
45+
// private static int innerI = Test_Class_Inner.i;
46+
// static {
47+
// System.out.println(Test_int1.int1);
48+
// System.out.println(Test_int2.int2);
49+
// System.out.println(Test_int3.int3);
50+
//
51+
// System.out.println(innerI);
52+
// assert(innerI == 5);
53+
// int x = 3;
54+
// y = Test_Boolean.i_;
55+
// Test_Array.y = 3;
56+
// y = Test_Array.y;
57+
// y = x;
58+
// Test_Boolean.main(null);
59+
// t = "setFromStaticInitializer";
60+
// s = "setFromStaticInitializer";
61+
// }
62+
//
63+
// public static String s, t = null;
64+
//
65+
// private void test1() {};
66+
// private void test() {
67+
// test1();
68+
// };
69+
//
70+
// private static byte[] bytes = new byte[3];
71+
// public static byte[] getByteArray() {
72+
// return bytes;
73+
// }
6874

6975
@SuppressWarnings("static-access")
7076
public static void main(String[] args) {
7177

72-
assert(t == null);
73-
assert s == "setFromStaticInitializer";
74-
75-
76-
/**
77-
* @j2sNative
78-
*
79-
* System.out.println(args);
80-
*
81-
* try{
82-
* afldsjadf.adfadf;
83-
*
84-
* } catch(e) {// is being called by Test_.js
85-
* if (e.stack.indexOf("Test_.js")) args[1] = 2;
86-
* }
87-
*
88-
*/
89-
{
90-
args = new String[] {"1","2","3"};
91-
}
92-
assert(args[1] == "2");
93-
94-
assert (y == 3);
95-
b = false;
96-
new Test_Static().test();
97-
new Test_Static().b ^= true;
98-
System.out.println(bytearray[2]);
99-
100-
new Test_Static().bytearray[--y]++;
101-
102-
103-
System.out.println(bytearray[2]);
104-
assert(bytearray[2] == -128);
78+
new Test_Static().callStatic();
79+
// each of these pairs will be the same:
80+
test.testStatic();
81+
Test_Static.testStatic();
82+
// and
83+
testc.testStatic();
84+
Test_Class.testStatic();
10585

106-
int p = 0;
107-
getByteArray()[p++] += (byte) ++p;
108-
System.out.println((new Test_Static()).getByteArray()[2]++);
109-
System.out.println((new Test_Static()).getByteArray()[2]);
11086

111-
byte bb = 127;
112-
bb++;
113-
System.out.println("bb = " + bb);
114-
assert(bb == -128);
115-
y++;
116-
new Test_Static().y++;
117-
new Test_Static().s += "test1" + c++ + 3 + 5.5f + c + 5.5 + y;
118-
assert (s.equals("setFromStaticInitializertest1c35.5d5.54") && b == true);
119-
120-
int i = 3 + y + (new Test_Static().y++);
121-
122-
assert (i == 11 && y == 5);
123-
i += 3 + y + ++(new Test_Static().y);
124-
assert (y == 6 && i == 25);
125-
126-
Test_Static st = new Test_Static();
127-
st.y++;
128-
129-
new Test_Static().y--;
130-
y--;
87+
String s = test.testStatic();
88+
System.out.println(s);
89+
System.out.println(test.testStatic());
90+
91+
// assert(t == null);
92+
// assert s == "setFromStaticInitializer";
93+
//
94+
//
95+
// /**
96+
// * @j2sNative
97+
// *
98+
// * System.out.println(args);
99+
// *
100+
// * try{
101+
// * afldsjadf.adfadf;
102+
// *
103+
// * } catch(e) {// is being called by Test_.js
104+
// * if (e.stack.indexOf("Test_.js")) args[1] = 2;
105+
// * }
106+
// *
107+
// */
108+
// {
109+
// args = new String[] {"1","2","3"};
110+
// }
111+
// assert(args[1] == "2");
112+
//
113+
// assert (y == 3);
114+
// b = false;
115+
// new Test_Static().test();
116+
// new Test_Static().b ^= true;
117+
// System.out.println(bytearray[2]);
118+
//
119+
// new Test_Static().bytearray[--y]++;
120+
//
121+
//
122+
// System.out.println(bytearray[2]);
123+
// assert(bytearray[2] == -128);
124+
//
125+
// int p = 0;
126+
// getByteArray()[p++] += (byte) ++p;
127+
// System.out.println((new Test_Static()).getByteArray()[2]++);
128+
// System.out.println((new Test_Static()).getByteArray()[2]);
129+
//
130+
// byte bb = 127;
131+
// bb++;
132+
// System.out.println("bb = " + bb);
133+
// assert(bb == -128);
134+
// y++;
135+
// new Test_Static().y++;
136+
// new Test_Static().s += "test1" + c++ + 3 + 5.5f + c + 5.5 + y;
137+
// assert (s.equals("setFromStaticInitializertest1c35.5d5.54") && b == true);
138+
//
139+
// int i = 3 + y + (new Test_Static().y++);
140+
//
141+
// assert (i == 11 && y == 5);
142+
// i += 3 + y + ++(new Test_Static().y);
143+
// assert (y == 6 && i == 25);
144+
//
145+
// Test_Static st = new Test_Static();
146+
// st.y++;
147+
//
148+
// new Test_Static().y--;
149+
// y--;
150+
//
151+
// String y1 = "0";
152+
// y1 += "test" + c + 3 + 5.5f + c + 5.5;
153+
// assert (y1.equals("0testd35.5d5.5"));
154+
// System.out.println("Test_Static OK");
155+
}
131156

132-
String y1 = "0";
133-
y1 += "test" + c + 3 + 5.5f + c + 5.5;
134-
assert (y1.equals("0testd35.5d5.5"));
135-
System.out.println("Test_Static OK");
157+
private void callStatic() {
158+
this.testStatic(); // will be C$.testStatic()
136159
}
137160

138161
}

0 commit comments

Comments
 (0)