1 /* 2 * Copyright (c) 2006, 2008, 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 26 package com.sun.tools.javac.api; 27 28 29 import javax.lang.model.element.Element; 30 import javax.lang.model.element.ExecutableElement; 31 import javax.lang.model.element.TypeElement; 32 33 import com.sun.tools.javac.comp.AttrContext; 34 import com.sun.tools.javac.comp.Env; 35 36 37 38 /** 39 * Provides an implementation of Scope. 40 * 41 * <p><b>This is NOT part of any supported API. 42 * If you write code that depends on this, you do so at your own 43 * risk. This code and its internal interfaces are subject to change 44 * or deletion without notice.</b></p> 45 * 46 * @author Jonathan Gibbons; 47 */ 48 public class JavacScope implements com.sun.source.tree.Scope { 49 protected final Env<AttrContext> env; 50 51 /** Creates a new instance of JavacScope */ 52 JavacScope(Env<AttrContext> env) { 53 env.getClass(); // null-check 54 this.env = env; 55 } 56 57 public JavacScope getEnclosingScope() { 58 if (env.outer != null && env.outer != env) 59 return new JavacScope(env.outer); 60 else { 61 // synthesize an outermost "star-import" scope 62 return new JavacScope(env) { 63 public boolean isStarImportScope() { 64 return true; 65 } 66 public JavacScope getEnclosingScope() { 67 return null; 68 } 69 public Iterable<? extends Element> getLocalElements() { 70 return env.toplevel.starImportScope.getElements(); 71 } 72 }; 73 } 74 } 75 76 public TypeElement getEnclosingClass() { 77 // hide the dummy class that javac uses to enclose the top level declarations 78 return (env.outer == null || env.outer == env ? null : env.enclClass.sym); 79 } 80 81 public ExecutableElement getEnclosingMethod() { 82 return (env.enclMethod == null ? null : env.enclMethod.sym); 83 } 84 85 public Iterable<? extends Element> getLocalElements() { 86 return env.info.getLocalElements(); 87 } 88 89 public Env<AttrContext> getEnv() { 90 return env; 91 } 92 93 public boolean isStarImportScope() { 94 return false; 95 } 96 97 public boolean equals(Object other) { 98 if (other instanceof JavacScope) { 99 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 }