Skip to content

Commit 1705455

Browse files
committed
2019.11.20 3.2.5-v1
Java2ScriptVisitor refactoring to remove old level-based this.$finals$ and replace that with a simple class-based HashSet that is loaded when a final variable is used and then used later to create the {a:a,b:this.$finals$.b} object that goes into this.$finals$.
1 parent efcb06a commit 1705455

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class Test_Class extends Test_Class2 {
1616
static int istatic = 5;
1717
static String sstatic = "test5";
1818

19+
static class Singleton {
20+
// reference to Test_Class.Singleton.instance
21+
// lazily initializes a new instance of Test_Class()
22+
static Test_Class instance = new Test_Class();
23+
// actually, not recommended for JavaScript, because this
24+
// instance would be shared among applications, unlike in Java.
25+
}
1926

2027
static {
2128
System.out.println("Test_Class static init " + istatic + " " + sstatic);
@@ -226,6 +233,8 @@ Class C() {
226233

227234
public static void main(String[] args) {
228235

236+
237+
System.out.println(Test_Class.Singleton.instance);
229238
String ss = "testing \10\13a \7777 \u0052";
230239
System.out.println(ss + " "+ ss.length());
231240
try {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package test;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.io.StringReader;
6+
import java.util.Map;
7+
import java.util.stream.Collector;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
/**
12+
* Check for proper final references for anonymous subclasses of local nonanonymous classes.
13+
* Check for proper final references for streams (where this issue was found).
14+
*
15+
* @author hansonr
16+
*
17+
*/
18+
public abstract class Test_Local extends Test_ {
19+
20+
static int nn = 0;
21+
22+
static abstract class Test_Local1 {
23+
24+
int a, b;
25+
26+
Test_Local1(int a, int b) {
27+
this.a = a * -(++nn);
28+
this.b = b * -(++nn);
29+
System.out.println("Test_Local1 a=" + a + " " + " b=" + b);
30+
}
31+
32+
abstract int showN(int n);
33+
34+
abstract Stream<String[]> splitStream();
35+
36+
public void testMe() {
37+
System.out.println("Test_Local1 testme a+b=" + (a + b));
38+
}
39+
}
40+
41+
static Test_Local1 newLocal1(int a, int b, String testing, Stream<String> x) {
42+
int c = a + b;
43+
44+
class ReducingSink extends Test_Local1 {
45+
46+
ReducingSink() {
47+
super(5, 6);
48+
}
49+
50+
@Override
51+
public void testMe() {
52+
System.out.println(" testMe c=" + c);
53+
super.testMe();
54+
}
55+
56+
@Override
57+
Stream<String[]> splitStream() {
58+
check(x);
59+
return x.map(line -> line.split("t"));
60+
}
61+
62+
private void check(Stream<String> x) {
63+
System.out.println("check x is " + x);
64+
65+
}
66+
67+
@Override
68+
int showN(int n) {
69+
System.out.println(" showN a=" + a + " b=" + b + " c=" + c + " n=" + n);
70+
return c;
71+
}
72+
73+
}
74+
// var xx=Clazz.new_($I$(2,1), [this, {c: c, x: x}],P$.Test_Local$1ReducingSink);
75+
// var yy=((P$.Test_Local$1||
76+
// (function(){/*type=a*/var C$=Clazz.newClass(P$,
77+
// "Test_Local$1",
78+
// function(){Clazz.newInstance(this, arguments[0],1,C$);},
79+
// Clazz.load('test.Test_Local$1ReducingSink'), null, 1);
80+
//
81+
// C$.$clinit$=1;
82+
//...
83+
// return this.$finals$.c;
84+
// });
85+
// })()
86+
// ), Clazz.new_($I$(3,1), [this, {c: c}],P$.Test_Local$1));
87+
88+
// var yy=((P$.Test_Local$1||
89+
// (function(){/*type=a*/var C$=Clazz.newClass(P$, "Test_Local$1", function(){Clazz.newInstance(this, arguments[0],1,C$);}, Clazz.load('test.Test_Local$1ReducingSink'), null, 1);
90+
//
91+
// C$.$clinit$=1;
92+
//...
93+
// System.out.println$S(" showN c=" + this.$finals$.c + " n=" + n );
94+
// return this.$finals$.c;
95+
// });
96+
// })()
97+
// ), Clazz.new_($I$(2,1), [this, {c: c}],P$.Test_Local$1));
98+
99+
// var xx=Clazz.new_($I$(3,1), [this, {c: c, x: x}],P$.Test_Local$1ReducingSink);
100+
101+
// was ok: ReducingSink xx = new ReducingSink();
102+
103+
// was missing x:
104+
ReducingSink yy = new ReducingSink() {
105+
106+
@Override
107+
int showN(int n) {
108+
super.showN(n);
109+
System.out.println(" showN c=" + c + " n=" + n);
110+
return c;
111+
}
112+
};
113+
114+
115+
return yy;
116+
}
117+
118+
public static void main(String[] args) {
119+
120+
Runnable r2 = () -> System. out.println("Hello world two!");
121+
BufferedReader lookupReader = new BufferedReader(new StringReader("testing\n1t1\n2t2\n3t3"));
122+
123+
Stream<String> x = lookupReader.lines();
124+
125+
Test_Local1 tl1 = newLocal1(3, 7, "This is a test", x);
126+
assert (tl1.showN(20) == 10);
127+
tl1.testMe();
128+
assert(tl1.a == -5);
129+
System.out.println(tl1.splitStream().collect(Collectors.toList()).get(2)[1]);
130+
131+
lookupReader = new BufferedReader(new StringReader("testing\n4t4\n5t5\n6t6"));
132+
133+
x = lookupReader.lines();
134+
Stream<String[]> y = x.map(line -> line.split("t"));
135+
Collector<String[], ?, Map<String, String>> z = Collectors.toMap(
136+
split -> split[0],
137+
split -> split[1]);
138+
Map<String, String> rawMap = y.collect(z);
139+
System.out.println(rawMap.entrySet().toString());
140+
assert(rawMap.entrySet().size() == 4);
141+
Test_Local1 tl2 = newLocal1(30, 70, "This is another test", x);
142+
assert (tl2.showN(80) == 100);
143+
tl2.testMe();
144+
assert(tl2.a == -15);
145+
assert (tl1.showN(20) == 10);
146+
tl1.testMe();
147+
assert(tl1.a == -5);
148+
149+
System.out.println("Test_Local OK");
150+
151+
}
152+
153+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test;
2+
3+
class Test_Static1 extends Test_ {
4+
5+
public static class InnerStatic {
6+
7+
// public InnerStatic() {
8+
// System.out.println("innerStatic initializer");
9+
// }
10+
11+
12+
public void test(String...strings) {
13+
14+
}
15+
16+
}
17+
18+
public static void main(String[] args) {
19+
20+
new Test_Static1.InnerStatic();
21+
}
22+
23+
24+
}
25+

0 commit comments

Comments
 (0)