RemoveSymbolTest.java revision 2947:283c9951fd23
1/*
2 * Copyright (c) 2015, 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 8080842
27 * @summary Ensure Scope impl can cope with remove() when a field and method share the name.
28 * @run main RemoveSymbolTest
29 */
30
31import java.util.Iterator;
32import java.util.LinkedList;
33
34public class RemoveSymbolTest<W> implements Iterable<W> {
35    static class Widget {
36        private String name;
37        Widget(String s) { name = s; }
38        @Override public String toString() { return name; }
39    }
40
41    private LinkedList<W> data;
42    // Instantiate an Iterable instance using a Lambda expression.
43    // Causes ClassFormatError if a local variable of type Widget is named after one of the methods.
44    private final Iterable<W> myIterator1 = () -> new Iterator<W>() {
45        private W hasNext = null;
46        private int index = 0;
47        @Override public boolean hasNext() { return index < data.size(); }
48        @Override public W next() { return data.get(index++); }
49    };
50
51    // Instantiate an Iterable instance using an anonymous class.
52    // Always works fine regardless of the name of the local variable.
53    private final Iterable<W> myIterator2 =
54        new Iterable<W>() {
55        @Override
56        public Iterator<W> iterator() {
57            return new Iterator<W>() {
58                private W hasNext = null;
59                private int index = 0;
60                @Override public boolean hasNext() { return index < data.size(); }
61                @Override public W next() { return data.get(index++); }
62            };
63        }
64    };
65    public RemoveSymbolTest() { data = new LinkedList<>(); }
66    public void add(W e) { data.add(e); }
67    @Override public String toString() { return data.toString(); }
68    @Override public Iterator<W> iterator() { return myIterator1.iterator(); }
69    public static void main(String[] args) {
70        RemoveSymbolTest<Widget> widgets = new RemoveSymbolTest<>();
71        widgets.add(new Widget("W1"));
72        widgets.add(new Widget("W2"));
73        widgets.add(new Widget("W3"));
74        System.out.println(".foreach() call: ");
75        widgets.forEach(w -> System.out.println(w + " "));
76    }
77}
78