GroovyShell的初始化
public GroovyShell(CompilerConfiguration config) {
this(new Binding(), config);
}
class Binding is subclass of GroovyObjectSupport
public GroovyObjectSupport() {
this.metaClass = InvokerHelper.getMetaClass(this.getClass());
}
while the InvokerHelper has static members
public static final MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry();
So the GroovySystem will be loaded, and its static members
static {
USE_REFLECTION = true;
META_CLASS_REGISTRY = new MetaClassRegistryImpl();
}
public MetaClassRegistryImpl() {
this(LOAD_DEFAULT, true);
}
it will call MetaClassRegistryImpl.registerMethods,
-> GeneratedMetaMethod.DgmMethodRecord.loadDgmInfo();
the function will analysis groovy-2.4.0.jar$META-INF/dgminfo
這是一個二進制流的文件,她定義了很多基本的函數的參數個數,類型,返回值的類型等等
這個函數最後返回一個列表,記錄了所歐的基本函數
registerMethods(null, true, true, map);返回後, map是一個CachedClass爲key (如java.util.Calendar)
list<MetaMethod> 爲value的map 如[org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy@a5852[name: clearTime params: [] returns: class java.util.Calendar owner: class java.util.Calendar],..]
接着和DefaultGroovyMethods.additionals 合併, 不斷填充map
接着和class org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods 合併 (所有函數爲 [
public static java.lang.StringBuilder org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.leftShift(java.lang.StringBuilder,java.lang.Object), public static java.lang.Object org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.next(java.lang.Enum),
public static java.lang.String org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.plus(java.lang.StringBuilder,java.lang.String), public static java.lang.Object org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.previous(java.lang.Enum),
public static void org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.putAt(java.lang.StringBuilder,groovy.lang.EmptyRange,java.lang.Object), public static void org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.putAt(java.lang.StringBuilder,groovy.lang.IntRange,java.lang.Object), public static int org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods.size(java.lang.StringBuilder)])
注意leftShift
接着DefaultGroovyStaticMethods
接着 ExtensionModuleScanner scanner = new ExtensionModuleScanner(new DefaultModuleListener(map), this.getClass().getClassLoader());
scanner.scanClasspathModules();
從所有META-INF/services/org.codehaus.groovy.runtime.ExtensionModule 中找出properties,
處理 ---------具體略以後分析
從 groovy-jsr223-<version>.jar中,找出
[public static java.lang.Object org.codehaus.groovy.jsr223.ScriptExtensions.eval(javax.script.ScriptEngine,java.io.Reader,groovy.lang.Binding) throws javax.script.ScriptException, public static java.lang.Object org.codehaus.groovy.jsr223.ScriptExtensions.eval(javax.script.ScriptEngine,java.lang.String,groovy.lang.Binding) throws javax.script.ScriptException, public static javax.script.ScriptEngine org.codehaus.groovy.jsr223.ScriptStaticExtensions.$static_propertyMissing(javax.script.ScriptEngineManager,java.lang.String)]
以上過程也在同時不斷寫MetaClassRegistryImpl類的
private FastArray instanceMethods = new FastArray();
private FastArray staticMethods = new FastArray();
GroovyShell的初始化過程
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.