Skip to content

Commit

Permalink
ArC: remove deprecated @AlternativePriority
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Feb 9, 2023
1 parent dbd6cb4 commit 86c7988
Show file tree
Hide file tree
Showing 23 changed files with 65 additions and 103 deletions.
6 changes: 1 addition & 5 deletions docs/src/main/asciidoc/cdi-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,10 @@ NOTE: Properties set at runtime have absolutely no effect on the bean resolution
In CDI, an alternative bean may be selected either globally for an application by means of `@Priority`, or for a bean archive using a `beans.xml` descriptor.
Quarkus has a simplified bean discovery and the content of `beans.xml` is ignored.

The disadvantage of `@jakarta.annotation.Priority` is that it has `@Target({ TYPE, PARAMETER })` and so it cannot be used for producer methods and fields.
This problem should be fixed in Common Annotations 2.1.
Users are encouraged to use `@io.quarkus.arc.Priority` instead, until Quarkus upgrades to this version of `jakarta.annotation-api`.

However, it is also possible to select alternatives for an application using the unified configuration.
The `quarkus.arc.selected-alternatives` property accepts a list of string values that are used to match alternative beans.
If any value matches then the priority of `Integer#MAX_VALUE` is used for the relevant bean.
The priority declared via `@Priority` or `@AlternativePriority` is overridden.
The priority declared via `@Priority` or inherited from a stereotype is overridden.

