ScopeListenerTest.java revision 2953:aff504edf8a1
1139749Simp/*
2101704Smjacob * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3101704Smjacob * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4101704Smjacob *
5101704Smjacob * This code is free software; you can redistribute it and/or modify it
6101704Smjacob * under the terms of the GNU General Public License version 2 only, as
7101704Smjacob * published by the Free Software Foundation.
8101704Smjacob *
9101704Smjacob * This code is distributed in the hope that it will be useful, but WITHOUT
10101704Smjacob * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11101704Smjacob * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12101704Smjacob * version 2 for more details (a copy is included in the LICENSE file that
13101704Smjacob * accompanied this code).
14101704Smjacob *
15101704Smjacob * You should have received a copy of the GNU General Public License version
16101704Smjacob * 2 along with this work; if not, write to the Free Software Foundation,
17101704Smjacob * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18101704Smjacob *
19101704Smjacob * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20101704Smjacob * or visit www.oracle.com if you need additional information or have any
21101704Smjacob * questions.
22101704Smjacob */
23101704Smjacob
24101704Smjacob/*
25101704Smjacob * @test
26101704Smjacob * @bug 8039262
27147883Sscottl * @summary Ensure that using Types.membersClosure does not increase the number of listeners on the
28101704Smjacob *          class's members Scope.
29156000Smjacob */
30156000Smjacob
31156000Smjacobimport com.sun.tools.javac.code.Scope;
32156000Smjacobimport com.sun.tools.javac.code.Symbol;
33156000Smjacobimport com.sun.tools.javac.code.Symtab;
34156000Smjacobimport com.sun.tools.javac.code.Types;
35156000Smjacobimport com.sun.tools.javac.file.JavacFileManager;
36156000Smjacobimport com.sun.tools.javac.util.Context;
37156000Smjacobimport com.sun.tools.javac.util.Names;
38156000Smjacobimport java.lang.reflect.Field;
39156000Smjacobimport java.util.Collection;
40156000Smjacob
41156000Smjacobpublic class ScopeListenerTest {
42156000Smjacob
43156000Smjacob    public static void main(String[] args) throws Exception {
44156000Smjacob        new ScopeListenerTest().run();
45156000Smjacob    }
46156000Smjacob
47156000Smjacob    void run() throws Exception {
48156000Smjacob        Context context = new Context();
49156000Smjacob        JavacFileManager.preRegister(context);
50156000Smjacob        Types types = Types.instance(context);
51156000Smjacob        Symtab syms = Symtab.instance(context);
52156000Smjacob        Names names = Names.instance(context);
53156000Smjacob        types.membersClosure(syms.stringType, true);
54156000Smjacob        types.membersClosure(syms.stringType, false);
55156000Smjacob
56156000Smjacob        Field listenersField = Scope.class.getDeclaredField("listeners");
57156000Smjacob
58156000Smjacob        listenersField.setAccessible(true);
59156000Smjacob
60156000Smjacob        int listenerCount =
61159052Smjacob                ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
62159052Smjacob
63159052Smjacob        for (int i = 0; i < 100; i++) {
64156000Smjacob            types.membersClosure(syms.stringType, true);
65101704Smjacob            types.membersClosure(syms.stringType, false);
66134123Sobrien        }
67134123Sobrien
68134123Sobrien        int newListenerCount
69147883Sscottl                = ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
70147883Sscottl
71147883Sscottl        if (listenerCount != newListenerCount) {
72147883Sscottl            throw new AssertionError("Orig listener count: " + listenerCount +
73147883Sscottl                                     "; new listener count: " + newListenerCount);
74157117Smjacob        }
75147883Sscottl
76147883Sscottl        for (Symbol s : types.membersClosure(syms.stringType, true).getSymbols())
77147883Sscottl            ;
78103914Smjacob        for (Symbol s : types.membersClosure(syms.stringType, false).getSymbolsByName(names.fromString("substring")))
79101704Smjacob            ;
80101704Smjacob    }
81101704Smjacob
82101704Smjacob}
83101704Smjacob