google / google/guice

Method interception does not work if another thread has access to object in constructor

Open
#1,070 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

The unit test below reproduces this behavior:
---------------- GuiceAopTest.java ---------------
public class GuiceAopTest {
@Test
public void testGuiceAop(){
Injector injector = Guice.createInjector(new MyModule());
MyService myService = injector.getInstance(MyService.class);
Assert.assertTrue("myService.isIntercepted() should return true", myService.isIntercepted());
}
}

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface MyAnnotation {}
class MyModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(MyAnnotation.class), new MyInterceptor());
}
}

class MyInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return true;
}
}

class MyService{
@Inject
public MyService() {
for(int i=0;i<10;i++){
//this line breaks test
Executors.newCachedThreadPool().execute(()->isIntercepted());
}
}
@MyAnnotation
public boolean isIntercepted(){
return false;
}
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.