diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java index cef41ac..c7c9427 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CastTest.java @@ -2374,6 +2374,54 @@ "SUCCESS" ); } +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=359284 +// Verify that checkcast is emitted for a cast expression. +public void test061b() throws Exception { + String source = + "public class X {\n" + + "public X() {\n" + + " Object[] x = (Object[])null;\n" + + "}\n" + + "}\n"; + this.runConformTest( + new String[] { + "X.java", + source + }, + "" + ); + String expectedOutput = + "public class X {\n" + + " \n" + + " // Method descriptor #6 ()V\n" + + " // Stack: 1, Locals: 2\n" + + " public X();\n" + + " 0 aload_0 [this]\n" + + " 1 invokespecial java.lang.Object() [8]\n" + + " 4 aconst_null\n" + + " 5 checkcast java.lang.Object[] [10]\n" + + " 8 astore_1 [x]\n" + + " 9 return\n" + + " Line numbers:\n" + + " [pc: 0, line: 2]\n" + + " [pc: 4, line: 3]\n" + + " [pc: 9, line: 4]\n" + + " Local variable table:\n" + + " [pc: 0, pc: 10] local: this index: 0 type: X\n" + + " [pc: 9, pc: 10] local: x index: 1 type: java.lang.Object[]\n" + + "}"; + File f = new File(OUTPUT_DIR + File.separator + "X.class"); + byte[] classFileBytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(f); + ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); + String result = disassembler.disassemble(classFileBytes, "\n", ClassFileBytesDisassembler.DETAILED); + int index = result.indexOf(expectedOutput); + if (index == -1 || expectedOutput.length() == 0) { + System.out.println(Util.displayString(result, 3)); + } + if (index == -1) { + assertEquals("Wrong contents", expectedOutput, result); + } +} public static Class testClass() { return CastTest.class; } diff --git a/org.eclipse.jdt.core/buildnotes_jdt-core.html b/org.eclipse.jdt.core/buildnotes_jdt-core.html index 68e92aa..594de69 100644 --- a/org.eclipse.jdt.core/buildnotes_jdt-core.html +++ b/org.eclipse.jdt.core/buildnotes_jdt-core.html @@ -52,7 +52,9 @@

What's new in this drop

Problem Reports Fixed

-361441 +359284 +Unnecessary checkast from null +
361441 Error in JDT Core during AST creation
363293 resource leaks in org.eclipse.jdt.compiler.tool.tests diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java index b11e476..2d0066b 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java @@ -135,9 +135,8 @@ this.initialization.generateCode(currentScope, codeStream, true); // 26903, need extra cast to store null in array local var if (this.binding.type.isArrayType() - && (this.initialization.resolvedType == TypeBinding.NULL // arrayLoc = null - || ((this.initialization instanceof CastExpression) // arrayLoc = (type[])null - && (((CastExpression)this.initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL)))){ + && ((this.initialization instanceof CastExpression) // arrayLoc = (type[])null + && (((CastExpression)this.initialization).innermostCastedExpression().resolvedType == TypeBinding.NULL))){ codeStream.checkcast(this.binding.type); } codeStream.store(this.binding, false); diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java index a2cdd89..d625d5b 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java @@ -352,9 +352,8 @@ } // 26903, need extra cast to store null in array local var if (localBinding.type.isArrayType() - && (assignment.expression.resolvedType == TypeBinding.NULL // arrayLoc = null - || ((assignment.expression instanceof CastExpression) // arrayLoc = (type[])null - && (((CastExpression)assignment.expression).innermostCastedExpression().resolvedType == TypeBinding.NULL)))){ + && ((assignment.expression instanceof CastExpression) // arrayLoc = (type[])null + && (((CastExpression)assignment.expression).innermostCastedExpression().resolvedType == TypeBinding.NULL))){ codeStream.checkcast(localBinding.type); }