| 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.tools.javac.api; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.lang.ref.SoftReference; |
| 30 | import java.util.Iterator; |
| 31 | |
| 32 | import javax.lang.model.element.Element; |
| 33 | import javax.lang.model.element.ExecutableElement; |
| 34 | import javax.lang.model.element.TypeElement; |
| 35 | import javax.tools.JavaFileObject; |
| 36 | |
| 37 | import com.sun.source.tree.Tree; |
| 38 | import com.sun.source.util.SourcePositions; |
| 39 | import com.sun.source.util.TreePath; |
| 40 | import com.sun.source.util.Trees; |
| 41 | import com.sun.tools.javac.code.Scope; |
| 42 | import com.sun.tools.javac.code.Symbol.ClassSymbol; |
| 43 | import com.sun.tools.javac.comp.Attr; |
| 44 | import com.sun.tools.javac.comp.AttrContext; |
| 45 | import com.sun.tools.javac.comp.Enter; |
| 46 | import com.sun.tools.javac.comp.Env; |
| 47 | import com.sun.tools.javac.comp.MemberEnter; |
| 48 | import com.sun.tools.javac.comp.Resolve; |
| 49 | import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
| 50 | import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
| 51 | import com.sun.tools.javac.tree.JCTree.JCExpression; |
| 52 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
| 53 | import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
| 54 | import com.sun.tools.javac.tree.JCTree; |
| 55 | import com.sun.tools.javac.tree.TreeCopier; |
| 56 | import com.sun.tools.javac.tree.TreeInfo; |
| 57 | import com.sun.tools.javac.tree.TreeMaker; |
| 58 | import com.sun.tools.javac.util.Context; |
| 59 | import com.sun.tools.javac.util.List; |
| 60 | import com.sun.tools.javac.util.Log; |
| 61 | |
| 62 | import static com.sun.source.tree.Tree.Kind.*; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Provides an implementation of Scope. |
| 67 | * |
| 68 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
| 69 | * If you write code that depends on this, you do so at your own |
| 70 | * risk. This code and its internal interfaces are subject to change |
| 71 | * or deletion without notice.</b></p> |
| 72 | * |
| 73 | * @author Jonathan Gibbons; |
| 74 | */ |
| 75 | public class JavacScope implements com.sun.source.tree.Scope { |
| 76 | protected final Env<AttrContext> env; |
| 77 | |
| 78 | /** Creates a new instance of JavacScope */ |
| 79 | JavacScope(Env<AttrContext> env) { |
| 80 | env.getClass(); // null-check |
| 81 | this.env = env; |
| 82 | } |
| 83 | |
| 84 | public JavacScope getEnclosingScope() { |
| 85 | if (env.outer != null && env.outer != env) |
| 86 | return new JavacScope(env.outer); |
| 87 | else { |
| 88 | // synthesize an outermost "star-import" scope |
| 89 | return new JavacScope(env) { |
| 90 | public boolean isStarImportScope() { |
| 91 | return true; |
| 92 | } |
| 93 | public JavacScope getEnclosingScope() { |
| 94 | return null; |
| 95 | } |
| 96 | public Iterable<? extends Element> getLocalElements() { |
| 97 | return env.toplevel.starImportScope.getElements(); |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public TypeElement getEnclosingClass() { |
| 104 | // hide the dummy class that javac uses to enclose the top level declarations |
| 105 | return (env.outer == null || env.outer == env ? null : env.enclClass.sym); |
| 106 | } |
| 107 | |
| 108 | public ExecutableElement getEnclosingMethod() { |
| 109 | return (env.enclMethod == null ? null : env.enclMethod.sym); |
| 110 | } |
| 111 | |
| 112 | public Iterable<? extends Element> getLocalElements() { |
| 113 | return env.info.getLocalElements(); |
| 114 | } |
| 115 | |
| 116 | public Env<AttrContext> getEnv() { |
| 117 | return env; |
| 118 | } |
| 119 | |
| 120 | public boolean isStarImportScope() { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | public boolean equals(Object other) { |
| 125 | if (other instanceof JavacScope) { |
| 126 | JavacScope s = (JavacScope) other; |
| 127 | return (env.equals(s.env) |
| 128 | && isStarImportScope() == s.isStarImportScope()); |
| 129 | } else |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | public int hashCode() { |
| 134 | return env.hashCode() + (isStarImportScope() ? 1 : 0); |
| 135 | } |
| 136 | |
| 137 | public String toString() { |
| 138 | return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]"; |
| 139 | } |
| 140 | } |