DoubleCastTest.java revision 2813:2d2baba27992
1180740Sdes/*
2180740Sdes * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3180740Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4180740Sdes *
5180740Sdes * This code is free software; you can redistribute it and/or modify it
6180740Sdes * under the terms of the GNU General Public License version 2 only, as
7204861Sdes * published by the Free Software Foundation.
8218767Sdes *
9204861Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
10180740Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11189006Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12189006Sdes * version 2 for more details (a copy is included in the LICENSE file that
13180740Sdes * accompanied this code).
14180740Sdes *
15180740Sdes * You should have received a copy of the GNU General Public License version
16180740Sdes * 2 along with this work; if not, write to the Free Software Foundation,
17180740Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18214979Sdes *
19214979Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20214979Sdes * or visit www.oracle.com if you need additional information or have any
21180740Sdes * questions.
22214979Sdes */
23214979Sdes
24214979Sdes/*
25180740Sdes * @test
26180740Sdes * @bug 8015499
27180740Sdes * @summary javac, Gen is generating extra checkcast instructions in some corner cases
28180740Sdes * @run main DoubleCastTest
29214979Sdes */
30214979Sdes
31225825Sdesimport java.util.List;
32180740Sdesimport java.util.ArrayList;
33225825Sdesimport com.sun.tools.classfile.*;
34225825Sdesimport com.sun.tools.javac.util.Assert;
35225825Sdes
36225825Sdespublic class DoubleCastTest {
37225825Sdes    class C {
38225825Sdes        Object x;
39180740Sdes        Object m() { return null; }
40180740Sdes        void m1(byte[] b) {}
41204861Sdes        void m2() {
42204861Sdes            Object o;
43204861Sdes            Object[] os = null;
44204861Sdes            m1((byte[])(o = null));
45204861Sdes            m1((byte[])o);
46204861Sdes            m1((byte[])(o == null ? o : o));
47180740Sdes            m1((byte[])m());
48180740Sdes            m1((byte[])os[0]);
49180740Sdes            m1((byte[])this.x);
50180740Sdes            m1((byte[])((byte []) (o = null)));
51180740Sdes        }
52180740Sdes    }
53214979Sdes
54214979Sdes    public static void main(String... cmdline) throws Exception {
55180740Sdes
56180740Sdes        ClassFile cls = ClassFile.read(DoubleCastTest.class.getResourceAsStream("DoubleCastTest$C.class"));
57180740Sdes        for (Method m: cls.methods)
58255670Sdes            check(m);
59255670Sdes    }
60255670Sdes
61255670Sdes    static void check(Method m) throws Exception {
62180740Sdes        boolean last_is_cast = false;
63180740Sdes        int last_ref = 0;
64180740Sdes        Code_attribute ea = (Code_attribute)m.attributes.get(Attribute.Code);
65204861Sdes        for (Instruction i : ea.getInstructions()) {
66204861Sdes            if (i.getOpcode() == Opcode.CHECKCAST) {
67204861Sdes                Assert.check
68204861Sdes                    (!(last_is_cast && last_ref == i.getUnsignedShort(1)),
69204861Sdes                     "Double cast found - Test failed");
70204861Sdes                last_is_cast = true;
71204861Sdes                last_ref = i.getUnsignedShort(1);
72204861Sdes            } else {
73180740Sdes                last_is_cast = false;
74180740Sdes            }
75180740Sdes        }
76180740Sdes    }
77204861Sdes}
78204861Sdes