### Question:
BuildSessionImpl implements BuildSession { @NonNull @VisibleForTesting static BuildSession createBuildSessionSingleton(@NonNull String pluginVersion) { Object buildSessionSingleton = Verify.verifyNotNull( new JvmWideVariable<>( BuildSessionImpl.class.getName(), BuildSessionImpl.class.getSimpleName(), pluginVersion, TypeToken.of(Object.class), BuildSessionImpl::new) .get()); if (buildSessionSingleton instanceof BuildSession) { return (BuildSession) buildSessionSingleton; } else { return (BuildSession) Proxy.newProxyInstance( BuildSession.class.getClassLoader(), new Class[] {BuildSession.class}, new DelegateInvocationHandler(buildSessionSingleton)); } }  @NonNull static BuildSession getSingleton(); @Override synchronized void initialize(@NonNull Gradle gradle); @Override synchronized void executeOnce(
            @NonNull String actionGroup, @NonNull String actionName, @NonNull Runnable action); @Override synchronized void executeOnceWhenBuildFinished(
            @NonNull String actionGroup, @NonNull String actionName, @NonNull Runnable action); @Override @SuppressWarnings("GuardedBy") String toString();  }### Answer:
@Test public void testCreateBuildSessionSingleton() throws Exception { BuildSession singleton = BuildSessionImpl.createBuildSessionSingleton(fakePluginVersion); assertThat(singleton).isInstanceOf(BuildSessionImpl.class); BuildSession sameSingleton = BuildSessionImpl.createBuildSessionSingleton(fakePluginVersion); assertThat(sameSingleton).isSameAs(singleton); BuildSession differentSingleton = BuildSessionImpl.createBuildSessionSingleton(fakePluginVersion2); assertThat(differentSingleton).isInstanceOf(BuildSessionImpl.class); assertThat(differentSingleton).isNotSameAs(singleton); MultiClassLoader classLoader = new MultiClassLoader( ImmutableList.of( BuildSession.class.getName(), BuildSessionImpl.class.getName())); List<Class<?>> classes = classLoader.load(); Class<?> buildSessionInterface = classes.get(0); Class<?> buildSessionImplClass = classes.get(1); assertThat(buildSessionInterface.getClassLoader()) .isNotSameAs(BuildSession.class.getClassLoader()); assertThat(buildSessionImplClass.getClassLoader()) .isNotSameAs(BuildSessionImpl.class.getClassLoader()); Method createBuildSessionSingletonMethod = buildSessionImplClass.getDeclaredMethod( "createBuildSessionSingleton", String.class); createBuildSessionSingletonMethod.setAccessible(true); Object proxyToSingleton = createBuildSessionSingletonMethod.invoke(null, fakePluginVersion); assertThat(proxyToSingleton).isInstanceOf(buildSessionInterface); assertThat(proxyToSingleton).isNotInstanceOf(buildSessionImplClass); assertThat(proxyToSingleton).isNotSameAs(singleton); Object delegateInvocationHandler = Proxy.getInvocationHandler(proxyToSingleton); Method getDelegateMethod = delegateInvocationHandler.getClass().getMethod("getDelegate"); getDelegateMethod.setAccessible(true); Object anotherSameSingleton = getDelegateMethod.invoke(delegateInvocationHandler); assertThat(anotherSameSingleton).isSameAs(singleton); }