Lines 10-18
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.tests.compiler.regression; |
11 |
package org.eclipse.jdt.tests.compiler.regression; |
12 |
|
12 |
|
|
|
13 |
import java.io.File; |
14 |
import java.io.IOException; |
13 |
import java.util.Hashtable; |
15 |
import java.util.Hashtable; |
|
|
16 |
import java.util.Map; |
14 |
|
17 |
|
|
|
18 |
import org.eclipse.jdt.core.ToolFactory; |
15 |
import org.eclipse.jdt.core.tests.compiler.regression.*; |
19 |
import org.eclipse.jdt.core.tests.compiler.regression.*; |
|
|
20 |
import org.eclipse.jdt.core.tests.util.Util; |
21 |
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; |
22 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
16 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
23 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
17 |
|
24 |
|
18 |
import junit.framework.Test; |
25 |
import junit.framework.Test; |
Lines 5101-5106
Link Here
|
5101 |
" ^^^^\n" + |
5108 |
" ^^^^\n" + |
5102 |
"Zork cannot be resolved to a type\n" + |
5109 |
"Zork cannot be resolved to a type\n" + |
5103 |
"----------\n"); |
5110 |
"----------\n"); |
|
|
5111 |
} |
5112 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=159021 |
5113 |
public void test189() { |
5114 |
Map options = getCompilerOptions(); |
5115 |
options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT); |
5116 |
this.runConformTest( |
5117 |
new String[] { |
5118 |
"X.java", |
5119 |
"interface I {\n" + |
5120 |
" int CONST = A.foo();\n" + |
5121 |
"}\n" + |
5122 |
"\n" + |
5123 |
"class A {\n" + |
5124 |
" static int foo() {\n" + |
5125 |
" System.out.println(\"SUCCESS\");\n" + |
5126 |
" return 0;\n" + |
5127 |
" }\n" + |
5128 |
"}\n" + |
5129 |
"class B implements I {\n" + |
5130 |
" static int LOCAL_STATIC;\n" + |
5131 |
" int local_field;\n" + |
5132 |
" B(int param) {\n" + |
5133 |
" int i = CONST; // keep for possible <clinit>\n" + |
5134 |
" int j = param; // may optimize out\n" + |
5135 |
" int k = LOCAL_STATIC; // may optimize out\n" + |
5136 |
" int l = local_field; // may optimize out\n" + |
5137 |
" }\n" + |
5138 |
"}\n" + |
5139 |
"public class X {\n" + |
5140 |
" public static void main(String[] args) {\n" + |
5141 |
" new B(12);\n" + |
5142 |
" }\n" + |
5143 |
"}", // ================= |
5144 |
}, |
5145 |
"SUCCESS", |
5146 |
null, |
5147 |
false, |
5148 |
null, |
5149 |
options, |
5150 |
null); |
5151 |
// check the reference to I.CONST still got generated (for <clinit> invocation side-effect) |
5152 |
String expectedOutput = new CompilerOptions(options).complianceLevel < ClassFileConstants.JDK1_4 |
5153 |
? " // Method descriptor #10 (I)V\n" + |
5154 |
" // Stack: 1, Locals: 2\n" + |
5155 |
" B(int param);\n" + |
5156 |
" 0 aload_0 [this]\n" + |
5157 |
" 1 invokespecial java.lang.Object() [12]\n" + |
5158 |
" 4 getstatic I.CONST : int [15]\n" + |
5159 |
" 7 pop\n" + |
5160 |
" 8 return\n" + |
5161 |
" Line numbers:\n" + |
5162 |
" [pc: 0, line: 14]\n" + |
5163 |
" [pc: 4, line: 15]\n" + |
5164 |
" [pc: 8, line: 19]\n" + |
5165 |
" Local variable table:\n" + |
5166 |
" [pc: 0, pc: 9] local: this index: 0 type: B\n" + |
5167 |
" [pc: 0, pc: 9] local: param index: 1 type: int\n" |
5168 |
: " // Method descriptor #10 (I)V\n" + |
5169 |
" // Stack: 1, Locals: 2\n" + |
5170 |
" B(int param);\n" + |
5171 |
" 0 aload_0 [this]\n" + |
5172 |
" 1 invokespecial java.lang.Object() [12]\n" + |
5173 |
" 4 getstatic B.CONST : int [15]\n" + |
5174 |
" 7 pop\n" + |
5175 |
" 8 return\n" + |
5176 |
" Line numbers:\n" + |
5177 |
" [pc: 0, line: 14]\n" + |
5178 |
" [pc: 4, line: 15]\n" + |
5179 |
" [pc: 8, line: 19]\n" + |
5180 |
" Local variable table:\n" + |
5181 |
" [pc: 0, pc: 9] local: this index: 0 type: B\n" + |
5182 |
" [pc: 0, pc: 9] local: param index: 1 type: int\n"; |
5183 |
|
5184 |
try { |
5185 |
File f = new File(OUTPUT_DIR + File.separator + "B.class"); |
5186 |
byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f); |
5187 |
ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); |
5188 |
String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED); |
5189 |
int index = result.indexOf(expectedOutput); |
5190 |
if (index == -1 || expectedOutput.length() == 0) { |
5191 |
System.out.println(Util.displayString(result, 3)); |
5192 |
} |
5193 |
if (index == -1) { |
5194 |
assertEquals("Wrong contents", expectedOutput, result); |
5195 |
} |
5196 |
} catch (org.eclipse.jdt.core.util.ClassFormatException e) { |
5197 |
assertTrue(false); |
5198 |
} catch (IOException e) { |
5199 |
assertTrue(false); |
5200 |
} |
5201 |
} |
5202 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=159021 - variation |
5203 |
public void test190() { |
5204 |
Map options = getCompilerOptions(); |
5205 |
options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT); |
5206 |
this.runConformTest( |
5207 |
new String[] { |
5208 |
"X.java", |
5209 |
"interface I {\n" + |
5210 |
" Value CONST = A.foo(\"[I.CONST]\");\n" + |
5211 |
"}\n" + |
5212 |
"class Value {\n" + |
5213 |
" static String NAME = \"\";\n" + |
5214 |
" String v;\n" + |
5215 |
" Value(String v) {\n" + |
5216 |
" this.v = v;\n" + |
5217 |
" }\n" + |
5218 |
"}\n" + |
5219 |
"class A {\n" + |
5220 |
" static Value foo(String str) {\n" + |
5221 |
" System.out.print(str);\n" + |
5222 |
" return new Value(\"str\");\n" + |
5223 |
" }\n" + |
5224 |
"}\n" + |
5225 |
"class B implements I {\n" + |
5226 |
" static Value LOCAL_STATIC = A.foo(\"[B.LOCAL_STATIC]\");\n" + |
5227 |
" Value local_field = A.foo(\"[B.local_field]\");\n" + |
5228 |
" B(Value param) {\n" + |
5229 |
" String i = CONST.NAME; // keep for possible <clinit>\n" + |
5230 |
" String j = param.NAME; // may optimize out\n" + |
5231 |
" String k = LOCAL_STATIC.NAME; // may optimize out\n" + |
5232 |
" String l = local_field.NAME; // may optimize out\n" + |
5233 |
" }\n" + |
5234 |
"}\n" + |
5235 |
"public class X {\n" + |
5236 |
" public static void main(String[] args) {\n" + |
5237 |
" new B(new Value(\"[PARAM]\"));\n" + |
5238 |
" }\n" + |
5239 |
"}", // ================= |
5240 |
}, |
5241 |
"[B.LOCAL_STATIC][B.local_field][I.CONST]", |
5242 |
null, |
5243 |
false, |
5244 |
null, |
5245 |
options, |
5246 |
null); |
5247 |
// check the reference to I.CONST still got generated (for <clinit> invocation side-effect) |
5248 |
String expectedOutput = new CompilerOptions(options).complianceLevel < ClassFileConstants.JDK1_4 |
5249 |
? " // Method descriptor #26 (LValue;)V\n" + |
5250 |
" // Stack: 2, Locals: 2\n" + |
5251 |
" B(Value param);\n" + |
5252 |
" 0 aload_0 [this]\n" + |
5253 |
" 1 invokespecial java.lang.Object() [27]\n" + |
5254 |
" 4 aload_0 [this]\n" + |
5255 |
" 5 ldc <String \"[B.local_field]\"> [29]\n" + |
5256 |
" 7 invokestatic A.foo(java.lang.String) : Value [15]\n" + |
5257 |
" 10 putfield B.local_field : Value [31]\n" + |
5258 |
" 13 getstatic I.CONST : Value [33]\n" + |
5259 |
" 16 pop\n" + |
5260 |
" 17 return\n" + |
5261 |
" Line numbers:\n" + |
5262 |
" [pc: 0, line: 20]\n" + |
5263 |
" [pc: 4, line: 19]\n" + |
5264 |
" [pc: 13, line: 21]\n" + |
5265 |
" [pc: 17, line: 25]\n" + |
5266 |
" Local variable table:\n" + |
5267 |
" [pc: 0, pc: 18] local: this index: 0 type: B\n" + |
5268 |
" [pc: 0, pc: 18] local: param index: 1 type: Value\n" |
5269 |
: " // Method descriptor #26 (LValue;)V\n" + |
5270 |
" // Stack: 2, Locals: 2\n" + |
5271 |
" B(Value param);\n" + |
5272 |
" 0 aload_0 [this]\n" + |
5273 |
" 1 invokespecial java.lang.Object() [27]\n" + |
5274 |
" 4 aload_0 [this]\n" + |
5275 |
" 5 ldc <String \"[B.local_field]\"> [29]\n" + |
5276 |
" 7 invokestatic A.foo(java.lang.String) : Value [15]\n" + |
5277 |
" 10 putfield B.local_field : Value [31]\n" + |
5278 |
" 13 getstatic I.CONST : Value [33]\n" + |
5279 |
" 16 pop\n" + |
5280 |
" 17 getstatic Value.NAME : java.lang.String [36]\n" + |
5281 |
" 20 pop\n" + |
5282 |
" 21 getstatic Value.NAME : java.lang.String [36]\n" + |
5283 |
" 24 pop\n" + |
5284 |
" 25 getstatic Value.NAME : java.lang.String [36]\n" + |
5285 |
" 28 pop\n" + |
5286 |
" 29 getstatic Value.NAME : java.lang.String [36]\n" + |
5287 |
" 32 pop\n" + |
5288 |
" 33 return\n" + |
5289 |
" Line numbers:\n" + |
5290 |
" [pc: 0, line: 20]\n" + |
5291 |
" [pc: 4, line: 19]\n" + |
5292 |
" [pc: 13, line: 21]\n" + |
5293 |
" [pc: 21, line: 22]\n" + |
5294 |
" [pc: 25, line: 23]\n" + |
5295 |
" [pc: 29, line: 24]\n" + |
5296 |
" [pc: 33, line: 25]\n" + |
5297 |
" Local variable table:\n" + |
5298 |
" [pc: 0, pc: 34] local: this index: 0 type: B\n" + |
5299 |
" [pc: 0, pc: 34] local: param index: 1 type: Value\n"; |
5300 |
|
5301 |
try { |
5302 |
File f = new File(OUTPUT_DIR + File.separator + "B.class"); |
5303 |
byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f); |
5304 |
ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); |
5305 |
String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED); |
5306 |
int index = result.indexOf(expectedOutput); |
5307 |
if (index == -1 || expectedOutput.length() == 0) { |
5308 |
System.out.println(Util.displayString(result, 3)); |
5309 |
} |
5310 |
if (index == -1) { |
5311 |
assertEquals("Wrong contents", expectedOutput, result); |
5312 |
} |
5313 |
} catch (org.eclipse.jdt.core.util.ClassFormatException e) { |
5314 |
assertTrue(false); |
5315 |
} catch (IOException e) { |
5316 |
assertTrue(false); |
5317 |
} |
5318 |
} |
5319 |
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=159021 - variation |
5320 |
public void test191() { |
5321 |
Map options = getCompilerOptions(); |
5322 |
options.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.OPTIMIZE_OUT); |
5323 |
this.runConformTest( |
5324 |
new String[] { |
5325 |
"X.java", |
5326 |
"interface I {\n" + |
5327 |
" Value CONST = A.foo(\"[I.CONST]\");\n" + |
5328 |
"}\n" + |
5329 |
"class Value {\n" + |
5330 |
" static String NAME = \"\";\n" + |
5331 |
" String v;\n" + |
5332 |
" Value(String v) {\n" + |
5333 |
" this.v = v;\n" + |
5334 |
" }\n" + |
5335 |
"}\n" + |
5336 |
"class A {\n" + |
5337 |
" static Value foo(String str) {\n" + |
5338 |
" System.out.print(str);\n" + |
5339 |
" return new Value(\"str\");\n" + |
5340 |
" }\n" + |
5341 |
"}\n" + |
5342 |
"class B implements I {\n" + |
5343 |
" static Value LOCAL_STATIC = A.foo(\"[B.LOCAL_STATIC]\");\n" + |
5344 |
" Value local_field = A.foo(\"[B.local_field]\");\n" + |
5345 |
" B(Value param) {\n" + |
5346 |
" String i = this.CONST.NAME; // keep for possible <clinit>\n" + |
5347 |
" String k = this.LOCAL_STATIC.NAME; // may optimize out\n" + |
5348 |
" String l = this.local_field.NAME; // may optimize out\n" + |
5349 |
" }\n" + |
5350 |
"}\n" + |
5351 |
"public class X {\n" + |
5352 |
" public static void main(String[] args) {\n" + |
5353 |
" new B(new Value(\"[PARAM]\"));\n" + |
5354 |
" }\n" + |
5355 |
"}", // ================= |
5356 |
}, |
5357 |
"[B.LOCAL_STATIC][B.local_field][I.CONST]", |
5358 |
null, |
5359 |
false, |
5360 |
null, |
5361 |
options, |
5362 |
null); |
5363 |
// check the reference to I.CONST still got generated (for <clinit> invocation side-effect) |
5364 |
String expectedOutput = new CompilerOptions(options).complianceLevel < ClassFileConstants.JDK1_4 |
5365 |
? " // Method descriptor #26 (LValue;)V\n" + |
5366 |
" // Stack: 2, Locals: 2\n" + |
5367 |
" B(Value param);\n" + |
5368 |
" 0 aload_0 [this]\n" + |
5369 |
" 1 invokespecial java.lang.Object() [27]\n" + |
5370 |
" 4 aload_0 [this]\n" + |
5371 |
" 5 ldc <String \"[B.local_field]\"> [29]\n" + |
5372 |
" 7 invokestatic A.foo(java.lang.String) : Value [15]\n" + |
5373 |
" 10 putfield B.local_field : Value [31]\n" + |
5374 |
" 13 getstatic I.CONST : Value [33]\n" + |
5375 |
" 16 pop\n" + |
5376 |
" 17 return\n" + |
5377 |
" Line numbers:\n" + |
5378 |
" [pc: 0, line: 20]\n" + |
5379 |
" [pc: 4, line: 19]\n" + |
5380 |
" [pc: 13, line: 21]\n" + |
5381 |
" [pc: 17, line: 24]\n" + |
5382 |
" Local variable table:\n" + |
5383 |
" [pc: 0, pc: 18] local: this index: 0 type: B\n" + |
5384 |
" [pc: 0, pc: 18] local: param index: 1 type: Value\n" |
5385 |
: " // Method descriptor #26 (LValue;)V\n" + |
5386 |
" // Stack: 2, Locals: 2\n" + |
5387 |
" B(Value param);\n" + |
5388 |
" 0 aload_0 [this]\n" + |
5389 |
" 1 invokespecial java.lang.Object() [27]\n" + |
5390 |
" 4 aload_0 [this]\n" + |
5391 |
" 5 ldc <String \"[B.local_field]\"> [29]\n" + |
5392 |
" 7 invokestatic A.foo(java.lang.String) : Value [15]\n" + |
5393 |
" 10 putfield B.local_field : Value [31]\n" + |
5394 |
" 13 getstatic B.CONST : Value [33]\n" + |
5395 |
" 16 pop\n" + |
5396 |
" 17 getstatic Value.NAME : java.lang.String [36]\n" + |
5397 |
" 20 pop\n" + |
5398 |
" 21 getstatic Value.NAME : java.lang.String [36]\n" + |
5399 |
" 24 pop\n" + |
5400 |
" 25 getstatic Value.NAME : java.lang.String [36]\n" + |
5401 |
" 28 pop\n" + |
5402 |
" 29 return\n" + |
5403 |
" Line numbers:\n" + |
5404 |
" [pc: 0, line: 20]\n" + |
5405 |
" [pc: 4, line: 19]\n" + |
5406 |
" [pc: 13, line: 21]\n" + |
5407 |
" [pc: 21, line: 22]\n" + |
5408 |
" [pc: 25, line: 23]\n" + |
5409 |
" [pc: 29, line: 24]\n" + |
5410 |
" Local variable table:\n" + |
5411 |
" [pc: 0, pc: 30] local: this index: 0 type: B\n" + |
5412 |
" [pc: 0, pc: 30] local: param index: 1 type: Value\n"; |
5413 |
|
5414 |
try { |
5415 |
File f = new File(OUTPUT_DIR + File.separator + "B.class"); |
5416 |
byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f); |
5417 |
ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); |
5418 |
String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED); |
5419 |
int index = result.indexOf(expectedOutput); |
5420 |
if (index == -1 || expectedOutput.length() == 0) { |
5421 |
System.out.println(Util.displayString(result, 3)); |
5422 |
} |
5423 |
if (index == -1) { |
5424 |
assertEquals("Wrong contents", expectedOutput, result); |
5425 |
} |
5426 |
} catch (org.eclipse.jdt.core.util.ClassFormatException e) { |
5427 |
assertTrue(false); |
5428 |
} catch (IOException e) { |
5429 |
assertTrue(false); |
5430 |
} |
5104 |
} |
5431 |
} |
5105 |
public static Class testClass() { |
5432 |
public static Class testClass() { |
5106 |
return InitializationTest.class; |
5433 |
return InitializationTest.class; |