DoubleCastTest.java revision 2942:08092deced3f
1/* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8015499 27 * @summary javac, Gen is generating extra checkcast instructions in some corner cases 28 * @modules jdk.jdeps/com.sun.tools.classfile 29 * jdk.compiler/com.sun.tools.javac.util 30 * @run main DoubleCastTest 31 */ 32 33import java.util.List; 34import java.util.ArrayList; 35import com.sun.tools.classfile.*; 36import com.sun.tools.javac.util.Assert; 37 38public class DoubleCastTest { 39 class C { 40 Object x; 41 Object m() { return null; } 42 void m1(byte[] b) {} 43 void m2() { 44 Object o; 45 Object[] os = null; 46 m1((byte[])(o = null)); 47 m1((byte[])o); 48 m1((byte[])(o == null ? o : o)); 49 m1((byte[])m()); 50 m1((byte[])os[0]); 51 m1((byte[])this.x); 52 m1((byte[])((byte []) (o = null))); 53 } 54 } 55 56 public static void main(String... cmdline) throws Exception { 57 58 ClassFile cls = ClassFile.read(DoubleCastTest.class.getResourceAsStream("DoubleCastTest$C.class")); 59 for (Method m: cls.methods) 60 check(m); 61 } 62 63 static void check(Method m) throws Exception { 64 boolean last_is_cast = false; 65 int last_ref = 0; 66 Code_attribute ea = (Code_attribute)m.attributes.get(Attribute.Code); 67 for (Instruction i : ea.getInstructions()) { 68 if (i.getOpcode() == Opcode.CHECKCAST) { 69 Assert.check 70 (!(last_is_cast && last_ref == i.getUnsignedShort(1)), 71 "Double cast found - Test failed"); 72 last_is_cast = true; 73 last_ref = i.getUnsignedShort(1); 74 } else { 75 last_is_cast = false; 76 } 77 } 78 } 79} 80