首页 > 代码库 > Gradle原码入门七

Gradle原码入门七

今天分析了Object org.gradle.internal.service.DefaultServiceRegistry.doGet(Type serviceType) 函数


    private Object doGet(Type serviceType) throws IllegalArgumentException {
        synchronized (lock) {
            if (closed) {
                throw new IllegalStateException(String.format("Cannot locate service of type %s, as %s has been closed.", format(serviceType), displayName));
            }

            DefaultLookupContext context = new DefaultLookupContext();
            ServiceProvider provider = context.find(serviceType, allServices);
            if (provider != null) {
                return provider.get();
            }

            throw new UnknownServiceException(serviceType, String.format("No service of type %s available in %s.", format(serviceType), displayName));
        }
    }

技术分享


这里重点是context.find(serviceType, allServices);

allServices 是一个CompositeProvider型变量, 在serviceType是某些值的时候,会走到

CompositeProvider类的getService里面, 类图可以看到它自身也维护了一个Provider的List,

遍历该list,   继续provider.getSerivce

默认ownservices也是allServices.providers的一员, ownServices的几何从前文<六>中的

  1.   findDecoratorMethods(target, type, methods, ownServices);  
  2.             findFactoryMethods(target, type, methods, ownServices);  
中来,


结果

例如当serviceType是 interface org.gradle.logging.StyledTextOutputFactory

provider就是Service StyledTextOutputFactory at LoggingServiceRegistry.createStyledTextOutputFactory()


Gradle原码入门七