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\n 1t1\n 2t2\n 3t3" ));
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\n 4t4\n 5t5\n 6t6" ));
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+ }
0 commit comments