Author: jcarman Date: Tue Jun 12 03:12:50 2012 New Revision: 1349104 URL: http://svn.apache.org/viewvc?rev=1349104&view=rev Log: Performance optimization. Using private, static, named inner classes where possible as opposed to anonymous inner classes.
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java?rev=1349104&r1=1349103&r2=1349104&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/AnnotationFactory.java Tue Jun 12 03:12:50 2012 @@ -208,54 +208,13 @@ public class AnnotationFactory { private static final ThreadLocal<Object> CONFIGURER = new ThreadLocal<Object>(); - private final StubConfigurer<Annotation> sharedConfigurer = new StubConfigurer<Annotation>() { - - /** - * {@inheritDoc} - */ - @Override - public Class<Annotation> getStubType() { - /* - * Suppress the warning because we are using this shared object in - * a very special, albeit peculiar, way, and effectively - * hijacking the only place where the generic type of the - * instance matters: namely, providing the type of - * Annotation to be stubbed at any given time. - */ - @SuppressWarnings("unchecked") - Class<Annotation> result = (Class<Annotation>) AnnotationFactory.getStubType(); - return result; - } - - /** - * {@inheritDoc} - */ - @Override - protected void configure(Annotation stub) { - when(stub.annotationType()).thenReturn(getStubType()); - Object o = CONFIGURER.get(); - if (o instanceof StubConfigurer<?>) { - @SuppressWarnings("unchecked") - final StubConfigurer<Annotation> configurer = (StubConfigurer<Annotation>) o; - configurer.configure(requireStubInterceptor(), stub); - } - } - }; - private ProxyFactory proxyFactory; /** * Create a new AnnotationFactory instance. */ public AnnotationFactory() { - this.proxyFactory = new StubProxyFactory(PROXY_FACTORY, sharedConfigurer) { - /** - * {@inheritDoc} - */ - protected boolean acceptsValue(Method m, Object o) { - return !(m.getDeclaringClass().isAnnotation() && o == null); - } - }; + this.proxyFactory = new AnnotationStubProxyFactory(AnnotationFactory.PROXY_FACTORY, new SharedConfigurer()); } /** @@ -453,4 +412,52 @@ public class AnnotationFactory { final StubConfigurer<A> configurer = (StubConfigurer<A>) o; return configurer.getStubType(); } + + private static class SharedConfigurer extends StubConfigurer<Annotation> { + + /** + * {@inheritDoc} + */ + @Override + public Class<Annotation> getStubType() { + /* + * Suppress the warning because we are using this shared object in + * a very special, albeit peculiar, way, and effectively + * hijacking the only place where the generic type of the + * instance matters: namely, providing the type of + * Annotation to be stubbed at any given time. + */ + @SuppressWarnings("unchecked") + Class<Annotation> result = (Class<Annotation>) AnnotationFactory.getStubType(); + return result; + } + + /** + * {@inheritDoc} + */ + @Override + protected void configure(Annotation stub) { + when(stub.annotationType()).thenReturn(getStubType()); + Object o = CONFIGURER.get(); + if (o instanceof StubConfigurer<?>) { + @SuppressWarnings("unchecked") + final StubConfigurer<Annotation> configurer = (StubConfigurer<Annotation>) o; + configurer.configure(requireStubInterceptor(), stub); + } + } + } + + private static class AnnotationStubProxyFactory extends StubProxyFactory { + + private AnnotationStubProxyFactory(ProxyFactory proxyFactory, StubConfigurer<Annotation> sharedConfigurer) { + super(proxyFactory, sharedConfigurer); + } + + /** + * {@inheritDoc} + */ + protected boolean acceptsValue(Method m, Object o) { + return !(m.getDeclaringClass().isAnnotation() && o == null); + } + } } Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java?rev=1349104&r1=1349103&r2=1349104&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java (original) +++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubInterceptor.java Tue Jun 12 03:12:50 2012 @@ -185,14 +185,7 @@ abstract class StubInterceptor implement InvocationMatcher invocationMatcher; //TODO use an approach like that of Mockito wrt capturing arg matchers, falling back to force equality like so: final RecordedInvocation recordedInvocation = currentInvocation; - invocationMatcher = new InvocationMatcher() { - - public boolean matches(Invocation invocation) { - return invocation.getMethod().getName().equals(recordedInvocation.getInvokedMethod().getName()) - && Arrays.equals(invocation.getArguments(), recordedInvocation.getArguments()); - } - - }; + invocationMatcher = new RecordedInvocationMatcher(recordedInvocation); //add to beginning, for priority, hence "stack" nomenclature: matchingResultStack.add(0, Pair.of(invocationMatcher, result)); } @@ -232,4 +225,19 @@ abstract class StubInterceptor implement protected boolean acceptsValue(Method m, Object o) { return TypeUtils.isInstance(o, m.getReturnType()); } + + private static class RecordedInvocationMatcher implements InvocationMatcher { + + private final RecordedInvocation recordedInvocation; + + private RecordedInvocationMatcher(RecordedInvocation recordedInvocation) { + this.recordedInvocation = recordedInvocation; + } + + public boolean matches(Invocation invocation) { + return invocation.getMethod().getName().equals(recordedInvocation.getInvokedMethod().getName()) + && Arrays.equals(invocation.getArguments(), recordedInvocation.getArguments()); + } + + } } \ No newline at end of file