langtools/test/tools/javac/scope/IterateAndRemove.java
author jlahoda
Mon, 08 Aug 2016 17:22:44 +0200
changeset 40235 fe6c00618434
child 42407 f3702cff2933
permissions -rw-r--r--
8144733: Iterating over elements of a Scope can return spurious inner class elements Summary: When a Symbol is removed from a Scope while iterating over it, update the iterator as well to reflect the change. Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40235
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     1
/*
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     4
 *
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     8
 *
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    14
 *
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    18
 *
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    21
 * questions.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    22
 */
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    23
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    24
/**
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    25
 * @test
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    26
 * @bug 8144733
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    27
 * @summary Verify that Scope.remove removes the Symbol also from already running iterations.
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.code
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.util
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    30
 */
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    31
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    32
import java.util.ArrayList;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    33
import java.util.Arrays;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    34
import java.util.Collections;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    35
import java.util.Iterator;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    36
import java.util.List;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    37
import java.util.function.Function;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    38
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    39
import com.sun.tools.javac.code.Scope;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    40
import com.sun.tools.javac.code.Scope.WriteableScope;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    41
import com.sun.tools.javac.code.Symbol;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    42
import com.sun.tools.javac.code.Symbol.PackageSymbol;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.util.Context;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    44
import com.sun.tools.javac.util.Name;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    45
import com.sun.tools.javac.util.Names;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    46
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    47
public class IterateAndRemove {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    48
    public static void main(String... args) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    49
        new IterateAndRemove().run();
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    50
    }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    51
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    52
    void run() {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    53
        Context ctx = new Context();
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    54
        Names names = Names.instance(ctx);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    55
        Symbol root = new PackageSymbol(names.empty, null);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    56
        Name one = names.fromString("1");
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    57
        PackageSymbol sym1 = new PackageSymbol(one, new PackageSymbol(names.fromString("a"), root));
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    58
        PackageSymbol sym2 = new PackageSymbol(one, new PackageSymbol(names.fromString("b"), root));
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    59
        PackageSymbol sym3 = new PackageSymbol(one, new PackageSymbol(names.fromString("c"), root));
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    60
        List<Symbol> symbols = Arrays.asList(sym1, sym2, sym3);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    61
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    62
        List<Function<Scope, Iterable<Symbol>>> getters = Arrays.asList(
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    63
                scope -> scope.getSymbols(),
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    64
                scope -> scope.getSymbolsByName(one)
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    65
        );
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    66
        for (Function<Scope, Iterable<Symbol>> scope2Content : getters) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    67
            for (int removeAt : new int[] {0, 1, 2, 3}) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    68
                for (Symbol removeWhat : new Symbol[] {sym1, sym2, sym3}) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    69
                    WriteableScope s = WriteableScope.create(root);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    70
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    71
                    symbols.forEach(s :: enter);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    72
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    73
                    Iterator<Symbol> it = scope2Content.apply(s).iterator();
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    74
                    List<PackageSymbol> actual = new ArrayList<>();
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    75
                    int count = 0;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    76
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    77
                    while (true) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    78
                        if (count++ == removeAt)
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    79
                            s.remove(removeWhat);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    80
                        if (!it.hasNext())
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    81
                            break;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    82
                        actual.add((PackageSymbol) it.next());
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    83
                    }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    84
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    85
                    List<Symbol> copy = new ArrayList<>(symbols);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    86
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    87
                    Collections.reverse(copy);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    88
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    89
                    count = 0;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    90
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    91
                    while (true) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    92
                        if (count == removeAt && copy.indexOf(removeWhat) >= count)
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    93
                            copy.remove(removeWhat);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    94
                        count++;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    95
                        if (count >= copy.size())
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    96
                            break;
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    97
                    }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    98
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
    99
                    if (!copy.equals(actual)) {
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   100
                        throw new AssertionError("differs: actual: " + actual + "; expected: " + copy);
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   101
                    }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   102
                }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   103
            }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   104
        }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   105
    }
fe6c00618434 8144733: Iterating over elements of a Scope can return spurious inner class elements
jlahoda
parents:
diff changeset
   106
}