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

changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacScope.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,140 @@
     1.4 +/*
     1.5 + * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.api;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.lang.ref.SoftReference;
    1.33 +import java.util.Iterator;
    1.34 +
    1.35 +import javax.lang.model.element.Element;
    1.36 +import javax.lang.model.element.ExecutableElement;
    1.37 +import javax.lang.model.element.TypeElement;
    1.38 +import javax.tools.JavaFileObject;
    1.39 +
    1.40 +import com.sun.source.tree.Tree;
    1.41 +import com.sun.source.util.SourcePositions;
    1.42 +import com.sun.source.util.TreePath;
    1.43 +import com.sun.source.util.Trees;
    1.44 +import com.sun.tools.javac.code.Scope;
    1.45 +import com.sun.tools.javac.code.Symbol.ClassSymbol;
    1.46 +import com.sun.tools.javac.comp.Attr;
    1.47 +import com.sun.tools.javac.comp.AttrContext;
    1.48 +import com.sun.tools.javac.comp.Enter;
    1.49 +import com.sun.tools.javac.comp.Env;
    1.50 +import com.sun.tools.javac.comp.MemberEnter;
    1.51 +import com.sun.tools.javac.comp.Resolve;
    1.52 +import com.sun.tools.javac.tree.JCTree.JCClassDecl;
    1.53 +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    1.54 +import com.sun.tools.javac.tree.JCTree.JCExpression;
    1.55 +import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
    1.56 +import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
    1.57 +import com.sun.tools.javac.tree.JCTree;
    1.58 +import com.sun.tools.javac.tree.TreeCopier;
    1.59 +import com.sun.tools.javac.tree.TreeInfo;
    1.60 +import com.sun.tools.javac.tree.TreeMaker;
    1.61 +import com.sun.tools.javac.util.Context;
    1.62 +import com.sun.tools.javac.util.List;
    1.63 +import com.sun.tools.javac.util.Log;
    1.64 +
    1.65 +import static com.sun.source.tree.Tree.Kind.*;
    1.66 +
    1.67 +
    1.68 +/**
    1.69 + * Provides an implementation of Scope.
    1.70 + *
    1.71 + * <p><b>This is NOT part of any API supported by Sun Microsystems.
    1.72 + * If you write code that depends on this, you do so at your own
    1.73 + * risk.  This code and its internal interfaces are subject to change
    1.74 + * or deletion without notice.</b></p>
    1.75 + *
    1.76 + * @author Jonathan Gibbons;
    1.77 + */
    1.78 +public class JavacScope implements com.sun.source.tree.Scope {
    1.79 +    protected final Env<AttrContext> env;
    1.80 +
    1.81 +    /** Creates a new instance of JavacScope */
    1.82 +    JavacScope(Env<AttrContext> env) {
    1.83 +        env.getClass(); // null-check
    1.84 +        this.env = env;
    1.85 +    }
    1.86 +
    1.87 +    public JavacScope getEnclosingScope() {
    1.88 +        if (env.outer != null && env.outer != env)
    1.89 +            return  new JavacScope(env.outer);
    1.90 +        else {
    1.91 +            // synthesize an outermost "star-import" scope
    1.92 +            return new JavacScope(env) {
    1.93 +                public boolean isStarImportScope() {
    1.94 +                    return true;
    1.95 +                }
    1.96 +                public JavacScope getEnclosingScope() {
    1.97 +                    return null;
    1.98 +                }
    1.99 +                public Iterable<? extends Element> getLocalElements() {
   1.100 +                    return env.toplevel.starImportScope.getElements();
   1.101 +                }
   1.102 +            };
   1.103 +        }
   1.104 +    }
   1.105 +
   1.106 +    public TypeElement getEnclosingClass() {
   1.107 +        // hide the dummy class that javac uses to enclose the top level declarations
   1.108 +        return (env.outer == null || env.outer == env ? null : env.enclClass.sym);
   1.109 +    }
   1.110 +
   1.111 +    public ExecutableElement getEnclosingMethod() {
   1.112 +        return (env.enclMethod == null ? null : env.enclMethod.sym);
   1.113 +    }
   1.114 +
   1.115 +    public Iterable<? extends Element> getLocalElements() {
   1.116 +        return env.info.getLocalElements();
   1.117 +    }
   1.118 +
   1.119 +    public Env<AttrContext> getEnv() {
   1.120 +        return env;
   1.121 +    }
   1.122 +
   1.123 +    public boolean isStarImportScope() {
   1.124 +        return false;
   1.125 +    }
   1.126 +
   1.127 +    public boolean equals(Object other) {
   1.128 +        if (other instanceof JavacScope) {
   1.129 +            JavacScope s = (JavacScope) other;
   1.130 +            return (env.equals(s.env)
   1.131 +                && isStarImportScope() == s.isStarImportScope());
   1.132 +        } else
   1.133 +            return false;
   1.134 +    }
   1.135 +
   1.136 +    public int hashCode() {
   1.137 +        return env.hashCode() + (isStarImportScope() ? 1 : 0);
   1.138 +    }
   1.139 +
   1.140 +    public String toString() {
   1.141 +        return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
   1.142 +    }
   1.143 +}

mercurial