Revision: 1542
Author:   sberlin
Date:     Thu Apr 21 11:17:15 2011
Log: specify that interceptors are called in the order they are listed in bindInterceptor(Matcher, Matcher, Interceptor...). add a test that guarantees it.
http://code.google.com/p/google-guice/source/detail?r=1542

Modified:
 /trunk/core/src/com/google/inject/Binder.java
 /trunk/core/test/com/google/inject/MethodInterceptionTest.java

=======================================
--- /trunk/core/src/com/google/inject/Binder.java       Tue Dec 14 06:08:21 2010
+++ /trunk/core/src/com/google/inject/Binder.java       Thu Apr 21 11:17:15 2011
@@ -216,7 +216,8 @@
    *     example: {@code only(Runnable.class)}.
* @param methodMatcher matches methods the interceptor should apply to. For
    *     example: {@code annotatedWith(Transactional.class)}.
-   * @param interceptors to bind
+ * @param interceptors to bind. The interceptors are called in the order they
+   *     are given.
    */
   void bindInterceptor(Matcher<? super Class<?>> classMatcher,
       Matcher<? super Method> methodMatcher,
=======================================
--- /trunk/core/test/com/google/inject/MethodInterceptionTest.java Wed Apr 20 09:57:24 2011 +++ /trunk/core/test/com/google/inject/MethodInterceptionTest.java Thu Apr 21 11:17:15 2011
@@ -19,6 +19,7 @@
 import com.google.inject.internal.util.ImmutableList;
 import com.google.inject.internal.util.ImmutableMap;
 import com.google.inject.internal.util.Iterables;
+import com.google.inject.internal.util.Lists;
 import com.google.inject.matcher.AbstractMatcher;
 import com.google.inject.matcher.Matchers;
 import static com.google.inject.matcher.Matchers.only;
@@ -264,5 +265,38 @@
       RetType aMethod(RetType obj);
   }
public static class Impl extends Superclass<RetType> implements Interface {
-  }
-}
+  }
+
+  public void testInterceptionOrder() {
+    final List<String> callList = Lists.newArrayList();
+    Injector injector = Guice.createInjector(new AbstractModule() {
+      protected void configure() {
+        bindInterceptor(Matchers.any(), Matchers.any(),
+          new NamedInterceptor("a", callList),
+          new NamedInterceptor("b", callList),
+          new NamedInterceptor("c", callList));
+      }
+    });
+
+ Interceptable interceptable = injector.getInstance(Interceptable.class);
+    assertEquals(0, callList.size());
+    interceptable.foo();
+    assertEquals(Arrays.asList("a", "b", "c"), callList);
+  }
+
+  private final class NamedInterceptor implements MethodInterceptor {
+    private final String name;
+    final List<String> called;
+
+    NamedInterceptor(String name, List<String> callList) {
+      this.name = name;
+      this.called = callList;
+    }
+
+ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
+      called.add(name);
+      return methodInvocation.proceed();
+    }
+  }
+
+}

--
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.

Reply via email to