@Test public void synchronizes_all_methods_on_the_lock_given_as_parameter() { SpyOutputStream spy = new SpyOutputStream(); PrintStream printStream = SynchronizedPrintStream.create(spy, Charset.defaultCharset(), lock); printStream.println("foo"); assertThat("was called", spy.wasCalled, is(true)); assertThat("used the lock", spy.lockWasHeldByCurrentThread, is(true)); }
public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); }
SynchronizedPrintStream { public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); } }
SynchronizedPrintStream { public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); }  }
SynchronizedPrintStream { public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); }  static PrintStream create(OutputStream out, Charset charset, Object sharedLock); }
SynchronizedPrintStream { public static PrintStream create(OutputStream out, Charset charset, Object sharedLock) { Enhancer e = new Enhancer(); e.setSuperclass(PrintStream.class); e.setCallback(new SynchronizedMethodInterceptor(sharedLock)); e.setNamingPolicy(new PrefixOverridingNamingPolicy(SynchronizedPrintStream.class.getName())); e.setUseFactory(false); return (PrintStream) e.create( new Class[]{OutputStream.class, boolean.class, String.class}, new Object[]{out, false, charset.name()} ); }  static PrintStream create(OutputStream out, Charset charset, Object sharedLock);  }