langtools/test/tools/javac/scope/7017664/CompoundScopeTest.java
author mcimadamore
Tue, 19 Apr 2011 13:57:25 +0100
changeset 9593 f917c044e651
parent 8427 703181b01773
child 16967 79d444669f3f
permissions -rw-r--r--
7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes Summary: CompoundScope.getElements() is not filtering elements according to the ScopeFilter argument Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     1
/*
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     4
 *
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     8
 *
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    14
 *
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    18
 *
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    21
 * questions.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    22
 */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    23
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    24
/*
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    25
 * @test
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
    26
 * @bug 7017664 7036906
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    27
 * @summary Basher for CompoundScopes
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    28
 */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    29
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    30
import java.util.Random;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    31
import java.util.Map;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    32
import java.util.HashMap;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.util.*;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.code.*;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.code.Scope.*;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.code.Symbol.*;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.javac.file.JavacFileManager;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    38
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    39
public class CompoundScopeTest {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    40
    public static void main(String... args) throws Exception {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    41
        new CompoundScopeTest().run(args);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    42
    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    43
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    44
    static final int MAX_SYMBOLS_COUNT = 20;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    45
    static final int PASSES = 10;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    46
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    47
    void run(String... args) throws Exception {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    48
        int count = PASSES;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    49
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    50
        for (int i = 0; i < args.length; i++) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    51
            String arg = args[i];
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    52
            if (arg.equals("-seed") && (i + 1 < args.length))
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    53
                seed = Long.parseLong(args[++i]);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    54
            else if(arg.equals("-tests") && (i + 1 < args.length))
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    55
                count = Integer.parseInt(args[++i]);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    56
            else
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    57
                throw new Exception("unknown arg: " + arg);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    58
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    59
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    60
        rgen = new Random(seed);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    61
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    62
        for (int i = 0; i < count; i++) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    63
            Test t = new Test();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    64
            t.run();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    65
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    66
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    67
        if (errors > 0)
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    68
            throw new Exception(errors + " errors found");
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    69
    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    70
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    71
    /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    72
     * Write a message to stderr.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    73
     */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    74
    void log(String msg) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    75
        System.err.println(msg);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    76
    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    77
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    78
    /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    79
     * Write an error message to stderr.
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    80
     */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    81
    void error(String msg) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    82
        System.err.println("Error: " + msg);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    83
        errors++;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    84
    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    85
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    86
    Random rgen;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    87
    long seed = 0;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    88
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    89
    int errors;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    90
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    91
    /** Class to encapsulate a test run. */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    92
    class Test {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    93
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    94
        List<Symbol> elems = List.nil();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    95
        Map<Name, List<Symbol>> shadowedMap = new HashMap<Name, List<Symbol>>();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    96
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    97
        /** Run the test. */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    98
        void run() throws Exception {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
    99
            log ("starting test");
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   100
            setup();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   101
            Scope[] scopes = { createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)),
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   102
                               createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)),
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   103
                               createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)) };
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   104
            boolean[][] scopeNesting = { {false, true, false, true},
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   105
                                   {false, true, true, true},
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   106
                                   {false, false, true, true} };
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   107
            /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   108
             * We want to generate (and check) the following compound scopes:
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   109
             * C1 = C(S1, S2, S3)
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   110
             * C2 = C((S1, S2), S3)
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   111
             * C3 = C(S1, (S2, S3))
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   112
             * C3 = C(C(S1, S2, S3))
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   113
             */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   114
            for (int i = 0 ; i < 4 ; i ++) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   115
                CompoundScope root = new CompoundScope(symtab.noSymbol);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   116
                CompoundScope sub = new CompoundScope(symtab.noSymbol);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   117
                boolean subAdded = false;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   118
                for (int sc = 0 ; sc < 3 ; sc ++) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   119
                    if (scopeNesting[sc][i]) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   120
                        sub.addSubScope(scopes[sc]);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   121
                        if (!subAdded) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   122
                            root.addSubScope(sub);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   123
                            subAdded = true;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   124
                        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   125
                    } else {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   126
                        root.addSubScope(scopes[sc]);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   127
                    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   128
                }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   129
                log("testing scope: " + root);
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   130
                checkElems(root, null);
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   131
                checkElems(root, new OddFilter());
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   132
                checkShadowed(root, null);
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   133
                checkShadowed(root, new OddFilter());
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   134
            }
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   135
        }
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   136
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   137
        class OddFilter implements Filter<Symbol> {
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   138
            public boolean accepts(Symbol s) {
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   139
                Name numPart = s.name.subName(1, s.name.length());
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   140
                return Integer.parseInt(numPart.toString()) % 2 != 0;
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   141
            }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   142
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   143
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   144
        /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   145
         * Create a scope containing a given number of synthetic symbols
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   146
         */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   147
        Scope createScope(int nelems) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   148
            Scope s = new Scope(symtab.noSymbol);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   149
            for (int i = 0 ; i < nelems ; i++) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   150
                Symbol sym = new TypeSymbol(0, names.fromString("s" + i), null, null);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   151
                s.enter(sym);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   152
                elems = elems.prepend(sym);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   153
                List<Symbol> shadowed = shadowedMap.get(sym.name);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   154
                if (shadowed == null) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   155
                    shadowed = List.nil();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   156
                }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   157
                shadowedMap.put(sym.name, shadowed.prepend(sym));
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   158
            }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   159
            return s;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   160
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   161
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   162
        /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   163
         * Setup compiler context
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   164
         */
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   165
        void setup() {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   166
            log ("setup");
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   167
            context = new Context();
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   168
            JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   169
            names = Names.instance(context);       // Name.Table impls tied to an instance of Names
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   170
            symtab = Symtab.instance(context);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   171
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   172
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   173
        /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   174
         * Check that CompoundScope.getElements() correctly visits all symbols
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   175
         * in all subscopes (in the correct order)
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   176
         */
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   177
        void checkElems(CompoundScope cs, Filter<Symbol> sf) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   178
            int count = 0;
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   179
            ListBuffer<Symbol> found = ListBuffer.lb();
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   180
            List<Symbol> allSymbols = sf == null ?
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   181
                    elems :
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   182
                    filter(elems, sf);
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   183
            int expectedCount = allSymbols.length();
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   184
            for (Symbol s : sf == null ? cs.getElements() : cs.getElements(sf)) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   185
                checkSameSymbols(s, allSymbols.head);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   186
                allSymbols = allSymbols.tail;
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   187
                found.append(s);
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   188
                count++;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   189
            }
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   190
            if (count != expectedCount) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   191
                error("CompoundScope.getElements() did not returned enough symbols");
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   192
            }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   193
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   194
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   195
        /**
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   196
         * Check that CompoundScope.getElements() correctly visits all symbols
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   197
         * with a given name in all subscopes (in the correct order)
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   198
         */
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   199
        void checkShadowed(CompoundScope cs, Filter<Symbol> sf) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   200
            for (Map.Entry<Name, List<Symbol>> shadowedEntry : shadowedMap.entrySet()) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   201
                int count = 0;
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   202
                List<Symbol> shadowed = sf == null ?
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   203
                    shadowedEntry.getValue() :
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   204
                    filter(shadowedEntry.getValue(), sf);
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   205
                int expectedCount = shadowed.length();
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   206
                Name name = shadowedEntry.getKey();
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   207
                for (Symbol s : sf == null ? cs.getElementsByName(name) : cs.getElementsByName(name, sf)) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   208
                    checkSameSymbols(s, shadowed.head);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   209
                    shadowed = shadowed.tail;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   210
                    count++;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   211
                }
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   212
                if (count != expectedCount) {
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   213
                    error("CompoundScope.lookup() did not returned enough symbols for name " + name);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   214
                }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   215
            }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   216
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   217
9593
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   218
        List<Symbol> filter(List<Symbol> elems, Filter<Symbol> sf) {
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   219
            ListBuffer<Symbol> res = ListBuffer.lb();
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   220
            for (Symbol s : elems) {
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   221
                if (sf.accepts(s)) {
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   222
                    res.append(s);
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   223
                }
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   224
            }
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   225
            return res.toList();
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   226
        }
f917c044e651 7036906: Scope: CompoundScope.getElements() doesn't pass scope filter to subscopes
mcimadamore
parents: 8427
diff changeset
   227
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   228
        void checkSameSymbols(Symbol found, Symbol req) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   229
            if (found != req) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   230
                error("Symbol mismatch - found    : " + found + ":" + found.hashCode() + "\n" +
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   231
                      "                  required : " + req + ":" + req.hashCode());
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   232
            }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   233
        }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   234
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   235
        Context context;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   236
        Symtab symtab;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   237
        Names names;
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   238
    }
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
diff changeset
   239
}