Gradle source code entry 7: gradle source code
Today I analyzed the Object org. gradle. internal. service. DefaultServiceRegistry. doGet (Type serviceType) function.
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)); } }
The focus here is context. find (serviceType, allServices );
AllServices is a variable of the CompositeProvider type. When serviceType is a value
In the getService of the CompositeProvider class, the class diagram shows that it also maintains a List of providers,
Traverse the list and continue provider. getSerivce.
Ownservices is also a member of allServices. providers by default.
- FindDecoratorMethods (target, type, methods, ownServices );
- FindFactoryMethods (target, type, methods, ownServices );
Come,
Result
For example, when serviceType is interface org. gradle. logging. StyledTextOutputFactory
Provider is Service StyledTextOutputFactory at LoggingServiceRegistry. createStyledTextOutputFactory ()