1 /*
2  * Copyright (c) 2003, 2004, 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 4921543 5009601
27  * @summary boxing/unboxing versus foreach crashes javac
28  * @author gafter
29  *
30  * @compile BoxedForeach.java
31  * @run main BoxedForeach
32  */
33 
34 import java.util.*;
35 
36 public class BoxedForeach {
f(Integer[] a)37     static void f(Integer[] a) {
38         for ( int i : a ) {
39             System.out.println(i);
40         }
41     }
f(Iterable<Integer> b)42     static void f(Iterable<Integer> b) {
43         g(b);
44         h(b);
45         for ( int i : b ) {
46             System.out.println(i);
47         }
48         for ( float i : b ) {
49             System.out.println(i);
50         }
51         for ( Iterator<Integer> it = b.iterator(); it.hasNext(); ) {
52             float i = it.next();
53         }
54     }
g(Iterable<T> b)55     static <T extends Integer> void g(Iterable<T> b) {
56         for ( int i : b ) {
57             System.out.println(i);
58         }
59     }
h(Iterable<? extends Integer> b)60     static void h(Iterable<? extends Integer> b) {
61         for ( int i : b ) {
62             System.out.println(i);
63         }
64     }
f(int[] c)65     static void f(int[] c) {
66         for ( Integer i : c ) {
67             System.out.println(i);
68         }
69     }
f(E[] values)70     static <E extends Enum<E>> void f(E[] values) {
71         for ( E e : values ) {
72             System.out.println(e);
73         }
74     }
f()75     static int f() {
76         for ( Foo f : foolist() ) {
77             return f.x;
78         }
79         return 0;
80     }
foolist()81     static List<Foo> foolist() {
82         List<Foo> l = new ArrayList<Foo>();
83         l.add(new Foo());
84         return l;
85     }
86     enum color { red, green, blue };
main(String[] args)87     public static void main(String[] args) {
88         Integer[] a1 = { Integer.valueOf(1), Integer.valueOf(2) };
89         f(a1);
90         f(Arrays.asList(a1));
91         int[] a2 = { 1, 2, 3 };
92         f(a2);
93         f(color.values());
94         f();
95     }
96 }
97 
98 class Foo {
99     static int n = 0;
100     final int x;
Foo()101     Foo() {
102         x = n++;
103     }
104 }
105 
106 class A {
107     class B {
108     }
109     java.util.List<String> l;
110     String[] args;
111     {
112         for (String s1 : l) {
113             for (String s2 : l) {
114                 B b = new B();
115             }
116         }
117         for (String s1 : args) {
118             for (String s2 : args) {
119                 B b = new B();
120             }
121         }
122     }
123 }
124