1 /*
 2  * Copyright (c) 2019, 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 8217875
27  * @summary [lworld] Javac does not allow express casts between value types and their light weight box types
28  * @run main/othervm -Xverify:none IntercastTest
29  */
30 
31 public primitive class IntercastTest {
32 
33     int ARRAY[] = { 10, 20, 30 };
34 
35     static primitive class Tuple {
36         private final int index;
37         private final int element;
38 
39         private Tuple(int index, int element) {
40             this.index = index;
41             this.element = element;
42         }
43     }
44 
45     static primitive class Cursor {
46         private final int[] array;
47         private final int index;
48 
49         private Cursor(int[] array, int index) {
50             this.array = array;
51             this.index = index;
52         }
53 
54         Tuple current() {
55             return new Tuple(index, array[index]);
56         }
57 
58         Cursor.ref next() {
59             if (index + 1 == array.length) {
60                 return null;
61             }
62             return new Cursor(array, index + 1);
63         }
64     }
65 
66     private static Cursor.ref indexedElements(int[] array) {
67         if (array.length == 0) {
68             return null;
69         }
70         return new Cursor(array, 0);
71     }
72 
73     public int sum() {
74         int sum = 0;
75         for (Cursor.ref cursor = indexedElements(ARRAY); cursor != null; cursor = cursor.next()) {
76             Tuple tuple = cursor.current();
77             sum += tuple.index + tuple.element;
78         }
79         return sum;
80     }
81 
82     public static void main(String [] args) {
83         IntercastTest x = new IntercastTest();
84         if (x.sum() != 63 || x.ARRAY.length != 3) {
85             throw new AssertionError("Broken");
86         }
87         IntercastTest.ref xbox = (IntercastTest.ref) x;
88         if (xbox.sum() != 63 || xbox.ARRAY.length != 3) {
89             throw new AssertionError("Broken");
90         }
91         x = (IntercastTest) xbox;
92         if (x.sum() != 63 || x.ARRAY.length != 3) {
93             throw new AssertionError("Broken");
94         }
95     }
96 }