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