Open GoogleCodeExporter opened 9 years ago
package blah; import static com.google.inject.matcher.Matchers.annotatedWith; import static com.google.inject.matcher.Matchers.any; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Inject; import com.google.inject.Key; import com.google.inject.Provider; import com.google.inject.matcher.Matcher; import com.google.inject.Module; import com.google.inject.TypeLiteral; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedElement; public class GuiceAopTesting { private static final Module ABSTRACT_WITH_ADVICE = new AbstractModule() { @Override public void configure() { bindInterceptor(any(), METHOD_MATCHER, new InterceptClass()); } }; private static final Module INSTALLING_ABSTRACT_ADVICE_WITH_PROVIDER_BINDING = new AbstractModule() { @Override public void configure() { install(ABSTRACT_WITH_ADVICE); bind(Intercepted.class).toProvider(InterceptedProvider.class); } }; public static void main(String[] args) { print(Guice.createInjector(INSTALLING_ABSTRACT_ADVICE_WITH_PROVIDER_BINDING)); } private static void print(Injector injector) { Provider<Intercepted> providerInterceptor = injector.getInstance(Key.get(new TypeLiteral<Provider<Intercepted>>() {})); Intercepted intercepted = providerInterceptor.get(); intercepted.interceptedMethod(); } private static final Matcher<AnnotatedElement> METHOD_MATCHER = annotatedWith(InterceptMethod.class); public interface Intercepted { public void interceptedMethod(); } public static class MethodIntercepted implements Intercepted { @Override @InterceptMethod public void interceptedMethod() { System.out.println("interceptedMethod"); } @InterceptMethod protected void protectedInterceptedMethod() { System.out.println("protectedMethod"); } @InterceptMethod void packagePrivateInterceptedMethod() { System.out.println("packagePrivateMethod"); } } @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @interface InterceptMethod {} public static class InterceptClass implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.print("Intercepted! "); invocation.proceed(); return invocation; } } public static class InterceptedProvider implements Provider<MethodIntercepted> { private final MethodIntercepted methodIntercepted; @Inject public InterceptedProvider(MethodIntercepted methodIntercepted) { this.methodIntercepted = methodIntercepted; } @Override @InterceptMethod public MethodIntercepted get() { System.out.println("Providing intercepted methods"); return methodIntercepted; } } }
Original issue reported on code.google.com by djwh...@google.com on 7 May 2012 at 7:50
djwh...@google.com
Original issue reported on code.google.com by
djwh...@google.com
on 7 May 2012 at 7:50