1 /*
 2  * Copyright (c) 2020, 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * @test
28  * @bug 8244711 8244712
29  * @summary Test that inline types work well with enhanced for loop.
30  * @run main EnhancedForLoopTest
31  */
32 
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.ArrayList;
36 
37 /* This test covers/verifies that the asSuper calls in
38 
39    com.sun.tools.javac.comp.Lower.visitIterableForeachLoop
40    com.sun.tools.javac.comp.Attr#visitForeachLoop
41 
42    work properly with primitive class types.
43 */
44 
45 public class EnhancedForLoopTest {
46 
47     static primitive class PrimitiveIterator<V> implements Iterator<V> {
48 
49         Iterator<V> iv;
50 
51         public PrimitiveIterator(List<V> lv) {
52             this.iv = lv.iterator();
53         }
54 
55         @Override
56         public boolean hasNext() {
57             return iv.hasNext();
58         }
59 
60         @Override
61         public V next() {
62             return iv.next();
63         }
64 
65     }
66 
67     primitive static class Foo<V> implements Iterable<V> {
68 
69         List<V> lv;
70 
71         public Foo() {
72             lv = new ArrayList<>();
73         }
74 
75         public void add(V v) {
76             lv.add(v);
77         }
78 
79        public PrimitiveIterator<V> iterator() {
80             return new PrimitiveIterator<V>(lv);
81         }
82     }
83 
84     public static void main(String[] args) {
85         Foo<String> foo = new Foo<String>();
86         foo.add ("Hello");
87         foo.add (" ");
88         foo.add ("World");
89         foo.add ("!");
90         String output = "";
91         for (var s : foo) {
92             output += s;
93         }
94         if (!output.equals("Hello World!"))
95             throw new AssertionError("Unexpected: " + output);
96     }
97 }