Provider binding get method is intercepted but fails with a guice provision error
- Dominant language
- Java
- Stars
- 12.7k
- Forks
- 1.7k
- Avg merge
- 11m
- Merged PRs (30d)
- 2
Description
_From [djwhang@google.com](https://code.google.com/u/105606863396118216395/) on May 07, 2012 15:50:23_
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: http://code.google.com/p/google-guice/issues/detail?id=703_
Contributor guide
Assessment
This issue has not been assessed yet.