Status: New
Owner: ----
New issue 702 by [email protected]: Private modules and aop interceptors
do not work together - test code in description
http://code.google.com/p/google-guice/issues/detail?id=702
package blah;
import static com.google.inject.matcher.Matchers.any;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.PrivateModule;
import com.google.inject.Injector;
import com.google.inject.Module;
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;
public class GuiceAopTesting {
private static final Module PRIVATE_WITH_ADVICE_AND_BINDING = new
PrivateModule() {
@Override
public void configure() {
expose(Intercepted.class);
bindInterceptor(any(), any(), new InterceptClass());
bind(Intercepted.class).to(MethodIntercepted.class);
}
};
private static final Module PRIVATE_WITH_ADVICE_ONLY = new
PrivateModule() {
@Override
public void configure() {
bindInterceptor(any(), any(), new InterceptClass());
}
};
private static final Module PRIVATE_WITH_BINDING = new PrivateModule() {
@Override
public void configure() {
expose(Intercepted.class);
bind(Intercepted.class).to(MethodIntercepted.class);
}
};
private static final Module ABSTRACT_WITH_ADVICE = new AbstractModule() {
@Override
public void configure() {
bindInterceptor(any(), any(), new InterceptClass());
}
};
private static final Module ABSTRACT_WITH_BINDING = new AbstractModule() {
@Override
public void configure() {
bind(Intercepted.class).to(MethodIntercepted.class);
}
};
private static final Module
PRIVATE_INSTALLING_ABSTRACT_WITH_ADVICE_AND_EXPOSING
= new PrivateModule() {
@Override
public void configure() {
install(ABSTRACT_WITH_ADVICE);
install(ABSTRACT_WITH_BINDING);
expose(Intercepted.class);
}
};
public static void main(String[] args) {
// Fails advices does not apply to the binding in the private module
System.out.println("CASE 1: PRIVATE DEFINING ADVICE AND BINDING\n");
print(Guice.createInjector(PRIVATE_WITH_ADVICE_AND_BINDING));
// Works!
System.out.println("CASE 2: ABSTRACT DEFINING ADVICE AND PRIVATE WITH
BINDING\n");
print(Guice.createInjector(ABSTRACT_WITH_ADVICE, PRIVATE_WITH_BINDING));
// Works!
System.out.println("CASE 3: ABSTRACT DEFINING ADVICE AND ABSTRACT
BINDING\n");
print(Guice.createInjector(ABSTRACT_WITH_ADVICE,
ABSTRACT_WITH_BINDING));
// Works advices from private module does not apply to the binding in
the abstract module
System.out.println("CASE 4: PRIVATE DEFINING ADVICE AND ABSTRACT
BINDING\n");
print(Guice.createInjector(PRIVATE_WITH_ADVICE_ONLY,
ABSTRACT_WITH_BINDING));
// Fails private kills testcase 3.
System.out.println("CASE 5: PRIVATE INSTALLING CASE #3 AND EXPOSING
IT\n");
print(Guice.createInjector(PRIVATE_INSTALLING_ABSTRACT_WITH_ADVICE_AND_EXPOSING));
}
private static void print(Injector injector) {
// Abstract binding interceptor installs private where interceptor
applies successfully
// Private binds interceptor binds target and target is not advise.
Intercepted intercepted = injector.getInstance(Intercepted.class);
intercepted.interceptedMethod();
System.out.println("\n");
MethodIntercepted methodIntercepted =
injector.getInstance(MethodIntercepted.class);
methodIntercepted.interceptedMethod();
methodIntercepted.packagePrivateInterceptedMethod();
methodIntercepted.protectedInterceptedMethod();
methodIntercepted.privateMethod();
methodIntercepted.finalMethod();
System.out.println("\n");
Method2Intercepted method2Intercepted =
injector.getInstance(Method2Intercepted.class);
method2Intercepted.interceptedMethod();
method2Intercepted.packagePrivateInterceptedMethod();
method2Intercepted.protectedInterceptedMethod();
method2Intercepted.finalMethod();
}
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");
}
@InterceptFinal
public final void finalMethod() {
System.out.println("finalMethod");
}
@InterceptPrivate
private void privateMethod() {
System.out.println("privateMethod");
}
}
public static class Method2Intercepted extends MethodIntercepted {
}
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})
@interface InterceptFinal {}
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})
@interface InterceptMethod {}
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})
@interface InterceptNothing {}
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})
@interface InterceptPrivate {}
public static class InterceptClass implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.print("Intercepted! ");
invocation.proceed();
return invocation;
}
}
}
--
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.