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

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/ArrayTest.java (+186 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.compiler.regression;
11
package org.eclipse.jdt.core.tests.compiler.regression;
12
import java.io.File;
12
import java.io.File;
13
import java.util.Map;
13
14
14
import junit.framework.Test;
15
import junit.framework.Test;
15
16
Lines 393-396 Link Here
393
		"argument cannot be resolved\n" +
394
		"argument cannot be resolved\n" +
394
		"----------\n");
395
		"----------\n");
395
}
396
}
397
// Check return type of array#clone()
398
public void test014() throws Exception {
399
	Map optionsMap = getCompilerOptions();
400
	CompilerOptions options = new CompilerOptions(optionsMap);
401
	if (options.complianceLevel <= ClassFileConstants.JDK1_4) {
402
		this.runNegativeTest(
403
				new String[] {
404
					"X.java",
405
					"public class X {\n" + 
406
					"	void foo(long[] longs) throws Exception {\n" + 
407
					"		long[] other = longs.clone();\n" + 
408
					"	}\n" + 
409
					"}\n",
410
				},
411
				"----------\n" + 
412
				"1. ERROR in X.java (at line 3)\n" + 
413
				"	long[] other = longs.clone();\n" + 
414
				"	               ^^^^^^^^^^^^^\n" + 
415
				"Type mismatch: cannot convert from Object to long[]\n" + 
416
				"----------\n");
417
		return;
418
	}
419
	// check that #clone() return type is changed only from -source 1.5 only (independant from compliance level)
420
	optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
421
	this.runNegativeTest(
422
			new String[] {
423
				"X.java",
424
				"public class X {\n" + 
425
				"	void foo(long[] longs) throws Exception {\n" + 
426
				"		long[] other = longs.clone();\n" + 
427
				"	}\n" + 
428
				"}\n",
429
			},
430
			"----------\n" + 
431
			"1. ERROR in X.java (at line 3)\n" + 
432
			"	long[] other = longs.clone();\n" + 
433
			"	               ^^^^^^^^^^^^^\n" + 
434
			"Type mismatch: cannot convert from Object to long[]\n" + 
435
			"----------\n",
436
			null,
437
			true,
438
			optionsMap);
439
440
	// try again with default source level >=1.5
441
	this.runConformTest(
442
		new String[] {
443
			"X.java",
444
			"public class X {\n" + 
445
			"	void foo(long[] longs) throws Exception {\n" + 
446
			"		long[] other = longs.clone();\n" + 
447
			"	}\n" + 
448
			"}\n",
449
		},
450
		"");
451
452
	ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
453
	byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(new File(OUTPUT_DIR + File.separator  +"X.class"));
454
	String actualOutput =
455
		disassembler.disassemble(
456
			classFileBytes,
457
			"\n",
458
			ClassFileBytesDisassembler.DETAILED);
459
460
	String expectedOutput =
461
		"  static {};\n" +
462
		"    0  iconst_2\n" +
463
		"    1  anewarray java.lang.Object [3]\n" +
464
		"    4  putstatic X.X : java.lang.Object[] [9]\n" +
465
		"    7  return\n" +
466
		"      Line numbers:\n" +
467
		"        [pc: 0, line: 2]\n" +
468
		"        [pc: 7, line: 1]\n";
469
470
	int index = actualOutput.indexOf(expectedOutput);
471
	if (index == -1 || expectedOutput.length() == 0) {
472
		System.out.println(Util.displayString(actualOutput, 3));
473
	}
474
	if (index == -1) {
475
		assertEquals("unexpected bytecode sequence", expectedOutput, actualOutput);
476
	}
477
}
478
//Check constant pool declaring class of array#clone()
479
public void test016() throws Exception {
480
	this.runConformTest(
481
			new String[] {
482
				"X.java",
483
				"public class X {\n" + 
484
				"	void foo(long[] longs) throws Exception {\n" + 
485
				"		Object other = longs.clone();\n" + 
486
				"	}\n" + 
487
				"}\n",
488
			},
489
			"");
490
	ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
491
	byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(new File(OUTPUT_DIR + File.separator  +"X.class"));
492
	String actualOutput =
493
		disassembler.disassemble(
494
			classFileBytes,
495
			"\n",
496
			ClassFileBytesDisassembler.DETAILED);
497
498
	String expectedOutput =	new CompilerOptions(getCompilerOptions()).sourceLevel <= ClassFileConstants.JDK1_4
499
		?	"  // Method descriptor #15 ([J)V\n" + 
500
			"  // Stack: 1, Locals: 3\n" + 
501
			"  void foo(long[] longs) throws java.lang.Exception;\n" + 
502
			"    0  aload_1 [longs]\n" + 
503
			"    1  invokevirtual java.lang.Object.clone() : java.lang.Object [19]\n" + 
504
			"    4  astore_2 [other]\n" + 
505
			"    5  return\n" + 
506
			"      Line numbers:\n" + 
507
			"        [pc: 0, line: 3]\n" + 
508
			"        [pc: 5, line: 4]\n"
509
		:	"  // Method descriptor #15 ([J)V\n" + 
510
			"  // Stack: 1, Locals: 3\n" + 
511
			"  void foo(long[] longs) throws java.lang.Exception;\n" + 
512
			"    0  aload_1 [longs]\n" + 
513
			"    1  invokevirtual long[].clone() : java.lang.Object [19]\n" + 
514
			"    4  astore_2 [other]\n" + 
515
			"    5  return\n" + 
516
			"      Line numbers:\n" + 
517
			"        [pc: 0, line: 3]\n" + 
518
			"        [pc: 5, line: 4]\n";
519
520
	int index = actualOutput.indexOf(expectedOutput);
521
	if (index == -1 || expectedOutput.length() == 0) {
522
		System.out.println(Util.displayString(actualOutput, 3));
523
	}
524
	if (index == -1) {
525
		assertEquals("unexpected bytecode sequence", expectedOutput, actualOutput);
526
	}		
527
	return;
528
}
529
530
531
//Check constant pool declaring class of array#clone()
532
public void test017() throws Exception {
533
	Map optionsMap = getCompilerOptions();
534
	CompilerOptions options = new CompilerOptions(optionsMap);
535
	if (options.complianceLevel >= ClassFileConstants.JDK1_4) {
536
		// check that #clone() return type is changed ONLY from -source 1.5 only (independant from compliance level)
537
		optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);		
538
	}
