langtools/test/tools/javac/boxing/BoxedForeach.java
author mikejwre
Wed, 09 Jun 2010 18:56:41 -0700
changeset 5634 895b66935810
parent 5520 86e4b9a9da40
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
     2
 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2985
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 4921543 5009601
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary boxing/unboxing versus foreach crashes javac
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author gafter
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 *
2985
f43e1241e7fb 6843761: Update langtools tests to remove unncessary -source and -target options
darcy
parents: 10
diff changeset
    30
 * @compile BoxedForeach.java
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * @run main BoxedForeach
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
public class BoxedForeach {
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    static void f(Integer[] a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
        for ( int i : a ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    static void f(Iterable<Integer> b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        g(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        h(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        for ( int i : b ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        for ( float i : b ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        for ( Iterator<Integer> it = b.iterator(); it.hasNext(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            float i = it.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    static <T extends Integer> void g(Iterable<T> b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        for ( int i : b ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    static void h(Iterable<? extends Integer> b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        for ( int i : b ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    static void f(int[] c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        for ( Integer i : c ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            System.out.println(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    static <E extends Enum<E>> void f(E[] values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        for ( E e : values ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            System.out.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    static int f() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        for ( Foo f : foolist() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            return f.x;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    static List<Foo> foolist() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        List<Foo> l = new ArrayList<Foo>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        l.add(new Foo());
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return l;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    enum color { red, green, blue };
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public static void main(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        Integer[] a1 = { Integer.valueOf(1), Integer.valueOf(2) };
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        f(a1);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        f(Arrays.asList(a1));
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        int[] a2 = { 1, 2, 3 };
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        f(a2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        f(color.values());
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        f();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
}
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
class Foo {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    static int n = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    final int x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    Foo() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        x = n++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
class A {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    class B {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    java.util.List<String> l;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    String[] args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        for (String s1 : l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            for (String s2 : l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                B b = new B();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        for (String s1 : args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            for (String s2 : args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                B b = new B();
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
}