Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArC: add synthetic interceptors API #34138

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jakarta.enterprise.event.Reception;
import jakarta.enterprise.inject.spi.DefinitionException;
import jakarta.enterprise.inject.spi.DeploymentException;
import jakarta.enterprise.inject.spi.InterceptionType;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand Down Expand Up @@ -1414,6 +1415,10 @@ private void addSyntheticBean(BeanInfo bean) {
beans.add(bean);
}

void addSyntheticInterceptor(InterceptorInfo interceptor) {
interceptors.add(interceptor);
}

private void addSyntheticObserver(ObserverConfigurator configurator) {
observers.add(ObserverInfo.create(configurator.id, this, configurator.beanClass, null, null, null, null,
configurator.observedType,
Expand Down Expand Up @@ -1688,6 +1693,20 @@ public <T> BeanConfigurator<T> configure(DotName beanClassName) {
return new BeanConfigurator<T>(beanClassName, beanDeployment, this);
}

@Override
public InterceptorConfigurator configureInterceptor(InterceptionType interceptionType) {
switch (Objects.requireNonNull(interceptionType)) {
case AROUND_INVOKE:
case POST_CONSTRUCT:
case PRE_DESTROY:
case AROUND_CONSTRUCT:
return new InterceptorConfigurator(beanDeployment, interceptionType);
default:
throw new IllegalArgumentException("Unsuppored interception type: " + interceptionType);
}

}

@Override
public void accept(BeanInfo bean) {
beanDeployment.addSyntheticBean(bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ public BeanGenerator(AnnotationLiteralProcessor annotationLiterals, Predicate<Do
* @return a collection of resources
*/
Collection<Resource> generate(BeanInfo bean) {
if (bean.getTarget().isPresent()) {
if (bean.isSynthetic()) {
return generateSyntheticBean(bean);
} else {
AnnotationTarget target = bean.getTarget().get();
switch (target.kind()) {
case CLASS:
Expand All @@ -138,9 +140,6 @@ Collection<Resource> generate(BeanInfo bean) {
default:
throw new IllegalArgumentException("Unsupported bean type");
}
} else {
// Synthetic beans
return generateSyntheticBean(bean);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Collection;

import jakarta.enterprise.inject.spi.InterceptionType;

import org.jboss.jandex.DotName;

/**
Expand Down Expand Up @@ -39,6 +41,15 @@ default <T> BeanConfigurator<T> configure(Class<?> beanClass) {
return configure(DotName.createSimple(beanClass.getName()));
}

/**
* Configure a new synthetic interceptor. The interceptor is not added to the deployment unless the
* {@link InterceptorConfigurator#creator(Class)} method is called.
*
* @param interceptionType
* @return a new synthetic interceptor configurator
*/
InterceptorConfigurator configureInterceptor(InterceptionType interceptionType);

/**
* The returned stream contains all non-synthetic beans (beans derived from classes) and beans
* registered by other {@link BeanRegistrar}s before the stream is created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ public class Injection {
private static final Logger LOGGER = Logger.getLogger(Injection.class);

static Injection forSyntheticBean(Iterable<TypeAndQualifiers> injectionPoints) {
List<InjectionPointInfo> ips = new ArrayList<>();
return forSynthetic(injectionPoints, BeanType.SYNTHETIC_BEAN);
}

static Injection forSyntheticInterceptor(Iterable<TypeAndQualifiers> injectionPoints) {
return forSynthetic(injectionPoints, BeanType.SYNTHETIC_INTERCEPTOR);
}

private static Injection forSynthetic(Iterable<TypeAndQualifiers> injectionPoints, BeanType beanType) {
List<InjectionPointInfo> ret = new ArrayList<>();
for (TypeAndQualifiers injectionPoint : injectionPoints) {
InjectionPointInfo injectionPointInfo = InjectionPointInfo.fromSyntheticInjectionPoint(injectionPoint);
validateInjections(injectionPointInfo, BeanType.SYNTHETIC_BEAN);
ips.add(injectionPointInfo);
InjectionPointInfo ip = InjectionPointInfo.fromSyntheticInjectionPoint(injectionPoint);
validateInjections(ip, beanType);
ret.add(ip);
}
return new Injection(null, ips);
return new Injection(null, ret);
}

private static void validateInjections(InjectionPointInfo injectionPointInfo, BeanType beanType) {
Expand Down Expand Up @@ -78,7 +86,8 @@ private static void validateInjections(InjectionPointInfo injectionPointInfo, Be
// declaring the injection point
if (injectionPointInfo.getRequiredType().name().equals(DotNames.BEAN)
&& injectionPointInfo.getRequiredType().kind() == Type.Kind.PARAMETERIZED_TYPE
&& injectionPointInfo.getRequiredType().asParameterizedType().arguments().size() == 1) {
&& injectionPointInfo.getRequiredType().asParameterizedType().arguments().size() == 1
&& injectionPointInfo.hasDefaultedQualifier()) {
Type actualType = injectionPointInfo.getRequiredType().asParameterizedType().arguments().get(0);
AnnotationTarget ipTarget = injectionPointInfo.getTarget();
DotName expectedType = null;
Expand Down Expand Up @@ -255,6 +264,7 @@ static enum BeanType {
PRODUCER_METHOD,
SYNTHETIC_BEAN,
INTERCEPTOR,
SYNTHETIC_INTERCEPTOR,
DECORATOR
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkus.arc.processor;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.InterceptionType;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Type;

import io.quarkus.arc.InterceptorCreator;
import io.quarkus.arc.processor.InjectionPointInfo.TypeAndQualifiers;

/**
* This construct is not thread-safe.
*/
public final class InterceptorConfigurator extends ConfiguratorBase<InterceptorConfigurator> {

private final BeanDeployment beanDeployment;

final InterceptionType type;
final Set<TypeAndQualifiers> injectionPoints;
final Set<AnnotationInstance> bindings;
int priority;

InterceptorConfigurator(BeanDeployment beanDeployment, InterceptionType type) {
this.beanDeployment = beanDeployment;
this.type = type;
this.injectionPoints = new HashSet<>();
this.bindings = new HashSet<>();
this.priority = 1;
}

public InterceptorConfigurator priority(int priority) {
this.priority = priority;
return this;
}

public InterceptorConfigurator bindings(AnnotationInstance... bindings) {
Collections.addAll(this.bindings, bindings);
return this;
}

public InterceptorConfigurator addInjectionPoint(Type requiredType, AnnotationInstance... requiredQualifiers) {
this.injectionPoints.add(new TypeAndQualifiers(requiredType,
requiredQualifiers.length == 0 ? Set.of(AnnotationInstance.builder(Default.class).build())
: Set.of(requiredQualifiers)));
return this;
}

public void creator(Class<? extends InterceptorCreator> creatorClass) {
beanDeployment.addSyntheticInterceptor(new InterceptorInfo(creatorClass, beanDeployment, bindings,
List.of(Injection.forSyntheticInterceptor(injectionPoints)), priority, type, params));
}

}
Loading