Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 415541
Collapse All | Expand All

(-)a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JSR308SpecSnippetTests.java (-14 / +38 lines)
Lines 11-17 Link Here
11
 * 
11
 * 
12
 * Contributors:
12
 * Contributors:
13
 *     IBM Corporation - initial API and implementation
13
 *     IBM Corporation - initial API and implementation
14
 * 
14
 *        Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contributions for
15
 *                          Bug 415541 - [1.8][compiler] Type annotations in the body of static initializer get dropped
15
 *******************************************************************************/
16
 *******************************************************************************/
16
package org.eclipse.jdt.core.tests.compiler.regression;
17
package org.eclipse.jdt.core.tests.compiler.regression;
17
18
Lines 1380-1386 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1380
		checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput, ClassFileBytesDisassembler.SYSTEM);
1381
		checkDisassembledClassFile(OUTPUT_DIR + File.separator + "X.class", "X", expectedOutput, ClassFileBytesDisassembler.SYSTEM);
1381
	}
1382
	}
1382
	// Test type annotations in initializer code.
1383
	// Test type annotations in initializer code.
1383
	public void test033() throws Exception { // WILL NEED TO BE ADJUSTED ONCE https://bugs.eclipse.org/bugs/show_bug.cgi?id=415541 IS FIXED.
1384
	public void test033() throws Exception {
1384
		this.runConformTest(
1385
		this.runConformTest(
1385
			new String[] {
1386
			new String[] {
1386
				"X.java",
1387
				"X.java",
Lines 1401-1406 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1401
				"}\n",
1402
				"}\n",
1402
		},
1403
		},
1403
		"");
1404
		"");
1405
		// javac b100
1406
		// For the annotations in the static {...} the clinit has:
1407
		//		RuntimeInvisibleTypeAnnotations:
1408
		//	        0: #11(): CAST, offset=0, type_index=0
1409
		//	        1: #11(): NEW, offset=0
1410
		// javac is skipping production of the cast so offset is 0. JDT is currently always producing the
1411
		// checkcast for an annotated cast so the offset is 7.
1412
		
1413
		// For the annotations in the initializer {...} the constructors both have:
1414
		//	      RuntimeInvisibleTypeAnnotations:
1415
		//	          0: #11(): NEW, offset=4
1416
1404
		String expectedOutput =
1417
		String expectedOutput =
1405
				"// Compiled from X.java (version 1.8 : 52.0, super bit)\n" + 
1418
				"// Compiled from X.java (version 1.8 : 52.0, super bit)\n" + 
1406
				"class X {\n" + 
1419
				"class X {\n" + 
Lines 1417-1426 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1417
				"    constant #10 utf8: \"<init>\"\n" + 
1430
				"    constant #10 utf8: \"<init>\"\n" + 
1418
				"    constant #11 utf8: \"LineNumberTable\"\n" + 
1431
				"    constant #11 utf8: \"LineNumberTable\"\n" + 
1419
				"    constant #12 utf8: \"LocalVariableTable\"\n" + 
1432
				"    constant #12 utf8: \"LocalVariableTable\"\n" + 
1420
				"    constant #13 utf8: \"this\"\n" + 
1433
				"    constant #13 utf8: \"RuntimeInvisibleTypeAnnotations\"\n" + 
1421
				"    constant #14 utf8: \"LX;\"\n" + 
1434
				"    constant #14 utf8: \"LNonNull;\"\n" + 
1422
				"    constant #15 utf8: \"RuntimeInvisibleTypeAnnotations\"\n" + 
1435
				"    constant #15 utf8: \"this\"\n" + 
1423
				"    constant #16 utf8: \"LNonNull;\"\n" + 
1436
				"    constant #16 utf8: \"LX;\"\n" + 
1424
				"    constant #17 utf8: \"(I)V\"\n" + 
1437
				"    constant #17 utf8: \"(I)V\"\n" + 
1425
				"    constant #18 utf8: \"x\"\n" + 
1438
				"    constant #18 utf8: \"x\"\n" + 
1426
				"    constant #19 utf8: \"I\"\n" + 
1439
				"    constant #19 utf8: \"I\"\n" + 
Lines 1430-1443 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1430
				"  // Method descriptor #6 ()V\n" + 
1443
				"  // Method descriptor #6 ()V\n" + 
1431
				"  // Stack: 2, Locals: 1\n" + 
1444
				"  // Stack: 2, Locals: 1\n" + 
1432
				"  static {};\n" + 
1445
				"  static {};\n" + 
1433
				"    0  new java.lang.Object [3]\n" + 
1446
				"     0  new java.lang.Object [3]\n" + 
1434
				"    3  dup\n" + 
1447
				"     3  dup\n" + 
1435
				"    4  invokespecial java.lang.Object() [8]\n" + 
1448
				"     4  invokespecial java.lang.Object() [8]\n" + 
1436
				"    7  astore_0\n" + 
1449
				"     7  checkcast java.lang.Object [3]\n" + 
1437
				"    8  return\n" + 
1450
				"    10  astore_0\n" + 
1451
				"    11  return\n" + 
1438
				"      Line numbers:\n" + 
1452
				"      Line numbers:\n" + 
1439
				"        [pc: 0, line: 6]\n" + 
1453
				"        [pc: 0, line: 6]\n" + 
1440
				"        [pc: 8, line: 7]\n" + 
1454
				"        [pc: 11, line: 7]\n" + 
1455
				"    RuntimeInvisibleTypeAnnotations: \n" + 
1456
				"      #14 @NonNull(\n" + 
1457
				"        target type = 0x44 NEW\n" + 
1458
				"        offset = 0\n" + 
1459
				"      )\n" + 
1460
				"      #14 @NonNull(\n" + 
1461
				"        target type = 0x47 CAST\n" + 
1462
				"        offset = 7\n" + 
1463
				"        type argument index = 0\n" + 
1464
				"      )\n" + 
1441
				"  \n" + 
1465
				"  \n" + 
1442
				"  // Method descriptor #6 ()V\n" + 
1466
				"  // Method descriptor #6 ()V\n" + 
1443
				"  // Stack: 1, Locals: 1\n" + 
1467
				"  // Stack: 1, Locals: 1\n" + 
Lines 1454-1460 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1454
				"      Local variable table:\n" + 
1478
				"      Local variable table:\n" + 
1455
				"        [pc: 0, pc: 11] local: this index: 0 type: X\n" + 
1479
				"        [pc: 0, pc: 11] local: this index: 0 type: X\n" + 
1456
				"    RuntimeInvisibleTypeAnnotations: \n" + 
1480
				"    RuntimeInvisibleTypeAnnotations: \n" + 
1457
				"      #16 @NonNull(\n" + 
1481
				"      #14 @NonNull(\n" + 
1458
				"        target type = 0x44 NEW\n" + 
1482
				"        target type = 0x44 NEW\n" + 
1459
				"        offset = 4\n" + 
1483
				"        offset = 4\n" + 
1460
				"      )\n" + 
1484
				"      )\n" + 
Lines 1475-1481 public class JSR308SpecSnippetTests extends AbstractRegressionTest { Link Here
1475
				"        [pc: 0, pc: 11] local: this index: 0 type: X\n" + 
1499
				"        [pc: 0, pc: 11] local: this index: 0 type: X\n" + 
1476
				"        [pc: 0, pc: 11] local: x index: 1 type: int\n" + 
1500
				"        [pc: 0, pc: 11] local: x index: 1 type: int\n" + 
1477
				"    RuntimeInvisibleTypeAnnotations: \n" + 
1501
				"    RuntimeInvisibleTypeAnnotations: \n" + 
1478
				"      #16 @NonNull(\n" + 
1502
				"      #14 @NonNull(\n" + 
1479
				"        target type = 0x44 NEW\n" + 
1503
				"        target type = 0x44 NEW\n" + 
1480
				"        offset = 4\n" + 
1504
				"        offset = 4\n" + 
1481
				"      )\n" + 
1505
				"      )\n" + 
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ClassFile.java (+9 lines)
Lines 17-22 Link Here
17
 *                          Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work)
17
 *                          Bug 383624 - [1.8][compiler] Revive code generation support for type annotations (from Olivier's work)
18
 *                          Bug 409236 - [1.8][compiler] Type annotations on intersection cast types dropped by code generator
18
 *                          Bug 409236 - [1.8][compiler] Type annotations on intersection cast types dropped by code generator
19
 *                          Bug 409246 - [1.8][compiler] Type annotations on catch parameters not handled properly
19
 *                          Bug 409246 - [1.8][compiler] Type annotations on catch parameters not handled properly
20
 *                          Bug 415541 - [1.8][compiler] Type annotations in the body of static initializer get dropped
20
 *******************************************************************************/
21
 *******************************************************************************/
21
package org.eclipse.jdt.internal.compiler;
22
package org.eclipse.jdt.internal.compiler;
22
23
Lines 1562-1567 public class ClassFile implements TypeConstants, TypeIds { Link Here
1562
					true);
1563
					true);
1563
		}
1564
		}
