JavacScope.java revision 2837:1e3266d870d6
1/*
2 * Copyright (c) 2006, 2014, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.api;
27
28
29import javax.lang.model.element.Element;
30import javax.lang.model.element.ExecutableElement;
31import javax.lang.model.element.TypeElement;
32
33import com.sun.tools.javac.comp.AttrContext;
34import com.sun.tools.javac.comp.Env;
35import com.sun.tools.javac.util.DefinedBy;
36import com.sun.tools.javac.util.DefinedBy.Api;
37import com.sun.tools.javac.util.Assert;
38
39/**
40 * Provides an implementation of Scope.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own
44 * risk.  This code and its internal interfaces are subject to change
45 * or deletion without notice.</b></p>
46 *
47 * @author Jonathan Gibbons;
48 */
49public class JavacScope implements com.sun.source.tree.Scope {
50
51    static JavacScope create(Env<AttrContext> env) {
52        if (env.outer == null || env.outer == env) {
53            //the "top-level" scope needs to return both imported and defined elements
54            //see test CheckLocalElements
55            return new JavacScope(env) {
56                @Override @DefinedBy(Api.COMPILER_TREE)
57                public Iterable<? extends Element> getLocalElements() {
58                    return env.toplevel.namedImportScope.getSymbols();
59                }
60            };
61        } else {
62            return new JavacScope(env);
63        }
64    }
65
66    protected final Env<AttrContext> env;
67
68    private JavacScope(Env<AttrContext> env) {
69        this.env = Assert.checkNonNull(env);
70    }
71
72    @DefinedBy(Api.COMPILER_TREE)
73    public JavacScope getEnclosingScope() {
74        if (env.outer != null && env.outer != env) {
75            return create(env.outer);
76        } else {
77            // synthesize an outermost "star-import" scope
78            return new JavacScope(env) {
79                public boolean isStarImportScope() {
80                    return true;
81                }
82                @DefinedBy(Api.COMPILER_TREE)
83                public JavacScope getEnclosingScope() {
84                    return null;
85                }
86                @DefinedBy(Api.COMPILER_TREE)
87                public Iterable<? extends Element> getLocalElements() {
88                    return env.toplevel.starImportScope.getSymbols();
89                }
90            };
91        }
92    }
93
94    @DefinedBy(Api.COMPILER_TREE)
95    public TypeElement getEnclosingClass() {
96        // hide the dummy class that javac uses to enclose the top level declarations
97        return (env.outer == null || env.outer == env ? null : env.enclClass.sym);
98    }
99
100    @DefinedBy(Api.COMPILER_TREE)
101    public ExecutableElement getEnclosingMethod() {
102        return (env.enclMethod == null ? null : env.enclMethod.sym);
103    }
104
105    @DefinedBy(Api.COMPILER_TREE)
106    public Iterable<? extends Element> getLocalElements() {
107        return env.info.getLocalElements();
108    }
109
110    public Env<AttrContext> getEnv() {
111        return env;
112    }
113
114    public boolean isStarImportScope() {
115        return false;
116    }
117
118    public boolean equals(Object other) {
119        if (other instanceof JavacScope) {
120            JavacScope s = (JavacScope) other;
121            return (env.equals(s.env)
122                && isStarImportScope() == s.isStarImportScope());
123        } else
124            return false;
125    }
126
127    public int hashCode() {
128        return env.hashCode() + (isStarImportScope() ? 1 : 0);
129    }
130
131    public String toString() {
132        return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
133    }
134}
135