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 (-3 / +107 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[] { 40 };
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 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
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=150074
2170
public void test040() { 
2171
	this.runConformTest(
2172
		new String[] {
2173
			"X.java",
2174
			"public class X {\n" + 
2175
			"        public static void main(String[] args) {\n" + 
2176
			"                for (int i : initForEach()) {\n" + 
2177
			"                }\n" + 
2178
			"        }\n" + 
2179
			"        static int[] initForEach() {\n" + 
2180
			"                System.out.println(\"initForEach\");\n" + 
2181
			"                return new int[] {1, 2, 3, 4};\n" + 
2182
			"        }\n" + 
2183
			"}",
2184
		},
2185
		"initForEach");
2186
2187
	String expectedOutput =
2188
		"  // Method descriptor #15 ([Ljava/lang/String;)V\n" + 
2189
		"  // Stack: 1, Locals: 1\n" + 
2190
		"  public static void main(java.lang.String[] args);\n" + 
2191
		"    0  invokestatic X.initForEach() : int[] [16]\n" + 
2192
		"    3  pop\n" + 
2193
		"    4  return\n";
2194
	
2195
	try {
2196
		File f = new File(OUTPUT_DIR + File.separator + "X.class");
2197
		byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f);
2198
		ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
2199
		String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED);
2200
		int index = result.indexOf(expectedOutput);
2201
		if (index == -1 || expectedOutput.length() == 0) {
2202
			System.out.println(Util.displayString(result, 3));
2203
		}
2204
		if (index == -1) {
2205
			assertEquals("Wrong contents", expectedOutput, result);
2206
		}
2207
	} catch (org.eclipse.jdt.core.util.ClassFormatException e) {
2208
		assertTrue(false);
2209
	} catch (IOException e) {
2210
		assertTrue(false);
2211
	}
2212
}
2109
public static Class testClass() {
2213
public static Class testClass() {
2110
	return ForeachStatementTest.class;
2214
	return ForeachStatementTest.class;
2111
}
2215
}

Return to bug 150074