码迷,mamicode.com
首页 > 其他好文 > 详细

Gradle源码八续

时间:2015-01-13 23:25:19      阅读:616      评论:0      收藏:0      [点我收藏+]

标签:gradle   源码   java   调试   

补充上次的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的来源之一.

    堆栈如图:
    



技术分享

Gradle源码八续

标签:gradle   源码   java   调试   

原文地址:http://blog.csdn.net/cleverwyq/article/details/42685027

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!