langtools/test/tools/javac/types/ScopeListenerTest.java
changeset 31005 673532e90337
child 32454 b0ac04e0fefe
equal deleted inserted replaced
31004:dc33ef3ba667 31005:673532e90337
       
     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 
       
    31 import com.sun.tools.javac.code.Scope;
       
    32 import com.sun.tools.javac.code.Symbol;
       
    33 import com.sun.tools.javac.code.Symtab;
       
    34 import com.sun.tools.javac.code.Types;
       
    35 import com.sun.tools.javac.file.JavacFileManager;
       
    36 import com.sun.tools.javac.util.Context;
       
    37 import com.sun.tools.javac.util.Names;
       
    38 import java.lang.reflect.Field;
       
    39 import java.util.Collection;
       
    40 
       
    41 public class ScopeListenerTest {
       
    42 
       
    43     public static void main(String[] args) throws Exception {
       
    44         new ScopeListenerTest().run();
       
    45     }
       
    46 
       
    47     void run() throws Exception {
       
    48         Context context = new Context();
       
    49         JavacFileManager.preRegister(context);
       
    50         Types types = Types.instance(context);
       
    51         Symtab syms = Symtab.instance(context);
       
    52         Names names = Names.instance(context);
       
    53         types.membersClosure(syms.stringType, true);
       
    54         types.membersClosure(syms.stringType, false);
       
    55 
       
    56         Field listenersField = Scope.class.getDeclaredField("listeners");
       
    57 
       
    58         listenersField.setAccessible(true);
       
    59 
       
    60         int listenerCount =
       
    61                 ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
       
    62 
       
    63         for (int i = 0; i < 100; i++) {
       
    64             types.membersClosure(syms.stringType, true);
       
    65             types.membersClosure(syms.stringType, false);
       
    66         }
       
    67 
       
    68         int newListenerCount
       
    69                 = ((Collection) listenersField.get(syms.stringType.tsym.members())).size();
       
    70 
       
    71         if (listenerCount != newListenerCount) {
       
    72             throw new AssertionError("Orig listener count: " + listenerCount +
       
    73                                      "; new listener count: " + newListenerCount);
       
    74         }
       
    75 
       
    76         for (Symbol s : types.membersClosure(syms.stringType, true).getSymbols())
       
    77             ;
       
    78         for (Symbol s : types.membersClosure(syms.stringType, false).getSymbolsByName(names.fromString("substring")))
       
    79             ;
       
    80     }
       
    81 
       
    82 }