View | Details | Raw Unified | Return to bug 150074 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/ForeachStatementTest.java (-4 / +600 lines)
Lines 38-48 Link Here
38
}
38
}
39
// Static initializer to specify tests subset using TESTS_* static variables
39
// Static initializer to specify tests subset using TESTS_* static variables
40
// All specified tests which do not belong to the class are skipped...
40
// All specified tests which do not belong to the class are skipped...
41
//static {
41
static {
42
//	TESTS_NAMES = new String[] { "test000" };
42
//	TESTS_NAMES = new String[] { "test000" };
43
//	TESTS_NUMBERS = new int[] { 31 };
43
//	TESTS_NUMBERS = new int[] { 45, 46 };
44
//	TESTS_RANGE = new int[] { 34, 38 };
44
//	TESTS_RANGE = new int[] { 34, 38 };
45
//}
45
}
46
public static Test suite() {
46
public static Test suite() {
47
	return buildComparableTestSuite(testClass());
47
	return buildComparableTestSuite(testClass());
48
}
48
}
Lines 817-823 Link Here
817
		assertTrue(false);
817
		assertTrue(false);
818
	} catch (IOException e) {
818
	} catch (IOException e) {
819
		assertTrue(false);
819
		assertTrue(false);
820
	}	
820
	}
