Skip to content
Browse files

Add an example with @NonBinding.

  • Loading branch information...
1 parent 9dafe06 commit 3970bf78e33411fe9b1d19e2de9ea6d196ededee @kevinpollet committed Jul 18, 2011
View
3 src/main/java/com/github/kevinpollet/sample/cdi/SampleExtension.java
@@ -18,6 +18,7 @@
import com.github.kevinpollet.sample.cdi.interceptor.Bar;
import com.github.kevinpollet.sample.cdi.interceptor.Foo;
import com.github.kevinpollet.sample.cdi.interceptor.FooInterceptor;
+import com.github.kevinpollet.sample.cdi.interceptor.FooLiteral;
import org.jboss.seam.solder.reflection.annotated.AnnotatedTypeBuilder;
import javax.enterprise.event.Observes;
@@ -44,7 +45,7 @@ void processFooInterceptorAnnotatedType(@Observes ProcessAnnotatedType<FooInterc
AnnotatedType<FooInterceptor> annotatedType = new AnnotatedTypeBuilder<FooInterceptor>()
.readFromType(event.getAnnotatedType())
.addToClass(new AnnotationLiteral<Interceptor>() {})
- .addToClass(new AnnotationLiteral<Foo>() {})
+ .addToClass(new FooLiteral())
.create();
event.setAnnotatedType(annotatedType);
View
3 src/main/java/com/github/kevinpollet/sample/cdi/interceptor/Foo.java
@@ -15,6 +15,7 @@
*/
package com.github.kevinpollet.sample.cdi.interceptor;
+import javax.enterprise.util.Nonbinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -26,5 +27,5 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
-
+ @Nonbinding boolean value() default true;
}
View
7 src/main/java/com/github/kevinpollet/sample/cdi/interceptor/FooInterceptor.java
@@ -25,6 +25,11 @@
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
- return "Foo interceptor called";
+ Foo fooAnnotation = context.getMethod().getAnnotation(Foo.class);
+
+ if (fooAnnotation.value() == true) {
+ return "Foo interceptor called";
+ }
+ return context.proceed();
}
}
View
27 src/main/java/com/github/kevinpollet/sample/cdi/interceptor/FooLiteral.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2011 Kevin Pollet
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.kevinpollet.sample.cdi.interceptor;
+
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * @author Kevin Pollet
+ */
+public class FooLiteral extends AnnotationLiteral<Foo> implements Foo{
+ public boolean value() {
+ return false;
+ }
+}
View
5 src/test/java/com/github/kevinpollet/sample/cdi/test/interceptor/FooInterceptorTest.java
@@ -16,6 +16,7 @@
package com.github.kevinpollet.sample.cdi.test.interceptor;
import com.github.kevinpollet.sample.cdi.interceptor.Foo;
+import com.github.kevinpollet.sample.cdi.interceptor.FooLiteral;
import com.github.kevinpollet.sample.cdi.test.util.Deployments;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
@@ -52,10 +53,12 @@ public static JavaArchive deployment() {
@Test
public void testFooInterceptor() {
String hi = greetingService.hi("Kevin");
+ String hey = greetingService.hey("Kevin");
String bye = greetingService.bye("Kevin");
- Assert.assertEquals(1, beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, new AnnotationLiteral<Foo>() {}).size());
+ Assert.assertEquals(1, beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, new FooLiteral()).size());
Assert.assertEquals("Foo interceptor called", hi);
+ Assert.assertEquals("Hey Kevin", hey);
Assert.assertEquals("Bye Kevin", bye);
}
}
View
5 src/test/java/com/github/kevinpollet/sample/cdi/test/interceptor/GreetingService.java
@@ -28,6 +28,11 @@ public String hi(String user) {
return "Hi " + user;
}
+ @Foo(false)
+ public String hey(String user) {
+ return "Hey " + user;
+ }
+
@Bar
public String hello(String user) {
return "Hello " + user;

0 comments on commit 3970bf7

Please sign in to comment.
Something went wrong with that request. Please try again.