RemoveSymbolUnitTest.java revision 3170:dc017a37aac5
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 * @modules jdk.compiler/com.sun.tools.javac.code 29 * jdk.compiler/com.sun.tools.javac.file 30 * jdk.compiler/com.sun.tools.javac.util 31 */ 32 33import com.sun.tools.javac.util.*; 34import com.sun.tools.javac.code.*; 35import com.sun.tools.javac.code.Scope.*; 36import com.sun.tools.javac.code.Symbol.*; 37import com.sun.tools.javac.file.JavacFileManager; 38 39public class RemoveSymbolUnitTest { 40 41 Context context; 42 Names names; 43 Symtab symtab; 44 45 public static void main(String... args) throws Exception { 46 new RemoveSymbolUnitTest().run(); 47 } 48 49 public void run() { 50 context = new Context(); 51 JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab 52 names = Names.instance(context); 53 symtab = Symtab.instance(context); 54 55 Name hasNext = names.fromString("hasNext"); 56 ClassSymbol clazz = new ClassSymbol(0, 57 names.fromString("X"), 58 Type.noType, 59 symtab.unnamedPackage); 60 61 VarSymbol v = new VarSymbol(0, hasNext, Type.noType, clazz); 62 MethodSymbol m = new MethodSymbol(0, hasNext, Type.noType, clazz); 63 64 // Try enter and remove in different shuffled combinations. 65 // working with fresh scope each time. 66 WriteableScope cs = WriteableScope.create(clazz); 67 cs.enter(v); 68 cs.enter(m); 69 cs.remove(v); 70 Symbol s = cs.findFirst(hasNext); 71 if (s != m) 72 throw new AssertionError("Wrong symbol"); 73 74 cs = WriteableScope.create(clazz); 75 cs.enter(m); 76 cs.enter(v); 77 cs.remove(v); 78 s = cs.findFirst(hasNext); 79 if (s != m) 80 throw new AssertionError("Wrong symbol"); 81 82 cs = WriteableScope.create(clazz); 83 cs.enter(v); 84 cs.enter(m); 85 cs.remove(m); 86 s = cs.findFirst(hasNext); 87 if (s != v) 88 throw new AssertionError("Wrong symbol"); 89 90 cs = WriteableScope.create(clazz); 91 cs.enter(m); 92 cs.enter(v); 93 cs.remove(m); 94 s = cs.findFirst(hasNext); 95 if (s != v) 96 throw new AssertionError("Wrong symbol"); 97 } 98} 99