001    /*
002     * Copyright 2006-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.api;
027    
028    
029    import javax.lang.model.element.Element;
030    import javax.lang.model.element.ExecutableElement;
031    import javax.lang.model.element.TypeElement;
032    
033    import com.sun.tools.javac.comp.AttrContext;
034    import com.sun.tools.javac.comp.Env;
035    
036    
037    
038    /**
039     * Provides an implementation of Scope.
040     *
041     * <p><b>This is NOT part of any API supported by Sun Microsystems.
042     * If you write code that depends on this, you do so at your own
043     * risk.  This code and its internal interfaces are subject to change
044     * or deletion without notice.</b></p>
045     *
046     * @author Jonathan Gibbons;
047     */
048    public class JavacScope implements com.sun.source.tree.Scope {
049        protected final Env<AttrContext> env;
050    
051        /** Creates a new instance of JavacScope */
052        JavacScope(Env<AttrContext> env) {
053            env.getClass(); // null-check
054            this.env = env;
055        }
056    
057        public JavacScope getEnclosingScope() {
058            if (env.outer != null && env.outer != env)
059                return  new JavacScope(env.outer);
060            else {
061                // synthesize an outermost "star-import" scope
062                return new JavacScope(env) {
063                    public boolean isStarImportScope() {
064                        return true;
065                    }
066                    public JavacScope getEnclosingScope() {
067                        return null;
068                    }
069                    public Iterable<? extends Element> getLocalElements() {
070                        return env.toplevel.starImportScope.getElements();
071                    }
072                };
073            }
074        }
075    
076        public TypeElement getEnclosingClass() {
077            // hide the dummy class that javac uses to enclose the top level declarations
078            return (env.outer == null || env.outer == env ? null : env.enclClass.sym);
079        }
080    
081        public ExecutableElement getEnclosingMethod() {
082            return (env.enclMethod == null ? null : env.enclMethod.sym);
083        }
084    
085        public Iterable<? extends Element> getLocalElements() {
086            return env.info.getLocalElements();
087        }
088    
089        public Env<AttrContext> getEnv() {
090            return env;
091        }
092    
093        public boolean isStarImportScope() {
094            return false;
095        }
096    
097        public boolean equals(Object other) {
098            if (other instanceof JavacScope) {
099                JavacScope s = (JavacScope) other;
100                return (env.equals(s.env)
101                    && isStarImportScope() == s.isStarImportScope());
102            } else
103                return false;
104        }
105    
106        public int hashCode() {
107            return env.hashCode() + (isStarImportScope() ? 1 : 0);
108        }
109    
110        public String toString() {
111            return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
112        }
113    }