Scope及其子类介绍
Posted extjs4
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scope及其子类介绍相关的知识,希望对你有一定的参考价值。
之前写的文章:
Scope及相关的子类如下:
同时有些Scope还继承了Scope.ScopeListener类,如下:
1、StarImportScope及ImportScope
在JCCompilationUnit中定义了两个属性,其类型就是这两个类型:
public ImportScope namedImportScope;
public StarImportScope starImportScope;
但在初始化JCCompilationUnit语法节点时并不初始化这两个属性,而是在Enter的如下类中进行初始化,代码如下:
/** Create a fresh environment for toplevels.
* @param tree The toplevel tree.
*/
public Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
tree.namedImportScope = new ImportScope(tree.packge);
tree.starImportScope = new StarImportScope(tree.packge);
Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
localEnv.toplevel = tree;
localEnv.enclClass = predefClassDef;
// JCCompilationUnit的环境Scope中使用的是namedImportScope
localEnv.info.scope = tree.namedImportScope;
localEnv.info.lint = lint;
return localEnv;
}
Scope最重要的作用就是用来查找特定范围内定义的符号,而这些符号的填充在MemberEnter类中完成的。首先看StarImportScope的填充,代码如下:
/** Import all classes of a class or package on demand.
* @param pos Position to be used for error reporting.
* @param tsym The class or package the members of which are imported.
* @param toScope The (import) scope in which imported classes are entered.
*/
private void importAll(int pos,
final TypeSymbol tsym,
Env<AttrContext> env) {
// Check that packages imported from exist (JLS ???).
// 在调用tsym.members()会调用complete()方法来完成这个符号属性的填充
if (tsym.kind == PCK01 &&
tsym.members().elems == null &&
!tsym.exists()) {
// If we can‘t find java.lang, exit immediately.
if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
// 致命错误: 在类路径或引导类路径中找不到程序包 java.lang
JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang");
throw new FatalError(msg);
} else {
// 程序包{0}不存在
log.error(JCDiagnosticFlag.RESOLVE_ERROR, pos, "doesnt.exist", tsym);
}
}
Scope sp = tsym.members();
env.toplevel.starImportScope.importAll(sp);
}
每个JCCompilationUnit编译单元通过import导入需要的依赖,有一个是默认导入的,就是java.lang.*下面的定义的类,而代码sp中包含的类符号如下:
Scope[
Void,VirtualMachineError,
VerifyError,UnsupportedOperationException,
UnsupportedClassVersionError,UnsatisfiedLinkError,
UnknownError, TypeNotPresentException, Throwable,
Throwable$WrappedPrintWriter,
Throwable$WrappedPrintStream,Throwable$SentinelHolder,
Throwable$PrintStreamOrWriter,ThreadLocal,
ThreadLocal$ThreadLocalMap,ThreadLocal$ThreadLocalMap$Entry,
ThreadGroup,ThreadDeath, Thread,
Thread$WeakClassKey, Thread$UncaughtExceptionHandler,
Thread$State, Thread$Caches, Terminator,
SystemClassLoaderAction, System, SuppressWarnings,
StringIndexOutOfBoundsException, StringCoding,
StringCoding$StringEncoder, StringCoding$StringDecoder,
StringBuilder, StringBuffer, String,
String$CaseInsensitiveComparator, StrictMath,
StackTraceElement, StackOverflowError,
Shutdown,Shutdown$Lock, Short,
Short$ShortCache, SecurityManager,
SecurityException, SafeVarargs, RuntimePermission,
RuntimeException, Runtime, Runnable,
ReflectiveOperationException, Readable,
ProcessImpl, ProcessImpl$LazyPattern,
ProcessEnvironment, ProcessEnvironment$NameComparator,
ProcessEnvironment$EntryComparator,
ProcessEnvironment$CheckedValues,
ProcessEnvironment$CheckedKeySet,
ProcessEnvironment$CheckedEntrySet,
ProcessEnvironment$CheckedEntry, ProcessBuilder,
ProcessBuilder$Redirect,
ProcessBuilder$Redirect$Type,ProcessBuilder$NullOutputStream,
ProcessBuilder$NullInputStream, Process,
Package, Override, OutOfMemoryError,
Object,NumberFormatException, Number,
NullPointerException, NoSuchMethodException,
NoSuchMethodError, NoSuchFieldException,
NoSuchFieldError, NoClassDefFoundError,
NegativeArraySizeException, Math, Long,
Long$LongCache, LinkageError, Iterable,
InterruptedException, InternalError, Integer,
Integer$IntegerCache, InstantiationException,
InstantiationError, InheritableThreadLocal,
IndexOutOfBoundsException,
IncompatibleClassChangeError,IllegalThreadStateException,
IllegalStateException, IllegalMonitorStateException,
IllegalArgumentException, IllegalAccessException,
IllegalAccessError, Float,
ExceptionInInitializerError, Exception, Error,
EnumConstantNotPresentException, Enum, Double,
Deprecated, ConditionalSpecialCasing,
ConditionalSpecialCasing$Entry, Compiler,
Comparable, Cloneable,CloneNotSupportedException,
ClassValue, ClassValue$Version,
ClassValue$Identity, ClassValue$Entry,
ClassValue$ClassValueMap,ClassNotFoundException,
ClassLoaderHelper, ClassLoader,
ClassLoader$ParallelLoaders, ClassLoader$NativeLibrary,
ClassFormatError, ClassCircularityError,
ClassCastException,Class,
Class$SecurityManagerHelper, Class$ReflectionData,
Class$MethodArray, Class$EnclosingMethodInfo,
Class$Atomic, CharacterName,
CharacterDataUndefined, CharacterDataPrivateUse,
CharacterDataLatin1, CharacterData0E,
CharacterData02,CharacterData01, CharacterData00,
CharacterData, Character, Character$UnicodeScript,
Character$UnicodeBlock, Character$Subset,
Character$CharacterCache, CharSequence, Byte,
Byte$ByteCache, BootstrapMethodError, Boolean,
AutoCloseable, AssertionStatusDirectives,
AssertionError, ArrayStoreException,
ArrayIndexOutOfBoundsException, ArithmeticException,
ApplicationShutdownHooks, Appendable,
AbstractStringBuilder, AbstractMethodError ]
调用了StarImportScope的importAll()方法,传入了sp参数:importAll()方法代码如下:
public class StarImportScope extends ImportScope implements Scope.ScopeListener {
public StarImportScope(Symbol owner) {
super(owner);
}
public void importAll (Scope fromScope) {
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
if (e.sym.kind == Kinds.TYP02 && !includes(e.sym)) {
enter(e.sym, fromScope);
}
}
// Register to be notified when imported items are removed
fromScope.addScopeListener(this);
}
public void symbolRemoved(Symbol sym, Scope s) {
remove(sym);
}
public void symbolAdded(Symbol sym, Scope s) { }
}
将sp中的所有符号导入StarImportScope中,并且StarImportScope中的table属性(Entry[]类型)中的元素为ImportEntry,如下部分截图:
说一下静态导入和非静态导入:
import java.io.*; import java.math.BigDecimal; import static com.test19.TestStatic.*; import static com.test19.TestStatic.method; // 导入 public class TestScope{ public void t(){ InputStream p; BigDecimal d; int b = v; method(); } }
无论静态还是非静态导入,最终都会调用MemberEnter类中的visitImport()方法,
以上是关于Scope及其子类介绍的主要内容,如果未能解决你的问题,请参考以下文章
Android基础到进阶UI ImageView及其子类 介绍+实例
UI组件之AdapterView及其子类ListView组件和ListActivity