JavacScope.java revision 2571:10fc81ac75b4
1/* 2 * Copyright (c) 2006, 2014, 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 26package com.sun.tools.javac.api; 27 28 29import javax.lang.model.element.Element; 30import javax.lang.model.element.ExecutableElement; 31import javax.lang.model.element.TypeElement; 32 33import com.sun.tools.javac.comp.AttrContext; 34import 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 */ 48public class JavacScope implements com.sun.source.tree.Scope { 49 50 static JavacScope create(Env<AttrContext> env) { 51 if (env.outer == null || env.outer == env) { 52 //the "top-level" scope needs to return both imported and defined elements 53 //see test CheckLocalElements 54 return new JavacScope(env) { 55 @Override 56 public Iterable<? extends Element> getLocalElements() { 57 return env.toplevel.namedImportScope.getSymbols(); 58 } 59 }; 60 } else { 61 return new JavacScope(env); 62 } 63 } 64 65 protected final Env<AttrContext> env; 66 67 private JavacScope(Env<AttrContext> env) { 68 env.getClass(); // null-check 69 this.env = env; 70 } 71 72 public JavacScope getEnclosingScope() { 73 if (env.outer != null && env.outer != env) { 74 return create(env.outer); 75 } else { 76 // synthesize an outermost "star-import" scope 77 return new JavacScope(env) { 78 public boolean isStarImportScope() { 79 return true; 80 } 81 public JavacScope getEnclosingScope() { 82 return null; 83 } 84 public Iterable<? extends Element> getLocalElements() { 85 return env.toplevel.starImportScope.getSymbols(); 86 } 87 }; 88 } 89 } 90 91 public TypeElement getEnclosingClass() { 92 // hide the dummy class that javac uses to enclose the top level declarations 93 return (env.outer == null || env.outer == env ? null : env.enclClass.sym); 94 } 95 96 public ExecutableElement getEnclosingMethod() { 97 return (env.enclMethod == null ? null : env.enclMethod.sym); 98 } 99 100 public Iterable<? extends Element> getLocalElements() { 101 return env.info.getLocalElements(); 102 } 103 104 public Env<AttrContext> getEnv() { 105 return env; 106 } 107 108 public boolean isStarImportScope() { 109 return false; 110 } 111 112 public boolean equals(Object other) { 113 if (other instanceof JavacScope) { 114 JavacScope s = (JavacScope) other; 115 return (env.equals(s.env) 116 && isStarImportScope() == s.isStarImportScope()); 117 } else 118 return false; 119 } 120 121 public int hashCode() { 122 return env.hashCode() + (isStarImportScope() ? 1 : 0); 123 } 124 125 public String toString() { 126 return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]"; 127 } 128} 129