Skip to content

Commit 2217b72

Browse files
author
zhourenjian
committed
Fixed a bug that normal blocks in body declaration is compiled as static blocks. For example:
public class TestClass { public void f() {} { System.out.println(this); //bug. "this" is Window. f(); // Error: this.f() isn't function. } public static void main(String[] a) {} } Thanks buzzer
1 parent 5173cb2 commit 2217b72

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ public boolean visit(AnonymousClassDeclaration node) {
201201
if (needPreparation) {
202202
break;
203203
}
204+
} else if (element instanceof Initializer) {
205+
Initializer init = (Initializer) element;
206+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
207+
needPreparation = true;
208+
break;
209+
}
204210
}
205211
}
206212

@@ -302,6 +308,11 @@ public boolean visit(AnonymousClassDeclaration node) {
302308
continue;
303309
}
304310
element.accept(this);
311+
} else if (element instanceof Initializer) {
312+
Initializer init = (Initializer) element;
313+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
314+
element.accept(this);
315+
}
305316
}
306317
}
307318
buffer.append("});\r\n");
@@ -672,6 +683,12 @@ public void endVisit(EnumDeclaration node) {
672683
if (needPreparation) {
673684
break;
674685
}
686+
} else if (element instanceof Initializer) {
687+
Initializer init = (Initializer) element;
688+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
689+
needPreparation = true;
690+
break;
691+
}
675692
}
676693
}
677694
if (needPreparation) {
@@ -684,6 +701,11 @@ public void endVisit(EnumDeclaration node) {
684701
continue;
685702
}
686703
element.accept(this);
704+
} else if (element instanceof Initializer) {
705+
Initializer init = (Initializer) element;
706+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
707+
element.accept(this);
708+
}
687709
}
688710
}
689711
buffer.append("};\r\n");
@@ -2208,6 +2230,12 @@ public void endVisit(TypeDeclaration node) {
22082230
if (needPreparation) {
22092231
break;
22102232
}
2233+
} else if (element instanceof Initializer) {
2234+
Initializer init = (Initializer) element;
2235+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
2236+
needPreparation = true;
2237+
break;
2238+
}
22112239
}
22122240
}
22132241
if (needPreparation) {
@@ -2220,6 +2248,11 @@ public void endVisit(TypeDeclaration node) {
22202248
continue;
22212249
}
22222250
element.accept(this);
2251+
} else if (element instanceof Initializer) {
2252+
Initializer init = (Initializer) element;
2253+
if ((init.getModifiers() & Modifier.STATIC) == 0) {
2254+
element.accept(this);
2255+
}
22232256
}
22242257
}
22252258
buffer.append("});\r\n");
@@ -2256,7 +2289,11 @@ public void endVisit(TypeDeclaration node) {
22562289
buffer.append(");\r\n");
22572290
staticCount = -1;
22582291
}
2259-
element.accept(this);
2292+
if ((((Initializer) element).getModifiers() & Modifier.STATIC) != 0) {
2293+
element.accept(this);
2294+
} else {
2295+
continue; // ignore here
2296+
}
22602297
} else if (element instanceof FieldDeclaration) {
22612298
FieldDeclaration field = (FieldDeclaration) element;
22622299
if ((field.getModifiers() & Modifier.STATIC) != 0) {

0 commit comments

Comments
 (0)