Gradle源码入门学习之十-- Listener & Dispatch

new ListenerBroadcast<OutputEventListener>(OutputEventListener.class);
->
    public ListenerBroadcast(Class<T> type) {
        this.type = type;
        broadcast = new BroadcastDispatch<T>(type);
        source = new ProxyDispatchAdapter<T>(broadcast, type);
    }
->
	//broadcast的初始化:1 type  2. handlers
	public BroadcastDispatch(Class<T> type) {
        this.type = type;
    }

	//生成的broadcast,传给dispatch param
	public ProxyDispatchAdapter(Dispatch<? super MethodInvocation> dispatch, Class<T> type, Class<?>... extraTypes) {
        this.type = type;
		//传入的dispatch 是上面的 broadcast 变量,通过InvocationHandler的构造函数,
		//在Invoke的时候,其实是broadcast在发消息,call dispatch函数.
        source = type.cast(Proxy.newProxyInstance(classLoader, types.toArray(new Class<?>[types.size()]),
                new DispatchingInvocationHandler(type, dispatch)));
    }
------------>
	    private DispatchingInvocationHandler(Class<?> type, Dispatch<? super MethodInvocation> dispatch) {
            this.type = type;
            this.dispatch = dispatch;
        }

		 public Object invoke(Object target, Method method, Object[] parameters) throws Throwable {
            dispatch.dispatch(new MethodInvocation(method, parameters));

        }

In DefaultGradle's constructor
buildListenerBroadcast = listenerManager.createAnonymousBroadcaster(BuildListener.class);
->DefaultListenerManager.java
    public <T> ListenerBroadcast<T> createAnonymousBroadcaster(Class<T> listenerClass) {
        ListenerBroadcast<T> broadcast = new ListenerBroadcast(listenerClass);
        broadcast.add(getBroadcasterInternal(listenerClass).getSource());
        return broadcast;
    }
->
    private <T> ListenerBroadcast<T> getBroadcasterInternal(Class<T> listenerClass) {
        synchronized (lock) {
            ListenerBroadcast<T> broadcaster = broadcasters.get(listenerClass);
            if (broadcaster == null) {
                broadcaster = new ListenerBroadcast<T>(listenerClass);
                broadcaster.add(getLogger(listenerClass));    //so BuildLogger 's method buildStarted can be called later.
                broadcaster.add(getDispatcher(listenerClass));
                if (parent != null) {
                    broadcaster.add(parent.getDispatcher(listenerClass));
                }
                broadcasters.put(listenerClass, broadcaster);
            }

            return broadcaster;
        }
    }
at this time before execute the function 'getBroadcasterInternal'
broadcasters =
{interface org.gradle.api.execution.TaskExecutionListener=[email protected],
interface org.gradle.groovy.script[email protected]14236f4,
interface org.gradle.api.loggi[email protected]a6d0a4,
interface org.gradl[email protected]25fbfa,
interface org.gradle.api.execution.T[email protected]1c2074c}

Then it can not find 'listenerClass', it will create a new ListenerBroadcast, add 2 or 3 items to its 'broadcast' variable 's  handlers
and put the new object into 'broadcasters' queue.

broadcasters=
{interface org.gradle.api.execut[email protected]1160f3c,
interface org.gradle.groovy.script[email protected]14236f4,
interface [email protected]2d1f6d,   <---------------------new one
interface org.gradle.api.loggi[email protected]a6d0a4,
interface org.gradl[email protected]25fbfa,
interface org.gradle.api.execution.T[email protected]1c2074c}

script.run()之前,都会script.init(target, services),
这个target就是gradle实例,以后call这个clousre时候的receiver in callsite function.

in DefaultGradleLauncher.java
        // Evaluate init scripts
        initScriptHandler.executeScripts(gradle);
	->
		script.run ()
	->
		init_<a-z>.run()
		public Object run()
		{
			CallSite[] arrayOfCallSite = $getCallSiteArray();return arrayOfCallSite[0].call(arrayOfCallSite[1].callGroovyObjectGetProperty(this), new init_c69lrc3uiec789gk9t0jr56ux._run_closure1(this));return null;
		}
	->
	    public static Object defaultCall(CallSite callSite, Object receiver, Object[] args) throws Throwable {
			return createCallSite(callSite, receiver, args).call(receiver, args);
		}
		//receiver is our gradle instance, args is array of Closure objects ,
		//[0] = [[email protected]]
	-> DefaultGradle.java
		public void projectsLoaded(Closure closure) {
			buildListenerBroadcast.add(new ClosureBackedMethodInvocationDispatch("projectsLoaded", closure));
		}

	//In Constructor of DefaultGradleLauncher,  gradle.getBuildListenerBroadcaster() is paased to the constructor, and worked as
	//private final BuildListener buildListener;

	public BuildListener getBuildListenerBroadcaster() {
        return buildListenerBroadcast.getSource();
    }

时间: 2024-12-20 23:19:14

Gradle源码入门学习之十-- Listener & Dispatch的相关文章

gradle源码入门学习六

DefaultServiceRegistry之findProviderMethods private void findProviderMethods(Object target) { Set<String> methods = new HashSet<String>(); for (Class<?> type = target.getClass(); type != Object.class; type = type.getSuperclass()) { findDe

Gradle源码入门三

本文以在command窗口执行最简单的 gradle -v 为例子说明 从上文提到的EntryPoint.java开始,调用该类的doAction</p><p>实际调用到Main.java  的doAction <pre name="code" class="java"> protected void doAction(String[] args, ExecutionListener listener) { createActio

Gradle源码入门四

接三 如果我们运行gradle hello  (hello 是一个task) 代码会走到org.gradle.launcher.cli.RunBuildAction,  这个类的run()函数 public void run() { executer.execute( new ExecuteBuildAction(startParameter), new DefaultBuildCancellationToken(), new DefaultBuildActionParameters(clien

Gradle源码入门九-settings的include是如何工作的

先上call stack script.run()  in org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory 本代码中,script=settings_ani2pwefx3gv..... ->org.gradle.groovy.scripts.BasicScript.methodMissing(String name, Object params)  //name = 'include' params='sub_proje

Gradle源码入门八

启动的时候,调用了  launcher.cli.BuildActionsFactory.java private Runnable runBuildInProcess(StartParameter startParameter, DaemonParameters daemonParameters, ServiceRegistry loggingServices) { ServiceRegistry globalServices = ServiceRegistryBuilder.builder

Gradle源码入门五

分析DefaultGradleLauncher.java private void doBuildStages(Stage upTo) 函数的第二步 // Evaluate settings script SettingsInternal settings = settingsHandler.findAndLoadSettings(gradle); buildListener.settingsEvaluated(settings); 从 startParameter 开始 StartParame

如何用Android Studio查看build.gradle源码

上一篇博客里讲过 build.gradle 里的每一行代码基本都是在调用一个方法,既然是这样,我们就可以用 android studio(下面简称as) 去查看它源码的方法注释说明,这样就可以理解每个方法是做什么的了,就算是在大神的 build.gradle 遇见一些没看懂的代码,也可以点进去看方法介绍来理解.就像我们在查看 SDK 里的各种方法一样. 但是有个问题,应该也有小伙伴跟我一样吧,每次在大神的 build.gradle 看到一堆不懂的代码时,按住 Ctrl 键后,鼠标移到代码上时有个

手机自动化测试:appium源码分析之bootstrap十

手机自动化测试:appium源码分析之bootstrap十 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478. setText package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.cor

手机自动化测试:appium源码分析之bootstrap十二

手机自动化测试:appium源码分析之bootstrap十二 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478. ScrollTo package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.c