.Value Examples
|===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ If it is the case in your production then it can be recommended that the token i
[source, java]
----
@ApplicationScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
public class CustomIntrospectionUserInfoCache implements TokenIntrospectionCache, UserInfoCache {
...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,11 @@ Here is a simple example:
----
package io.quarkus.oidc.test;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Inject;
import io.quarkus.arc.AlternativePriority;
import io.quarkus.oidc.AuthorizationCodeTokens;
import io.quarkus.oidc.OidcTenantConfig;
import io.quarkus.oidc.TokenStateManager;
Expand All @@ -407,7 +408,8 @@ import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.RoutingContext;
@ApplicationScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
public class CustomTokenStateManager implements TokenStateManager {
@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public class ArcConfig {
* </ul>
* Each element value is used to match an alternative bean class, an alternative stereotype annotation type or a bean class
* that declares an alternative producer. If any value matches then the priority of {@link Integer#MAX_VALUE} is used for
* the relevant bean. The priority declared via {@link jakarta.annotation.Priority} or
* {@link io.quarkus.arc.AlternativePriority} is overridden.
* the relevant bean. The priority declared via {@link jakarta.annotation.Priority} is overridden.
*/
@ConfigItem
public Optional<List<String>> selectedAlternatives;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import static jakarta.interceptor.Interceptor.Priority.PLATFORM_AFTER;

import jakarta.annotation.Priority;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.quarkus.arc.AlternativePriority;

/**
* @return the single resolvable "root" MeterRegistry
*/
public class CompositeRegistryCreator {
@Produces
@Singleton
@AlternativePriority(PLATFORM_AFTER)
@Alternative
@Priority(PLATFORM_AFTER)
public MeterRegistry produceRootRegistry() {
return Metrics.globalRegistry;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.quarkus.oidc.test;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Inject;

import io.quarkus.arc.AlternativePriority;
import io.quarkus.oidc.AuthorizationCodeTokens;
import io.quarkus.oidc.OidcRequestContext;
import io.quarkus.oidc.OidcTenantConfig;
Expand All @@ -13,7 +14,8 @@
import io.vertx.ext.web.RoutingContext;

@ApplicationScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
public class CustomTokenStateManager implements TokenStateManager {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.quarkus.oidc.runtime;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;

import org.jboss.logging.Logger;

import io.quarkus.arc.AlternativePriority;
import io.quarkus.oidc.AccessTokenCredential;
import io.quarkus.oidc.IdTokenCredential;
import io.quarkus.oidc.RefreshToken;
Expand Down Expand Up @@ -38,7 +39,8 @@ IdTokenCredential currentIdToken() {

@Produces
@RequestScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
AccessTokenCredential currentAccessToken() {
AccessTokenCredential cred = identity.getCredential(AccessTokenCredential.class);
if (cred == null || cred.getToken() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.quarkus.smallrye.health.runtime;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Inject;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;

import io.quarkus.arc.AlternativePriority;
import io.quarkus.runtime.BlockingOperationControl;
import io.smallrye.health.AsyncHealthCheckFactory;
import io.smallrye.health.api.AsyncHealthCheck;
Expand All @@ -19,7 +20,8 @@
* health checks with different executors provided by {@link MutinyHelper}.
*/
@ApplicationScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
public class QuarkusAsyncHealthCheckFactory extends AsyncHealthCheckFactory {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;

import org.jose4j.jwt.JwtClaims;

import io.quarkus.arc.AlternativePriority;
import io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal;
import io.smallrye.jwt.auth.principal.JWTAuthContextInfo;
import io.smallrye.jwt.auth.principal.JWTCallerPrincipal;
import io.smallrye.jwt.auth.principal.JWTCallerPrincipalFactory;
import io.smallrye.jwt.auth.principal.ParseException;

@ApplicationScoped
@AlternativePriority(1)
@Alternative
@Priority(1)
public class TestJWTCallerPrincipalFactory extends JWTCallerPrincipalFactory {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import jakarta.enterprise.context.BeforeDestroyed;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.Reception;
import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Inject;

import org.eclipse.microprofile.config.ConfigProvider;
import org.slf4j.LoggerFactory;

import io.quarkus.arc.AlternativePriority;
import io.smallrye.mutiny.Uni;
import io.smallrye.reactive.messaging.annotations.Blocking;
import io.smallrye.reactive.messaging.providers.connectors.ExecutionHolder;
Expand All @@ -25,7 +25,8 @@
import io.vertx.mutiny.core.Context;
import io.vertx.mutiny.core.WorkerExecutor;

@AlternativePriority(1)
@Alternative
@Priority(1)
@ApplicationScoped
// TODO: create a different entry for WorkerPoolRegistry than `analyzeWorker` and drop this class
public class QuarkusWorkerPoolRegistry extends WorkerPoolRegistry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,6 @@ private Map<DotName, StereotypeInfo> findStereotypes(Map<DotName, ClassInfo> int
alternativePriority = annotation.value().asInt();
} else if (DotNames.ARC_PRIORITY.equals(annotation.name()) && alternativePriority == null) {
alternativePriority = annotation.value().asInt();
} else if (DotNames.ALTERNATIVE_PRIORITY.equals(annotation.name())) {
isAlternative = true;
alternativePriority = annotation.value().asInt();
} else {
final ScopeInfo scope = getScope(annotation.name(), customContexts);
if (scope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;

import io.quarkus.arc.AlternativePriority;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.processor.BeanDeploymentValidator.ValidationContext;
import io.quarkus.arc.processor.BuildExtension.BuildContext;
Expand Down Expand Up @@ -754,8 +753,7 @@ public Builder setGenerateSources(boolean value) {

/**
* Can be used to compute a priority of an alternative bean. A non-null computed value always
* takes precedence over the priority defined by {@link Priority}, {@link AlternativePriority} or an alternative
* stereotype.
* takes precedence over the priority defined by {@link Priority} or a stereotype.
*
* @param priorities
* @return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ static BeanInfo createProducerMethod(Set<Type> beanTypes, MethodInfo producerMet
isAlternative = true;
continue;
}
if (DotNames.ALTERNATIVE_PRIORITY.equals(annotationName)) {
isAlternative = true;
priority = annotation.value().asInt();
continue;
}
if ((!isAlternative || priority == null) && annotationName.equals(DotNames.PRIORITY)) {
if (DotNames.PRIORITY.equals(annotationName)) {
priority = annotation.value().asInt();
continue;
}
Expand Down Expand Up @@ -179,7 +174,7 @@ static BeanInfo createProducerMethod(Set<Type> beanTypes, MethodInfo producerMet
if (priority == null) {
// after all attempts, priority is still null, bean will be ignored
LOGGER.debugf(
"Ignoring producer method %s - declared as an @Alternative but not selected by @AlternativePriority or quarkus.arc.selected-alternatives",
"Ignoring producer method %s - declared as an @Alternative but not selected by @Priority or quarkus.arc.selected-alternatives",
declaringBean.getTarget().get().asClass().name() + "#" + producerMethod.name());
return null;
}
Expand Down Expand Up @@ -234,12 +229,7 @@ static BeanInfo createProducerField(FieldInfo producerField, BeanInfo declaringB
isAlternative = true;
continue;
}
if (DotNames.ALTERNATIVE_PRIORITY.equals(annotationName)) {
isAlternative = true;
priority = annotation.value().asInt();
continue;
}
if ((!isAlternative || priority == null) && annotation.name().equals(DotNames.PRIORITY)) {
if (DotNames.PRIORITY.equals(annotation.name())) {
priority = annotation.value().asInt();
continue;
}
Expand Down Expand Up @@ -293,7 +283,7 @@ static BeanInfo createProducerField(FieldInfo producerField, BeanInfo declaringB
// after all attempts, priority is still null
if (priority == null) {
LOGGER.debugf(
"Ignoring producer field %s - declared as an @Alternative but not selected by @AlternativePriority or quarkus.arc.selected-alternatives",
"Ignoring producer field %s - declared as an @Alternative but not selected by @Priority or quarkus.arc.selected-alternatives",
producerField);
return null;
}
Expand Down Expand Up @@ -938,7 +928,7 @@ private static Integer initAlternativePriority(AnnotationTarget target, Integer
if (computedPriority != null) {
if (alternativePriority != null) {
LOGGER.infof(
"Computed priority [%s] overrides the priority [%s] declared via @Priority or @AlternativePriority",
"Computed priority [%s] overrides the priority [%s] declared via @Priority",
computedPriority, alternativePriority);
}
alternativePriority = computedPriority;
Expand Down Expand Up @@ -1124,24 +1114,19 @@ void processAnnotation(AnnotationInstance annotation,
// we needn't process it further, the annotation was a qualifier (or multiple repeating ones)
return;
}
if (annotationName.equals(DotNames.ALTERNATIVE)) {
isAlternative = true;
return;
}
if (annotationName.equals(DotNames.ALTERNATIVE_PRIORITY)) {
if (DotNames.ALTERNATIVE.equals(annotationName)) {
isAlternative = true;
priority = annotation.value().asInt();
return;
}
if (DotNames.DEFAULT_BEAN.equals(annotationName)) {
isDefaultBean = true;
return;
}
if ((!isAlternative || priority == null) && annotationName.equals(DotNames.PRIORITY)) {
if (DotNames.PRIORITY.equals(annotationName)) {
priority = annotation.value().asInt();
return;
}
if (priority == null && annotationName.equals(DotNames.ARC_PRIORITY)) {
if (priority == null && DotNames.ARC_PRIORITY.equals(annotationName)) {
priority = annotation.value().asInt();
return;
}
Expand Down Expand Up @@ -1204,7 +1189,7 @@ BeanInfo create() {
if (priority == null) {
// after all attempts, priority is still null, bean will be ignored
LOGGER.debugf(
"Ignoring bean defined via %s - declared as an @Alternative but not selected by @Priority, @AlternativePriority or quarkus.arc.selected-alternatives",
"Ignoring bean defined via %s - declared as an @Alternative but not selected by @Priority or quarkus.arc.selected-alternatives",
beanClass.name());
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.jboss.jandex.DotName;

import io.quarkus.arc.All;
import io.quarkus.arc.AlternativePriority;
import io.quarkus.arc.ArcInvocationContext;
import io.quarkus.arc.DefaultBean;
import io.quarkus.arc.InjectableBean;
Expand Down Expand Up @@ -100,7 +99,6 @@ public final class DotNames {
public static final DotName EVENT = create(Event.class);
public static final DotName EVENT_METADATA = create(EventMetadata.class);
public static final DotName ALTERNATIVE = create(Alternative.class);
public static final DotName ALTERNATIVE_PRIORITY = create(AlternativePriority.class);
public static final DotName DEFAULT_BEAN = create(DefaultBean.class);
public static final DotName SINGLETON = create(Singleton.class);
public static final DotName APPLICATION_SCOPED = create(ApplicationScoped.class);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Since Common Annotations 2.1, the {@code jakarta.annotation.Priority} is no longer
* meta-annotated {@code @Target}, so these two annotations are equivalent.
* <p>
* A priority specified by {@link AlternativePriority} and {@link jakarta.annotation.Priority} takes precedence.
* A priority specified by {@link jakarta.annotation.Priority} takes precedence.
*
* @deprecated use {@link jakarta.annotation.Priority}; this annotation will be removed at some time after Quarkus 3.6
*/
Expand Down
Loading

0 comments on commit 86c7988

Please sign in to comment.