821
}
821
}
822
/*
822
/*
823
 * Break the loop
823
 * Break the loop
Lines 2106-2111 Link Here
2106
		assertTrue(false);
2106
		assertTrue(false);
2107
	}
2107
	}
2108
}
2108
}
2109
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2110
public void test039() { 
2111
	this.runConformTest(
2112
		new String[] {
2113
			"X.java",
2114
			"import java.util.HashSet;\n" + 
2115
			"import java.util.Set;\n" + 
2116
			"import java.util.Iterator;\n" + 
2117
			"\n" + 
2118
			"public class X {\n" + 
2119
			"\n" + 
2120
			"        public static void main(String[] args) {\n" + 
2121
			"                for (Object o : initForEach()) {\n" + 
2122
			"                }\n" + 
2123
			"        }\n" + 
2124
			"\n" + 
2125
			"		static class MyIterator<T> implements Iterator<T> {\n" + 
2126
			"			Iterator<T> iterator;\n" + 
2127
			"			\n" + 
2128
			"			MyIterator(Iterator<T> it) {\n" + 
2129
			"				this.iterator = it;\n" + 
2130
			"			}\n" + 
2131
			"			public boolean hasNext() {\n" + 
2132
			"				System.out.println(\"hasNext\");\n" + 
2133
			"				return this.iterator.hasNext();\n" + 
2134
			"			}			\n" + 
2135
			"			public T next() {\n" + 
2136
			"				System.out.println(\"next\");\n" + 
2137
			"				return this.iterator.next();\n" + 
2138
			"			}\n" + 
2139
			"			public void remove() {\n" + 
2140
			"				System.out.println(\"remove\");\n" + 
2141
			"				this.iterator.remove();\n" + 
2142
			"			}\n" + 
2143
			"		}\n" + 
2144
			"		\n" + 
2145
			"        static Set<Object> initForEach()        {\n" + 
2146
			"                System.out.println(\"initForEach\");\n" + 
2147
			"                HashSet<Object> set = new HashSet<Object>() {\n" + 
2148
			"                	private static final long serialVersionUID = 1L;\n" + 
2149
			"                	public Iterator<Object> iterator() {\n" + 
2150
			"                		System.out.println(\"iterator\");\n" + 
2151
			"                		return new MyIterator<Object>(super.iterator());\n" + 
2152
			"                	}\n" + 
2153
			"                };\n" + 
2154
			"                for (int i = 0; i < 3; i++) set.add(i);\n" + 
2155
			"                return set;\n" + 
2156
			"        }\n" + 
2157
			"}",
2158
		},
2159
		"initForEach\n" + 
2160
		"iterator\n" + 
2161
		"hasNext\n" + 
2162
		"next\n" + 
2163
		"hasNext\n" + 
2164
		"next\n" + 
2165
		"hasNext\n" + 
2166
		"next\n" + 
2167
		"hasNext");
2168
2169
	String expectedOutput =
2170
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2171
		"  // Stack: 1, Locals: 2\n" + 
2172
		"  public static void main(java.lang.String[] args);\n" + 
2173
		"     0  invokestatic X.initForEach() : java.util.Set [16]\n" + 
2174
		"     3  invokeinterface java.util.Set.iterator() : java.util.Iterator [20] [nargs: 1]\n" + 
2175
		"     8  astore_1\n" + 
2176
		"     9  goto 19\n" + 
2177
		"    12  aload_1\n" + 
2178
		"    13  invokeinterface java.util.Iterator.next() : java.lang.Object [26] [nargs: 1]\n" + 
2179
		"    18  pop\n" + 
2180
		"    19  aload_1\n" + 
2181
		"    20  invokeinterface java.util.Iterator.hasNext() : boolean [32] [nargs: 1]\n" + 
2182
		"    25  ifne 12\n" + 
2183
		"    28  return\n" + 
2184
		"      Line numbers:\n" + 
2185
		"        [pc: 0, line: 8]\n" + 
2186
		"        [pc: 28, line: 10]\n" + 
2187
		"      Local variable table:\n" + 
2188
		"        [pc: 0, pc: 29] local: args index: 0 type: java.lang.String[]\n";
2189
	
2190
	try {
2191
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2192
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2193
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2194
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2195
		int index = result.indexOf(expectedOutput);
2196
		if (index == -1 || expectedOutput.length() == 0) {
2197
			System.out.println(Util.displayString(result, 3));
2198
		}
2199
		if (index == -1) {
2200
			assertEquals("Wrong contents", expectedOutput, result);
2201
		}
2202
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2203
		assertTrue(false);
2204
	} catch (IOException e) {
2205
		assertTrue(false);
2206
	}
2207
}
2208
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2209
public void test040() { 
2210
	Map options = this.getCompilerOptions();
2211
	options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.PRESERVE);
2212
	
2213
	this.runConformTest(
2214
		new String[] {
2215
			"X.java",
2216
			"import java.util.HashSet;\n" + 
2217
			"import java.util.Set;\n" + 
2218
			"import java.util.Iterator;\n" + 
2219
			"\n" + 
2220
			"public class X {\n" + 
2221
			"\n" + 
2222
			"        public static void main(String[] args) {\n" + 
2223
			"                for (Object o : initForEach()) {\n" + 
2224
			"                }\n" + 
2225
			"        }\n" + 
2226
			"\n" + 
2227
			"		static class MyIterator<T> implements Iterator<T> {\n" + 
2228
			"			Iterator<T> iterator;\n" + 
2229
			"			\n" + 
2230
			"			MyIterator(Iterator<T> it) {\n" + 
2231
			"				this.iterator = it;\n" + 
2232
			"			}\n" + 
2233
			"			public boolean hasNext() {\n" + 
2234
			"				System.out.println(\"hasNext\");\n" + 
2235
			"				return this.iterator.hasNext();\n" + 
2236
			"			}			\n" + 
2237
			"			public T next() {\n" + 
2238
			"				System.out.println(\"next\");\n" + 
2239
			"				return this.iterator.next();\n" + 
2240
			"			}\n" + 
2241
			"			public void remove() {\n" + 
2242
			"				System.out.println(\"remove\");\n" + 
2243
			"				this.iterator.remove();\n" + 
2244
			"			}\n" + 
2245
			"		}\n" + 
2246
			"		\n" + 
2247
			"        static Set<Object> initForEach()        {\n" + 
2248
			"                System.out.println(\"initForEach\");\n" + 
2249
			"                HashSet<Object> set = new HashSet<Object>() {\n" + 
2250
			"                	private static final long serialVersionUID = 1L;\n" + 
2251
			"                	public Iterator<Object> iterator() {\n" + 
2252
			"                		System.out.println(\"iterator\");\n" + 
2253
			"                		return new MyIterator<Object>(super.iterator());\n" + 
2254
			"                	}\n" + 
2255
			"                };\n" + 
2256
			"                for (int i = 0; i < 3; i++) set.add(i);\n" + 
2257
			"                return set;\n" + 
2258
			"        }\n" + 
2259
			"}",
2260
		},
2261
		"initForEach\n" + 
2262
		"iterator\n" + 
2263
		"hasNext\n" + 
2264
		"next\n" + 
2265
		"hasNext\n" + 
2266
		"next\n" + 
2267
		"hasNext\n" + 
2268
		"next\n" + 
2269
		"hasNext",
2270
		null,
2271
		true,
2272
		null,
2273
		options,
2274
		null);
2275
2276
	String expectedOutput =
2277
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2278
		"  // Stack: 1, Locals: 3\n" + 
2279
		"  public static void main(java.lang.String[] args);\n" + 
2280
		"     0  invokestatic X.initForEach() : java.util.Set [16]\n" + 
2281
		"     3  invokeinterface java.util.Set.iterator() : java.util.Iterator [20] [nargs: 1]\n" + 
2282
		"     8  astore_2\n" + 
2283
		"     9  goto 19\n" + 
2284
		"    12  aload_2\n" + 
2285
		"    13  invokeinterface java.util.Iterator.next() : java.lang.Object [26] [nargs: 1]\n" + 
2286
		"    18  astore_1 [o]\n" + 
2287
		"    19  aload_2\n" + 
2288
		"    20  invokeinterface java.util.Iterator.hasNext() : boolean [32] [nargs: 1]\n" + 
2289
		"    25  ifne 12\n" + 
2290
		"    28  return\n" + 
2291
		"      Line numbers:\n" + 
2292
		"        [pc: 0, line: 8]\n" + 
2293
		"        [pc: 28, line: 10]\n" + 
2294
		"      Local variable table:\n" + 
2295
		"        [pc: 0, pc: 29] local: args index: 0 type: java.lang.String[]\n" + 
2296
		"        [pc: 19, pc: 28] local: o index: 1 type: java.lang.Object\n";
2297
	
2298
	try {
2299
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2300
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2301
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2302
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2303
		int index = result.indexOf(expectedOutput);
2304
		if (index == -1 || expectedOutput.length() == 0) {
2305
			System.out.println(Util.displayString(result, 3));
2306
		}
2307
		if (index == -1) {
2308
			assertEquals("Wrong contents", expectedOutput, result);
2309
		}
2310
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2311
		assertTrue(false);
2312
	} catch (IOException e) {
2313
		assertTrue(false);
2314
	}
2315
}
2316
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2317
public void test041() { 
2318
	this.runConformTest(
2319
		new String[] {
2320
			"X.java",
2321
			"public class X {\n" + 
2322
			"        public static void main(String[] args) {\n" + 
2323
			"                for (int i : initForEach()) {\n" + 
2324
			"                }\n" + 
2325
			"        }\n" + 
2326
			"        static int[] initForEach() {\n" + 
2327
			"                System.out.println(\"initForEach\");\n" + 
2328
			"                return new int[] {1, 2, 3, 4};\n" + 
2329
			"        }\n" + 
2330
			"}",
2331
		},
2332
		"initForEach");
2333
2334
	String expectedOutput =
2335
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2336
		"  // Stack: 1, Locals: 1\n" + 
2337
		"  public static void main(java.lang.String[] args);\n" + 
2338
		"    0  invokestatic X.initForEach() : int[] [16]\n" + 
2339
		"    3  pop\n" + 
2340
		"    4  return\n";
2341
	
2342
	try {
2343
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2344
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2345
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2346
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2347
		int index = result.indexOf(expectedOutput);
2348
		if (index == -1 || expectedOutput.length() == 0) {
2349
			System.out.println(Util.displayString(result, 3));
2350
		}
2351
		if (index == -1) {
2352
			assertEquals("Wrong contents", expectedOutput, result);
2353
		}
2354
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2355
		assertTrue(false);
2356
	} catch (IOException e) {
2357
		assertTrue(false);
2358
	}
2359
}
2360
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2361
public void test042() { 
2362
	Map options = this.getCompilerOptions();
2363
	options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.PRESERVE);
2364
	
2365
	this.runConformTest(
2366
		new String[] {
2367
			"X.java",
2368
			"public class X {\n" + 
2369
			"        public static void main(String[] args) {\n" + 
2370
			"                for (int i : initForEach()) {\n" + 
2371
			"                }\n" + 
2372
			"        }\n" + 
2373
			"        static int[] initForEach() {\n" + 
2374
			"                System.out.println(\"initForEach\");\n" + 
2375
			"                return new int[] {1, 2, 3, 4};\n" + 
2376
			"        }\n" + 
2377
			"}",
2378
		},
2379
		"initForEach",
2380
		null,
2381
		true,
2382
		null,
2383
		options,
2384
		null);
2385
2386
	String expectedOutput =
2387
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2388
		"  // Stack: 2, Locals: 5\n" + 
2389
		"  public static void main(java.lang.String[] args);\n" + 
2390
		"     0  invokestatic X.initForEach() : int[] [16]\n" + 
2391
		"     3  astore 4\n" + 
2392
		"     5  iconst_0\n" + 
2393
		"     6  istore_2\n" + 
2394
		"     7  aload 4\n" + 
2395
		"     9  arraylength\n" + 
2396
		"    10  istore_3\n" + 
2397
		"    11  goto 22\n" + 
2398
		"    14  aload 4\n" + 
2399
		"    16  iload_2\n" + 
2400
		"    17  iaload\n" + 
2401
		"    18  istore_1 [i]\n" + 
2402
		"    19  iinc 2 1\n" + 
2403
		"    22  iload_2\n" + 
2404
		"    23  iload_3\n" + 
2405
		"    24  if_icmplt 14\n" + 
2406
		"    27  return\n" + 
2407
		"      Line numbers:\n" + 
2408
		"        [pc: 0, line: 3]\n" + 
2409
		"        [pc: 27, line: 5]\n" + 
2410
		"      Local variable table:\n" + 
2411
		"        [pc: 0, pc: 28] local: args index: 0 type: java.lang.String[]\n" + 
2412
		"        [pc: 19, pc: 27] local: i index: 1 type: int\n";
2413
	
2414
	try {
2415
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2416
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2417
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2418
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2419
		int index = result.indexOf(expectedOutput);
2420
		if (index == -1 || expectedOutput.length() == 0) {
2421
			System.out.println(Util.displayString(result, 3));
2422
		}
2423
		if (index == -1) {
2424
			assertEquals("Wrong contents", expectedOutput, result);
2425
		}
2426
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2427
		assertTrue(false);
2428
	} catch (IOException e) {
2429
		assertTrue(false);
2430
	}
2431
}
2432
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2433
public void test043() { 
2434
	this.runConformTest(
2435
		new String[] {
2436
			"X.java",
2437
			"public class X {\n" + 
2438
			"        public static void main(String[] args) {\n" + 
2439
			"			 foo();\n" +
2440
			"        }\n" + 
2441
			"        public static void foo() {\n" + 
2442
			"                for (int i : initForEach()) {\n" + 
2443
			"                }\n" + 
2444
			"        }\n" + 
2445
			"        static int[] initForEach() {\n" + 
2446
			"                System.out.println(\"initForEach\");\n" + 
2447
			"                return new int[] {1, 2, 3, 4};\n" + 
2448
			"        }\n" + 
2449
			"}",
2450
		},
2451
		"initForEach");
2452
2453
	String expectedOutput =
2454
		"  // Method descriptor #6 ()V\n" + 
2455
		"  // Stack: 1, Locals: 0\n" + 
2456
		"  public static void foo();\n" + 
2457
		"    0  invokestatic X.initForEach() : int[] [21]\n" + 
2458
		"    3  pop\n" + 
2459
		"    4  return\n" + 
2460
		"      Line numbers:\n" + 
2461
		"        [pc: 0, line: 6]\n" + 
2462
		"        [pc: 4, line: 8]\n";
2463
	
2464
	try {
2465
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2466
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2467
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2468
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2469
		int index = result.indexOf(expectedOutput);
2470
		if (index == -1 || expectedOutput.length() == 0) {
2471
			System.out.println(Util.displayString(result, 3));
2472
		}
2473
		if (index == -1) {
2474
			assertEquals("Wrong contents", expectedOutput, result);
2475
		}
2476
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2477
		assertTrue(false);
2478
	} catch (IOException e) {
2479
		assertTrue(false);
2480
	}
2481
}
2482
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2483
public void test044() { 
2484
	Map options = this.getCompilerOptions();
2485
	options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.PRESERVE);
2486
2487
	this.runConformTest(
2488
		new String[] {
2489
			"X.java",
2490
			"public class X {\n" + 
2491
			"        public static void main(String[] args) {\n" + 
2492
			"			 foo();\n" +
2493
			"        }\n" + 
2494
			"        public static void foo() {\n" + 
2495
			"                for (int i : initForEach()) {\n" + 
2496
			"                }\n" + 
2497
			"        }\n" + 
2498
			"        static int[] initForEach() {\n" + 
2499
			"                System.out.println(\"initForEach\");\n" + 
2500
			"                return new int[] {1, 2, 3, 4};\n" + 
2501
			"        }\n" + 
2502
			"}",
2503
		},
2504
		"initForEach",
2505
		null,
2506
		true,
2507
		null,
2508
		options,
2509
		null);
2510
2511
	String expectedOutput =
2512
		"  // Method descriptor #6 ()V\n" + 
2513
		"  // Stack: 2, Locals: 4\n" + 
2514
		"  public static void foo();\n" + 
2515
		"     0  invokestatic X.initForEach() : int[] [21]\n" + 
2516
		"     3  astore_3\n" + 
2517
		"     4  iconst_0\n" + 
2518
		"     5  istore_1\n" + 
2519
		"     6  aload_3\n" + 
2520
		"     7  arraylength\n" + 
2521
		"     8  istore_2\n" + 
2522
		"     9  goto 19\n" + 
2523
		"    12  aload_3\n" + 
2524
		"    13  iload_1\n" + 
2525
		"    14  iaload\n" + 
2526
		"    15  istore_0 [i]\n" + 
2527
		"    16  iinc 1 1\n" + 
2528
		"    19  iload_1\n" + 
2529
		"    20  iload_2\n" + 
2530
		"    21  if_icmplt 12\n" + 
2531
		"    24  return\n" + 
2532
		"      Line numbers:\n" + 
2533
		"        [pc: 0, line: 6]\n" + 
2534
		"        [pc: 24, line: 8]\n" + 
2535
		"      Local variable table:\n" + 
2536
		"        [pc: 16, pc: 24] local: i index: 0 type: int\n";
2537
	
2538
	try {
2539
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2540
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2541
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2542
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2543
		int index = result.indexOf(expectedOutput);
2544
		if (index == -1 || expectedOutput.length() == 0) {
2545
			System.out.println(Util.displayString(result, 3));
2546
		}
2547
		if (index == -1) {
2548
			assertEquals("Wrong contents", expectedOutput, result);
2549
		}
2550
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2551
		assertTrue(false);
2552
	} catch (IOException e) {
2553
		assertTrue(false);
2554
	}
2555
}
2556
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2557
public void test045() { 
2558
	Map options = this.getCompilerOptions();
2559
	options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.PRESERVE);
2560
2561
	this.runConformTest(
2562
		new String[] {
2563
			"X.java",
2564
			"public class X {\n" + 
2565
			"        public static void main(String[] args) {\n" + 
2566
			"                for (int i : initForEach()) {\n" + 
2567
			"                	System.out.print(\'a\');\n" + 
2568
			"                }\n" + 
2569
			"        }\n" + 
2570
			"        static int[] initForEach() {\n" + 
2571
			"                return new int[] {1, 2, 3, 4};\n" + 
2572
			"        }\n" + 
2573
			"}",
2574
		},
2575
		"aaaa",
2576
		null,
2577
		true,
2578
		null,
2579
		options,
2580
		null);
2581
2582
	String expectedOutput =
2583
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2584
		"  // Stack: 2, Locals: 5\n" + 
2585
		"  public static void main(java.lang.String[] args);\n" + 
2586
		"     0  invokestatic X.initForEach() : int[] [16]\n" + 
2587
		"     3  astore 4\n" + 
2588
		"     5  iconst_0\n" + 
2589
		"     6  istore_2\n" + 
2590
		"     7  aload 4\n" + 
2591
		"     9  arraylength\n" + 
2592
		"    10  istore_3\n" + 
2593
		"    11  goto 30\n" + 
2594
		"    14  aload 4\n" + 
2595
		"    16  iload_2\n" + 
2596
		"    17  iaload\n" + 
2597
		"    18  istore_1 [i]\n" + 
2598
		"    19  getstatic java.lang.System.out : java.io.PrintStream [20]\n" + 
2599
		"    22  bipush 97\n" + 
2600
		"    24  invokevirtual java.io.PrintStream.print(char) : void [26]\n" + 
2601
		"    27  iinc 2 1\n" + 
2602
		"    30  iload_2\n" + 
2603
		"    31  iload_3\n" + 
2604
		"    32  if_icmplt 14\n" + 
2605
		"    35  return\n" + 
2606
		"      Line numbers:\n" + 
2607
		"        [pc: 0, line: 3]\n" + 
2608
		"        [pc: 19, line: 4]\n" + 
2609
		"        [pc: 27, line: 3]\n" + 
2610
		"        [pc: 35, line: 6]\n" + 
2611
		"      Local variable table:\n" + 
2612
		"        [pc: 0, pc: 36] local: args index: 0 type: java.lang.String[]\n" + 
2613
		"        [pc: 19, pc: 35] local: i index: 1 type: int\n";
2614
	
2615
	try {
2616
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2617
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2618
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2619
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2620
		int index = result.indexOf(expectedOutput);
2621
		if (index == -1 || expectedOutput.length() == 0) {
2622
			System.out.println(Util.displayString(result, 3));
2623
		}
2624
		if (index == -1) {
2625
			assertEquals("Wrong contents", expectedOutput, result);
2626
		}
2627
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2628
		assertTrue(false);
2629
	} catch (IOException e) {
2630
		assertTrue(false);
2631
	}
2632
}
2633
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2634
public void test046() { 
2635
	Map options = this.getCompilerOptions();
2636
	options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT);
2637
2638
	this.runConformTest(
2639
		new String[] {
2640
			"X.java",
2641
			"public class X {\n" + 
2642
			"        public static void main(String[] args) {\n" + 
2643
			"                for (int i : initForEach()) {\n" + 
2644
			"                	System.out.print(\'a\');\n" + 
2645
			"                }\n" + 
2646
			"        }\n" + 
2647
			"        static int[] initForEach() {\n" + 
2648
			"                return new int[] {1, 2, 3, 4};\n" + 
2649
			"        }\n" + 
2650
			"}",
2651
		},
2652
		"aaaa",
2653
		null,
2654
		true,
2655
		null,
2656
		options,
2657
		null);
2658
2659
	String expectedOutput =
2660
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2661
		"  // Stack: 2, Locals: 4\n" + 
2662
		"  public static void main(java.lang.String[] args);\n" + 
2663
		"     0  invokestatic X.initForEach() : int[] [16]\n" + 
2664
		"     3  astore_3\n" + 
2665
		"     4  iconst_0\n" + 
2666
		"     5  istore_1\n" + 
2667
		"     6  aload_3\n" + 
2668
		"     7  arraylength\n" + 
2669
		"     8  istore_2\n" + 
2670
		"     9  goto 23\n" + 
2671
		"    12  getstatic java.lang.System.out : java.io.PrintStream [20]\n" + 
2672
		"    15  bipush 97\n" + 
2673
		"    17  invokevirtual java.io.PrintStream.print(char) : void [26]\n" + 
2674
		"    20  iinc 1 1\n" + 
2675
		"    23  iload_1\n" + 
2676
		"    24  iload_2\n" + 
2677
		"    25  if_icmplt 12\n" + 
2678
		"    28  return\n" + 
2679
		"      Line numbers:\n" + 
2680
		"        [pc: 0, line: 3]\n" + 
2681
		"        [pc: 12, line: 4]\n" + 
2682
		"        [pc: 20, line: 3]\n" + 
2683
		"        [pc: 28, line: 6]\n" + 
2684
		"      Local variable table:\n" + 
2685
		"        [pc: 0, pc: 29] local: args index: 0 type: java.lang.String[]\n";
2686
	
2687
	try {
2688
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2689
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2690
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2691
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2692
		int index = result.indexOf(expectedOutput);
2693
		if (index == -1 || expectedOutput.length() == 0) {
2694
			System.out.println(Util.displayString(result, 3));
2695
		}
2696
		if (index == -1) {
2697
			assertEquals("Wrong contents", expectedOutput, result);
2698
		}
2699
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2700
		assertTrue(false);
2701
	} catch (IOException e) {
2702
		assertTrue(false);
2703
	}
2704
}
2109
public static Class testClass() {
2705
public static Class testClass() {
2110
	return ForeachStatementTest.class;
2706
	return ForeachStatementTest.class;
2111
}
2707
}

Return to bug 150074