539
	this.runConformTest(
540
			new String[] {
541
				"X.java",
542
				"public class X {\n" + 
543
				"	void foo(long[] longs) throws Exception {\n" + 
544
				"		Object other = longs.clone();\n" + 
545
				"	}\n" + 
546
				"}\n",
547
			},
548
			"",
549
			null,
550
			true,
551
			null,
552
			optionsMap,
553
			null);
554
	ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
555
	byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(new File(OUTPUT_DIR + File.separator  +"X.class"));
556
	String actualOutput =
557
		disassembler.disassemble(
558
			classFileBytes,
559
			"\n",
560
			ClassFileBytesDisassembler.DETAILED);
561
562
	String expectedOutput =
563
		"  // Method descriptor #15 ([J)V\n" + 
564
		"  // Stack: 1, Locals: 3\n" + 
565
		"  void foo(long[] longs) throws java.lang.Exception;\n" + 
566
		"    0  aload_1 [longs]\n" + 
567
		"    1  invokevirtual java.lang.Object.clone() : java.lang.Object [19]\n" + 
568
		"    4  astore_2 [other]\n" + 
569
		"    5  return\n" + 
570
		"      Line numbers:\n" + 
571
		"        [pc: 0, line: 3]\n" + 
572
		"        [pc: 5, line: 4]\n";
573
574
	int index = actualOutput.indexOf(expectedOutput);
575
	if (index == -1 || expectedOutput.length() == 0) {
576
		System.out.println(Util.displayString(actualOutput, 3));
577
	}
578
	if (index == -1) {
579
		assertEquals("unexpected bytecode sequence", expectedOutput, actualOutput);
580
	}		
581
}
396
}
582
}

Return to bug 247307