补充上次的runBuildInProcess builder() ->new 了一个ServiceRegistryBuilder build()->new 一个前文多次提到的DefaultServiceRegistry, 返回,同时把传入的GlobalScopeServices类attach了上去 找configure方法的时候,会找到例如 void configure(ServiceRegistration registration, ClassLoaderRegistry classLoaderRegistry) ServiceRegistryBuild.java public ServiceRegistry build() { DefaultServiceRegistry registry = new DefaultServiceRegistry(displayName, parents); for (Object provider : providers) { registry.addProvider(provider); } return registry; } 这个provider就是GlobalScopeServices DefaultServiceRegistry.java private void findProviderMethods(Object target) { Set<String> methods = new HashSet<String>(); for (Class<?> type = target.getClass(); type != Object.class; type = type.getSuperclass()) { findDecoratorMethods(target, type, methods, ownServices); findFactoryMethods(target, type, methods, ownServices); } findConfigureMethod(target); } //findConfigureMethod(target); 这个target也是GlobalScopeServices 在private void findConfigureMethod(Object target)中 invoke(method, target, params); //method就是void org.gradle.internal.service.scopes.GlobalScopeServices.configure(org.gradle.internal.service.ServiceRegistration,org.gradle.initialization.ClassLoaderRegistry) //target => org.gradle.internal.service.scopes.GlobalScopeServices@1c9eaf8 //params[0]=>org.gradle.internal.service.DefaultServiceRegistry$1,也就是ServiceRegistration, 它是一个接口定义,在 //DefaultServiceRegistry.java中重新定义 private ServiceRegistration newRegistration() { return new ServiceRegistration(){ public <T> void add(Class<T> serviceType, T serviceInstance) { DefaultServiceRegistry.this.add(serviceType, serviceInstance); } public void add(Class<?> serviceType) { ownServices.add(new ConstructorService(serviceType)); } public void addProvider(Object provider) { DefaultServiceRegistry.this.addProvider(provider); } }; } 而在GlobalScopeService.java中 (从上面的invoke调入) void configure(ServiceRegistration registration, ClassLoaderRegistry classLoaderRegistry) { final List<PluginServiceRegistry> pluginServiceFactories = new ServiceLocator(classLoaderRegistry.getRuntimeClassLoader(), classLoaderRegistry.getPluginsClassLoader()).getAll(PluginServiceRegistry.class); for (PluginServiceRegistry pluginServiceRegistry : pluginServiceFactories) { registration.add(PluginServiceRegistry.class, pluginServiceRegistry); pluginServiceRegistry.registerGlobalServices(registration); } } 它会走进override的registration.add , 它会把item加到DefaultServiceRegistry.ownServices列表中 这也是ownService列表里面items的来源之一. 堆栈如图:
原文地址:http://blog.csdn.net/cleverwyq/article/details/42685027