-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: fix inheritance of interceptor binding annotations
Class-level interceptor bindings declared on a superclass should only be taken into account when they are `@Inherited`. ArC however inherits all interceptor bindings from superclasses, even if they are _not_ declared `@Inherited`. This commit fixes that in the strict mode, to not affect Quarkus security (which assumes that security annotations are inherited, even if they are not). Method-level annotations are never inherited. This means that the lowest override of a method determines whether method-level interceptor binding annotations are present. ArC used to behave differently. If a method on a class had no interceptor binding annotations but overrode a method from a superclass that _did_ have interceptor binding annotations, the method-level annotations from the superclass were inherited. This commit fixes that.
- Loading branch information
Showing
6 changed files
with
187 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../java/io/quarkus/arc/test/interceptors/bindings/inherited/InheritedBindingOnBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.quarkus.arc.test.interceptors.bindings.inherited; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.inject.Singleton; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InheritedBindingOnBeanTest { | ||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer.Builder() | ||
.beanClasses(MyBean.class, FooBinding.class, BarBinding.class, FooInterceptor.class, BarInterceptor.class) | ||
.strictCompatibility(true) // correct interceptor binding inheritance | ||
.build(); | ||
|
||
@Test | ||
public void testInterception() { | ||
MyBean bean = Arc.container().instance(MyBean.class).get(); | ||
assertNotNull(bean); | ||
bean.doSomething(); | ||
assertTrue(FooInterceptor.intercepted); | ||
assertFalse(BarInterceptor.intercepted); | ||
} | ||
|
||
@FooBinding | ||
@BarBinding | ||
static class MySuperclass { | ||
} | ||
|
||
@Singleton | ||
static class MyBean extends MySuperclass { | ||
void doSomething() { | ||
} | ||
} | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
@Inherited | ||
@interface FooBinding { | ||
} | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
// not @Inherited | ||
@interface BarBinding { | ||
} | ||
|
||
@FooBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class FooInterceptor { | ||
static boolean intercepted = false; | ||
|
||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
intercepted = true; | ||
return ctx.proceed(); | ||
} | ||
} | ||
|
||
@BarBinding | ||
@Interceptor | ||
@Priority(1) | ||
static class BarInterceptor { | ||
static boolean intercepted = false; | ||
|
||
@AroundInvoke | ||
Object intercept(InvocationContext ctx) throws Exception { | ||
intercepted = true; | ||
return ctx.proceed(); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../arc/test/interceptors/bindings/inherited/InheritedMethodsWithInterceptorBindingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.arc.test.interceptors.bindings.inherited; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
|
||
public class InheritedMethodsWithInterceptorBindingTest { | ||
@RegisterExtension | ||
ArcTestContainer container = new ArcTestContainer(MyBean.class, MyInterceptorBinding.class, MyInterceptor.class); | ||
|
||
@Test | ||
public void test() { | ||
MyBean bean = Arc.container().instance(MyBean.class).get(); | ||
assertEquals("foobar", bean.foobar()); | ||
assertEquals("intercepted: foobar", bean.foobarNotInherited()); | ||
} | ||
|
||
static class MySuperclass { | ||
@MyInterceptorBinding | ||
String foobar() { | ||
return "this should be ignored"; | ||
} | ||
|
||
@MyInterceptorBinding | ||
String foobarNotInherited() { | ||
return "foobar"; | ||
} | ||
} | ||
|
||
@Dependent | ||
static class MyBean extends MySuperclass { | ||
@Override | ||
String foobar() { | ||
return "foobar"; | ||
} | ||
} | ||
|
||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
@interface MyInterceptorBinding { | ||
} | ||
|
||
@MyInterceptorBinding | ||
@Priority(1) | ||
@Interceptor | ||
static class MyInterceptor { | ||
@AroundInvoke | ||
public Object intercept(InvocationContext ctx) throws Exception { | ||
return "intercepted: " + ctx.proceed(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters