ScopeListenerTest.java revision 3019:176472b94f2e
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 8039262
27 * @summary Ensure that using Types.membersClosure does not increase the number of listeners on the
28 *          class's members Scope.
29 */
30
31import com.sun.tools.javac.code.Scope;
32import com.sun.tools.javac.code.Scope.ScopeListenerList;
33import com.sun.tools.javac.code.Symbol;
34import com.sun.tools.javac.code.Symtab;
35import com.sun.tools.javac.code.Types;
36import com.sun.tools.javac.file.JavacFileManager;
37import com.sun.tools.javac.util.Context;
38import com.sun.tools.javac.util.Names;
39import java.lang.reflect.Field;
40import java.util.Collection;
41
42public class ScopeListenerTest {
43
44    public static void main(String[] args) throws Exception {
45        new ScopeListenerTest().run();
46    }
47
48    void run() throws Exception {
49        Context context = new Context();
50        JavacFileManager.preRegister(context);
51        Types types = Types.instance(context);
52        Symtab syms = Symtab.instance(context);
53        Names names = Names.instance(context);
54        types.membersClosure(syms.stringType, true);
55        types.membersClosure(syms.stringType, false);
56
57        int listenerCount = listenerCount(syms.stringType.tsym.members());
58
59        for (int i = 0; i < 100; i++) {
60            types.membersClosure(syms.stringType, true);
61            types.membersClosure(syms.stringType, false);
62        }
63
64        int newListenerCount = listenerCount(syms.stringType.tsym.members());
65
66        if (listenerCount != newListenerCount) {
67            throw new AssertionError("Orig listener count: " + listenerCount +
68                                     "; new listener count: " + newListenerCount);
69        }
70
71        for (Symbol s : types.membersClosure(syms.stringType, true).getSymbols())
72            ;
73        for (Symbol s : types.membersClosure(syms.stringType, false).getSymbolsByName(names.fromString("substring")))
74            ;
75    }
76
77    int listenerCount(Scope s) throws ReflectiveOperationException {
78        Field listenersListField = Scope.class.getDeclaredField("listeners");
79        listenersListField.setAccessible(true);
80        Field listenersField = ScopeListenerList.class.getDeclaredField("listeners");
81        listenersField.setAccessible(true);
82        return ((Collection<?>)listenersField.get(listenersListField.get(s))).size();
83    }
84
85}
86