diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java index b990a86c41b75..72eda2708f2ba 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java @@ -1416,6 +1416,13 @@ private void addSyntheticBean(BeanInfo bean) { } void addSyntheticInterceptor(InterceptorInfo interceptor) { + for (InterceptorInfo i : interceptors) { + if (i.getIdentifier().equals(interceptor.getIdentifier())) { + throw new IllegalStateException( + "A synthetic interceptor with identifier " + interceptor.getIdentifier() + " is already registered: " + + i); + } + } interceptors.add(interceptor); } diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorConfigurator.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorConfigurator.java index 278bcaf369f03..6b56804b3bb08 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorConfigurator.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorConfigurator.java @@ -11,6 +11,7 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.Type; +import io.quarkus.arc.InjectableInterceptor; import io.quarkus.arc.InterceptorCreator; import io.quarkus.arc.processor.InjectionPointInfo.TypeAndQualifiers; @@ -24,6 +25,7 @@ public final class InterceptorConfigurator extends ConfiguratorBase injectionPoints; final Set bindings; + String identifier; int priority; InterceptorConfigurator(BeanDeployment beanDeployment, InterceptionType type) { @@ -39,6 +41,19 @@ public InterceptorConfigurator priority(int priority) { return this; } + /** + * The identifier becomes a part of the {@link InterceptorInfo#getIdentifier()} and + * {@link InjectableInterceptor#getIdentifier()}. An identifier can be used to register multiple synthetic interceptors with + * the same {@link InterceptorCreator} class. + * + * @param identifier + * @return self + */ + public InterceptorConfigurator identifier(String identifier) { + this.identifier = identifier; + return this; + } + public InterceptorConfigurator bindings(AnnotationInstance... bindings) { Collections.addAll(this.bindings, bindings); return this; @@ -53,7 +68,7 @@ public InterceptorConfigurator addInjectionPoint(Type requiredType, AnnotationIn public void creator(Class creatorClass) { beanDeployment.addSyntheticInterceptor(new InterceptorInfo(creatorClass, beanDeployment, bindings, - List.of(Injection.forSyntheticInterceptor(injectionPoints)), priority, type, params)); + List.of(Injection.forSyntheticInterceptor(injectionPoints)), priority, type, params, identifier)); } } diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java index af1f59b50230a..828ecdbc573d3 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java @@ -50,7 +50,7 @@ public class InterceptorInfo extends BeanInfo implements Comparable creatorClass, BeanDeployment beanDeployment, Set bindings, List injections, int priority, InterceptionType interceptionType, - Map params) { + Map params, String identifier) { super(null, ClassType.create(InterceptFunction.class), null, beanDeployment, BuiltinScope.DEPENDENT.getInfo(), Sets.singletonHashSet(Type.create(DotName.OBJECT_NAME, Kind.CLASS)), new HashSet<>(), injections, null, null, false, @@ -64,7 +64,7 @@ public class InterceptorInfo extends BeanInfo implements Comparable handle = Arc.container().instance(MyBean.class); assertEquals("ok", handle.get().ping()); handle.destroy(); - assertEquals(4, EVENTS.size()); + assertEquals(5, EVENTS.size()); assertEquals(TestAroundConstruct.class.getName(), EVENTS.get(0)); assertEquals(TestPostConstruct.class.getName(), EVENTS.get(1)); - assertEquals(TestAroundInvoke.class.getName(), EVENTS.get(2)); - assertEquals(TestPreDestroy.class.getName(), EVENTS.get(3)); + assertEquals(TestPostConstruct.class.getName(), EVENTS.get(2)); + assertEquals(TestAroundInvoke.class.getName(), EVENTS.get(3)); + assertEquals(TestPreDestroy.class.getName(), EVENTS.get(4)); } @SimpleBinding @@ -155,6 +156,13 @@ public void register(RegistrationContext context) { AnnotationInstance.builder(Intercepted.class).build()) .creator(TestPostConstruct.class); + context.configureInterceptor(InterceptionType.POST_CONSTRUCT) + .bindings(AnnotationInstance.builder(SimpleBinding.class).build()) + .addInjectionPoint(ParameterizedType.create(Bean.class, WildcardType.UNBOUNDED), + AnnotationInstance.builder(Intercepted.class).build()) + .identifier("foo") + .creator(TestPostConstruct.class); + context.configureInterceptor(InterceptionType.PRE_DESTROY) .bindings(AnnotationInstance.builder(SimpleBinding.class).build()) .addInjectionPoint(ParameterizedType.create(Bean.class, WildcardType.UNBOUNDED),