Status: New
Owner: ----
New issue 703 by [email protected]: Provider binding get method is
intercepted but fails with a guice provision error
http://code.google.com/p/google-guice/issues/detail?id=703
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;
}
}
}
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.