1564
1565
1566
		if ((this.produceAttributes & ClassFileConstants.ATTR_TYPE_ANNOTATION) != 0) {
1567
			attributesNumber += generateTypeAnnotationsOnCodeAttribute();
1568
		}
1569
1565
		// update the number of attributes
1570
		// update the number of attributes
1566
		// ensure first that there is enough space available inside the contents array
1571
		// ensure first that there is enough space available inside the contents array
1567
		if (codeAttributeAttributeOffset + 2 >= this.contents.length) {
1572
		if (codeAttributeAttributeOffset + 2 >= this.contents.length) {
Lines 1672-1677 public class ClassFile implements TypeConstants, TypeIds { Link Here
1672
					true);
1677
					true);
1673
		}
1678
		}
1674
1679
1680
		if ((this.produceAttributes & ClassFileConstants.ATTR_TYPE_ANNOTATION) != 0) {
1681
			attributesNumber += generateTypeAnnotationsOnCodeAttribute();
1682
		}
1683
		
1675
		// update the number of attributes
1684
		// update the number of attributes
1676
		// ensure first that there is enough space available inside the contents array
1685
		// ensure first that there is enough space available inside the contents array
1677
		if (codeAttributeAttributeOffset + 2 >= this.contents.length) {
1686
		if (codeAttributeAttributeOffset + 2 >= this.contents.length) {
(-)a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java (-6 / +7 lines)
Lines 19-24 Link Here
19
 *								bug 383368 - [compiler][null] syntactic null analysis for field references
19
 *								bug 383368 - [compiler][null] syntactic null analysis for field references
20
 *								bug 401017 - [compiler][null] casted reference to @Nullable field lacks a warning
20
 *								bug 401017 - [compiler][null] casted reference to @Nullable field lacks a warning
21
 *								bug 400761 - [compiler][null] null may be return as boolean without a diagnostic
21
 *								bug 400761 - [compiler][null] null may be return as boolean without a diagnostic
22
 *        Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contributions for
23
 *                          Bug 415541 - [1.8][compiler] Type annotations in the body of static initializer get dropped
22
 *******************************************************************************/
24
 *******************************************************************************/
23
package org.eclipse.jdt.internal.compiler.ast;
25
package org.eclipse.jdt.internal.compiler.ast;
24
26
Lines 51-64 public class CastExpression extends Expression { Link Here
51
	public Expression expression;
53
	public Expression expression;
52
	public TypeReference type;
54
	public TypeReference type;
53
	public TypeBinding expectedType; // when assignment conversion to a given expected type: String s = (String) t;
55
	public TypeBinding expectedType; // when assignment conversion to a given expected type: String s = (String) t;
56
	public boolean annotatedCast;
54
57
55
//expression.implicitConversion holds the cast for baseType casting
58
//expression.implicitConversion holds the cast for baseType casting
56
public CastExpression(Expression expression, TypeReference type) {
59
public CastExpression(Expression expression, TypeReference type) {
57
	this.expression = expression;
60
	this.expression = expression;
58
	this.type = type;
61
	this.type = type;
59
	type.bits |= ASTNode.IgnoreRawTypeCheck; // no need to worry about raw type usage
62
	type.bits |= ASTNode.IgnoreRawTypeCheck; // no need to worry about raw type usage
60
	if ((this.type.bits & ASTNode.HasTypeAnnotations) != 0)
63
	this.annotatedCast = (this.type.bits & ASTNode.HasTypeAnnotations) != 0;
61
		this.bits |= ASTNode.GenerateCheckcast;
62
}
64
}
63
65
64
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
66
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
Lines 424-432 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean Link Here
424
	int pc = codeStream.position;
426
	int pc = codeStream.position;
425
	boolean needRuntimeCheckcast = (this.bits & ASTNode.GenerateCheckcast) != 0;
427
	boolean needRuntimeCheckcast = (this.bits & ASTNode.GenerateCheckcast) != 0;
426
	if (this.constant != Constant.NotAConstant) {
428
	if (this.constant != Constant.NotAConstant) {
427
		if (valueRequired || needRuntimeCheckcast) { // Added for: 1F1W9IG: IVJCOM:WINNT - Compiler omits casting check
429
		if (valueRequired || needRuntimeCheckcast || this.annotatedCast) { // Added for: 1F1W9IG: IVJCOM:WINNT - Compiler omits casting check
428
			codeStream.generateConstant(this.constant, this.implicitConversion);
430
			codeStream.generateConstant(this.constant, this.implicitConversion);
429
			if (needRuntimeCheckcast) {
431
			if (needRuntimeCheckcast || this.annotatedCast) {
430
				codeStream.checkcast(this.type, this.resolvedType);
432
				codeStream.checkcast(this.type, this.resolvedType);
431
			}
433
			}
432
			if (!valueRequired) {
434
			if (!valueRequired) {
Lines 438-444 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean Link Here
438
		return;
440
		return;
439
	}
441
	}
440
	this.expression.generateCode(currentScope, codeStream, valueRequired || needRuntimeCheckcast);
442
	this.expression.generateCode(currentScope, codeStream, valueRequired || needRuntimeCheckcast);
441
	if (needRuntimeCheckcast && this.expression.postConversionType(currentScope) != this.resolvedType.erasure()) { // no need to issue a checkcast if already done as genericCast
443
	if (this.annotatedCast || (needRuntimeCheckcast && this.expression.postConversionType(currentScope) != this.resolvedType.erasure())) { // no need to issue a checkcast if already done as genericCast
442
		codeStream.checkcast(this.type, this.resolvedType);
444
		codeStream.checkcast(this.type, this.resolvedType);
443
	}
445
	}
444
	if (valueRequired) {
446
	if (valueRequired) {
445
- 

Return to bug 415541