Skip to content

Commit

Permalink
ArC: validate managed beans whose declaring class is generic
Browse files Browse the repository at this point in the history
They must be `@Dependent` per the specification. We already have similar
validation for producers, this commit adds it for managed beans.
  • Loading branch information
Ladicek committed May 15, 2023
1 parent 1514eb2 commit ff6bf2e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.List;
import java.util.stream.Collectors;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.spi.DeploymentException;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -36,7 +36,7 @@ public void testFailure() {
fail();
}

@Singleton
@Dependent
static class Foo<T> {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Consumer;
import java.util.function.Supplier;

import jakarta.enterprise.context.Dependent;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.WebApplicationException;
Expand Down Expand Up @@ -137,6 +138,7 @@ public interface HasMessage {
}

@Provider
@Dependent
public static class HasMessageMessageBodyWriter<T extends HasMessage> implements ServerMessageBodyWriter<T> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,12 @@ BeanInfo create() {
} else {
scope = scopes.get(0);
}
if (scope != null // `null` is just like `@Dependent`
&& !BuiltinScope.DEPENDENT.is(scope)
&& !beanClass.typeParameters().isEmpty()) {
throw new DefinitionException(
"Declaring class of a managed bean is generic, its scope must be @Dependent: " + beanClass);
}
if (!isAlternative) {
isAlternative = initStereotypeAlternative(stereotypes, beanDeployment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@
<exclude name="testGetBean"/>
</methods>
</class>
<class name="org.jboss.cdi.tck.tests.definition.bean.genericbroken.GenericManagedBeanTest">
<methods>
<exclude name="testNonDependentGenericManagedBeanNotOk"/>
</methods>
</class>
<class name="org.jboss.cdi.tck.tests.implementation.simple.lifecycle.unproxyable.UnproxyableManagedBeanTest">
<methods>
<exclude name="testNormalScopedUnproxyableBeanWithPublicFinalMethodResolution"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.arc.test.bean.illegal;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.spi.DefinitionException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.test.ArcTestContainer;

public class NonDependentGenericBeanTest {
@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(IllegalBean.class)
.shouldFail()
.build();

@Test
public void trigger() {
Throwable error = container.getFailure();
assertNotNull(error);
assertInstanceOf(DefinitionException.class, error);
assertTrue(error.getMessage().contains("Declaring class of a managed bean is generic, its scope must be @Dependent"));
}

@ApplicationScoped
static class IllegalBean<T> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -22,7 +22,7 @@ public void testBeans() {

}

@ApplicationScoped
@Dependent
static class Foo<T> {

protected String ping() {
Expand Down

0 comments on commit ff6bf2e

Please sign in to comment.