Skip to content

Commit afbb315

Browse files
authored
Merge pull request #34219 from mkouba/arc-interceptor-configurator-id
ArC: add InterceptorConfigurator#identifier() method
2 parents 94c34c2 + fbf1465 commit afbb315

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,13 @@ private void addSyntheticBean(BeanInfo bean) {
14161416
}
14171417

14181418
void addSyntheticInterceptor(InterceptorInfo interceptor) {
1419+
for (InterceptorInfo i : interceptors) {
1420+
if (i.getIdentifier().equals(interceptor.getIdentifier())) {
1421+
throw new IllegalStateException(
1422+
"A synthetic interceptor with identifier " + interceptor.getIdentifier() + " is already registered: "
1423+
+ i);
1424+
}
1425+
}
14191426
interceptors.add(interceptor);
14201427
}
14211428

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorConfigurator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.jboss.jandex.AnnotationInstance;
1212
import org.jboss.jandex.Type;
1313

14+
import io.quarkus.arc.InjectableInterceptor;
1415
import io.quarkus.arc.InterceptorCreator;
1516
import io.quarkus.arc.processor.InjectionPointInfo.TypeAndQualifiers;
1617

@@ -24,6 +25,7 @@ public final class InterceptorConfigurator extends ConfiguratorBase<InterceptorC
2425
final InterceptionType type;
2526
final Set<TypeAndQualifiers> injectionPoints;
2627
final Set<AnnotationInstance> bindings;
28+
String identifier;
2729
int priority;
2830

2931
InterceptorConfigurator(BeanDeployment beanDeployment, InterceptionType type) {
@@ -39,6 +41,19 @@ public InterceptorConfigurator priority(int priority) {
3941
return this;
4042
}
4143

44+
/**
45+
* The identifier becomes a part of the {@link InterceptorInfo#getIdentifier()} and
46+
* {@link InjectableInterceptor#getIdentifier()}. An identifier can be used to register multiple synthetic interceptors with
47+
* the same {@link InterceptorCreator} class.
48+
*
49+
* @param identifier
50+
* @return self
51+
*/
52+
public InterceptorConfigurator identifier(String identifier) {
53+
this.identifier = identifier;
54+
return this;
55+
}
56+
4257
public InterceptorConfigurator bindings(AnnotationInstance... bindings) {
4358
Collections.addAll(this.bindings, bindings);
4459
return this;
@@ -53,7 +68,7 @@ public InterceptorConfigurator addInjectionPoint(Type requiredType, AnnotationIn
5368

5469
public void creator(Class<? extends InterceptorCreator> creatorClass) {
5570
beanDeployment.addSyntheticInterceptor(new InterceptorInfo(creatorClass, beanDeployment, bindings,
56-
List.of(Injection.forSyntheticInterceptor(injectionPoints)), priority, type, params));
71+
List.of(Injection.forSyntheticInterceptor(injectionPoints)), priority, type, params, identifier));
5772
}
5873

5974
}

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class InterceptorInfo extends BeanInfo implements Comparable<InterceptorI
5050

5151
InterceptorInfo(Class<? extends InterceptorCreator> creatorClass, BeanDeployment beanDeployment,
5252
Set<AnnotationInstance> bindings, List<Injection> injections, int priority, InterceptionType interceptionType,
53-
Map<String, Object> params) {
53+
Map<String, Object> params, String identifier) {
5454
super(null, ClassType.create(InterceptFunction.class), null, beanDeployment, BuiltinScope.DEPENDENT.getInfo(),
5555
Sets.singletonHashSet(Type.create(DotName.OBJECT_NAME, Kind.CLASS)), new HashSet<>(), injections, null,
5656
null, false,
@@ -64,7 +64,7 @@ public class InterceptorInfo extends BeanInfo implements Comparable<InterceptorI
6464
creatorClass.getName() + "#create() must not return null");
6565
mc.returnValue(ret);
6666
},
67-
null, params, true, false, null, priority, creatorClass.getName());
67+
null, params, true, false, null, priority, creatorClass.getName() + (identifier != null ? identifier : ""));
6868
this.bindings = bindings;
6969
this.interceptionType = interceptionType;
7070
this.creatorClass = creatorClass;

independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/buildextension/beans/SyntheticInterceptorTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ public void testSyntheticInterceptor() {
4747
InstanceHandle<MyBean> handle = Arc.container().instance(MyBean.class);
4848
assertEquals("ok", handle.get().ping());
4949
handle.destroy();
50-
assertEquals(4, EVENTS.size());
50+
assertEquals(5, EVENTS.size());
5151
assertEquals(TestAroundConstruct.class.getName(), EVENTS.get(0));
5252
assertEquals(TestPostConstruct.class.getName(), EVENTS.get(1));
53-
assertEquals(TestAroundInvoke.class.getName(), EVENTS.get(2));
54-
assertEquals(TestPreDestroy.class.getName(), EVENTS.get(3));
53+
assertEquals(TestPostConstruct.class.getName(), EVENTS.get(2));
54+
assertEquals(TestAroundInvoke.class.getName(), EVENTS.get(3));
55+
assertEquals(TestPreDestroy.class.getName(), EVENTS.get(4));
5556
}
5657

5758
@SimpleBinding
@@ -155,6 +156,13 @@ public void register(RegistrationContext context) {
155156
AnnotationInstance.builder(Intercepted.class).build())
156157
.creator(TestPostConstruct.class);
157158

159+
context.configureInterceptor(InterceptionType.POST_CONSTRUCT)
160+
.bindings(AnnotationInstance.builder(SimpleBinding.class).build())
161+
.addInjectionPoint(ParameterizedType.create(Bean.class, WildcardType.UNBOUNDED),
162+
AnnotationInstance.builder(Intercepted.class).build())
163+
.identifier("foo")
164+
.creator(TestPostConstruct.class);
165+
158166
context.configureInterceptor(InterceptionType.PRE_DESTROY)
159167
.bindings(AnnotationInstance.builder(SimpleBinding.class).build())
160168
.addInjectionPoint(ParameterizedType.create(Bean.class, WildcardType.UNBOUNDED),

0 commit comments

Comments
 (0)