src/share/classes/com/sun/tools/javac/api/JavacScope.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

Initial load

     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  */
    26 package com.sun.tools.javac.api;
    28 import java.io.IOException;
    29 import java.lang.ref.SoftReference;
    30 import java.util.Iterator;
    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;
    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;
    62 import static com.sun.source.tree.Tree.Kind.*;
    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;
    78     /** Creates a new instance of JavacScope */
    79     JavacScope(Env<AttrContext> env) {
    80         env.getClass(); // null-check
    81         this.env = env;
    82     }
    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     }
   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     }
   108     public ExecutableElement getEnclosingMethod() {
   109         return (env.enclMethod == null ? null : env.enclMethod.sym);
   110     }
   112     public Iterable<? extends Element> getLocalElements() {
   113         return env.info.getLocalElements();
   114     }
   116     public Env<AttrContext> getEnv() {
   117         return env;
   118     }
   120     public boolean isStarImportScope() {
   121         return false;
   122     }
   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     }
   133     public int hashCode() {
   134         return env.hashCode() + (isStarImportScope() ? 1 : 0);
   135     }
   137     public String toString() {
   138         return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
   139     }
   140 }

mercurial