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=org.gradle.listener.ListenerBroadcast@1160f3c, interface org.gradle.groovy.scripts.ScriptExecutionListener=org.gradle.listener.ListenerBroadcast@14236f4, interface org.gradle.api.logging.StandardOutputListener=org.gradle.listener.ListenerBroadcast@a6d0a4, interface org.gradle.profile.ProfileListener=org.gradle.listener.ListenerBroadcast@25fbfa, interface org.gradle.api.execution.TaskExecutionGraphListener=org.gradle.listener.ListenerBroadcast@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.execution.TaskExecutionListener=org.gradle.listener.ListenerBroadcast@1160f3c, interface org.gradle.groovy.scripts.ScriptExecutionListener=org.gradle.listener.ListenerBroadcast@14236f4, interface org.gradle.BuildListener=org.gradle.listener.ListenerBroadcast@12d1f6d, <---------------------new one interface org.gradle.api.logging.StandardOutputListener=org.gradle.listener.ListenerBroadcast@a6d0a4, interface org.gradle.profile.ProfileListener=org.gradle.listener.ListenerBroadcast@25fbfa, interface org.gradle.api.execution.TaskExecutionGraphListener=org.gradle.listener.ListenerBroadcast@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] = [init_c69lrc3uiec789gk9t0jr56ux$_run_closure1@3701e9] -> 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(); }