@Test public void testAddMethodListener() throws Exception { final DataSource actual = mock(DataSource.class); doReturn(mock(Connection.class)).when(actual).getConnection(); final RegexProxyFactory<DataSource> factory = new RegexProxyFactory<DataSource>(DataSource.class); factory.addMethodListener("getConnection", new RegexMethodInterceptorAdapter<DataSource>() { @Override public void afterCall(RegexMethodInterceptorEvent<DataSource> evt) { assertThat(evt.getException(), is(CoreMatchers.<Object>nullValue())); assertThat(evt.getReturnedValue(), is(CoreMatchers.<Object>notNullValue())); assertThat(evt.getSource(), is(CoreMatchers.<Object>notNullValue())); assertThat(evt.getTarget(), is(CoreMatchers.<Object>notNullValue())); assertThat(actual, sameInstance(evt.getInvocation().getThis())); assertNotNull(evt.getInvocation().getStaticPart()); } }); DataSource proxy = factory.getProxy(actual); proxy.getConnection(); proxy.getConnection(); assertThat(proxy, not(sameInstance(actual))); verify(actual, times(2)).getConnection(); }
public void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener) { if(listener == null) { throw new IllegalArgumentException("listener cannot be null."); } Set<RegexMethodInterceptorListener<T>> set = listeners.get(regex); if(set == null) { set = new HashSet<RegexMethodInterceptorListener<T>>(); listeners.put(regex, set); } set.add(listener); }
RegexProxyFactory implements ProxyAware<T> { public void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener) { if(listener == null) { throw new IllegalArgumentException("listener cannot be null."); } Set<RegexMethodInterceptorListener<T>> set = listeners.get(regex); if(set == null) { set = new HashSet<RegexMethodInterceptorListener<T>>(); listeners.put(regex, set); } set.add(listener); } }
RegexProxyFactory implements ProxyAware<T> { public void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener) { if(listener == null) { throw new IllegalArgumentException("listener cannot be null."); } Set<RegexMethodInterceptorListener<T>> set = listeners.get(regex); if(set == null) { set = new HashSet<RegexMethodInterceptorListener<T>>(); listeners.put(regex, set); } set.add(listener); } RegexProxyFactory(Class<T> clazz); }
RegexProxyFactory implements ProxyAware<T> { public void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener) { if(listener == null) { throw new IllegalArgumentException("listener cannot be null."); } Set<RegexMethodInterceptorListener<T>> set = listeners.get(regex); if(set == null) { set = new HashSet<RegexMethodInterceptorListener<T>>(); listeners.put(regex, set); } set.add(listener); } RegexProxyFactory(Class<T> clazz); T getProxy(T object); @SuppressWarnings("unchecked") ProxyInterceptorAware<T> getProxyInterceptor(T object); void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener); }
RegexProxyFactory implements ProxyAware<T> { public void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener) { if(listener == null) { throw new IllegalArgumentException("listener cannot be null."); } Set<RegexMethodInterceptorListener<T>> set = listeners.get(regex); if(set == null) { set = new HashSet<RegexMethodInterceptorListener<T>>(); listeners.put(regex, set); } set.add(listener); } RegexProxyFactory(Class<T> clazz); T getProxy(T object); @SuppressWarnings("unchecked") ProxyInterceptorAware<T> getProxyInterceptor(T object); void addMethodListener(String regex, RegexMethodInterceptorListener<T> listener);  }