JavacScope.java revision 2601:8e638f046bf0
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;
37
38
39
40/**
41 * Provides an implementation of Scope.
42 *
43 * <p><b>This is NOT part of any supported API.
44 * If you write code that depends on this, you do so at your own
45 * risk.  This code and its internal interfaces are subject to change
46 * or deletion without notice.</b></p>
47 *
48 * @author Jonathan Gibbons;
49 */
50public class JavacScope implements com.sun.source.tree.Scope {
51
52    static JavacScope create(Env<AttrContext> env) {
53        if (env.outer == null || env.outer == env) {
54            //the "top-level" scope needs to return both imported and defined elements
55            //see test CheckLocalElements
56            return new JavacScope(env) {
57                @Override @DefinedBy(Api.COMPILER_TREE)
58                public Iterable<? extends Element> getLocalElements() {
59                    return env.toplevel.namedImportScope.getSymbols();
60                }
61            };
62        } else {
63            return new JavacScope(env);
64        }
65    }
66
67    protected final Env<AttrContext> env;
68
69    private JavacScope(Env<AttrContext> env) {
70        env.getClass(); // null-check
71        this.env = env;
72    }
73
74    @DefinedBy(Api.COMPILER_TREE)
75    public JavacScope getEnclosingScope() {
76        if (env.outer != null && env.outer != env) {
77            return create(env.outer);
78        } else {
79            // synthesize an outermost "star-import" scope
80            return new JavacScope(env) {
81                public boolean isStarImportScope() {
82                    return true;
83                }
84                @DefinedBy(Api.COMPILER_TREE)
85                public JavacScope getEnclosingScope() {
86                    return null;
87                }
88                @DefinedBy(Api.COMPILER_TREE)
89                public Iterable<? extends Element> getLocalElements() {
90                    return env.toplevel.starImportScope.getSymbols();
91                }
92            };
93        }
94    }
95
96    @DefinedBy(Api.COMPILER_TREE)
97    public TypeElement getEnclosingClass() {
98        // hide the dummy class that javac uses to enclose the top level declarations
99        return (env.outer == null || env.outer == env ? null : env.enclClass.sym);
100    }
101
102    @DefinedBy(Api.COMPILER_TREE)
103    public ExecutableElement getEnclosingMethod() {
104        return (env.enclMethod == null ? null : env.enclMethod.sym);
105    }
106
107    @DefinedBy(Api.COMPILER_TREE)
108    public Iterable<? extends Element> getLocalElements() {
109        return env.info.getLocalElements();
110    }
111
112    public Env<AttrContext> getEnv() {
113        return env;
114    }
115
116    public boolean isStarImportScope() {
117        return false;
118    }
119
120    public boolean equals(Object other) {
121        if (other instanceof JavacScope) {
122            JavacScope s = (JavacScope) other;
123            return (env.equals(s.env)
124                && isStarImportScope() == s.isStarImportScope());
125        } else
126            return false;
127    }
128
129    public int hashCode() {
130        return env.hashCode() + (isStarImportScope() ? 1 : 0);
131    }
132
133    public String toString() {
134        return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
135    }
136}
137