IterateAndRemove.java revision 3563:45241cff9d3a
1/* 2 * Copyright (c) 2016, 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 8144733 27 * @summary Verify that Scope.remove removes the Symbol also from already running iterations. 28 * @modules jdk.compiler/com.sun.tools.javac.code 29 * jdk.compiler/com.sun.tools.javac.util 30 */ 31 32import java.util.ArrayList; 33import java.util.Arrays; 34import java.util.Collections; 35import java.util.Iterator; 36import java.util.List; 37import java.util.function.Function; 38 39import com.sun.tools.javac.code.Scope; 40import com.sun.tools.javac.code.Scope.WriteableScope; 41import com.sun.tools.javac.code.Symbol; 42import com.sun.tools.javac.code.Symbol.PackageSymbol; 43import com.sun.tools.javac.util.Context; 44import com.sun.tools.javac.util.Name; 45import com.sun.tools.javac.util.Names; 46 47public class IterateAndRemove { 48 public static void main(String... args) { 49 new IterateAndRemove().run(); 50 } 51 52 void run() { 53 Context ctx = new Context(); 54 Names names = Names.instance(ctx); 55 Symbol root = new PackageSymbol(names.empty, null); 56 Name one = names.fromString("1"); 57 PackageSymbol sym1 = new PackageSymbol(one, new PackageSymbol(names.fromString("a"), root)); 58 PackageSymbol sym2 = new PackageSymbol(one, new PackageSymbol(names.fromString("b"), root)); 59 PackageSymbol sym3 = new PackageSymbol(one, new PackageSymbol(names.fromString("c"), root)); 60 List<Symbol> symbols = Arrays.asList(sym1, sym2, sym3); 61 62 List<Function<Scope, Iterable<Symbol>>> getters = Arrays.asList( 63 scope -> scope.getSymbols(), 64 scope -> scope.getSymbolsByName(one) 65 ); 66 for (Function<Scope, Iterable<Symbol>> scope2Content : getters) { 67 for (int removeAt : new int[] {0, 1, 2, 3}) { 68 for (Symbol removeWhat : new Symbol[] {sym1, sym2, sym3}) { 69 WriteableScope s = WriteableScope.create(root); 70 71 symbols.forEach(s :: enter); 72 73 Iterator<Symbol> it = scope2Content.apply(s).iterator(); 74 List<PackageSymbol> actual = new ArrayList<>(); 75 int count = 0; 76 77 while (true) { 78 if (count++ == removeAt) 79 s.remove(removeWhat); 80 if (!it.hasNext()) 81 break; 82 actual.add((PackageSymbol) it.next()); 83 } 84 85 List<Symbol> copy = new ArrayList<>(symbols); 86 87 Collections.reverse(copy); 88 89 count = 0; 90 91 while (true) { 92 if (count == removeAt && copy.indexOf(removeWhat) >= count) 93 copy.remove(removeWhat); 94 count++; 95 if (count >= copy.size()) 96 break; 97 } 98 99 if (!copy.equals(actual)) { 100 throw new AssertionError("differs: actual: " + actual + "; expected: " + copy); 101 } 102 } 103 } 104 } 105 } 106}