Skip to content

Commit

Permalink
Arc - Using @typed with illegal values should throw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jan 18, 2023
1 parent d72072c commit 07d0a51
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ static Set<Type> getProducerMethodTypeClosure(MethodInfo producerMethod, BeanDep
throw new IllegalArgumentException("Unsupported return type");
}
}
return restrictBeanTypes(types, beanDeployment.getAnnotations(producerMethod), beanDeployment.getBeanArchiveIndex());
return restrictBeanTypes(types, beanDeployment.getAnnotations(producerMethod), beanDeployment.getBeanArchiveIndex(),
producerMethod);
}

static Set<Type> getProducerFieldTypeClosure(FieldInfo producerField, BeanDeployment beanDeployment) {
Expand Down Expand Up @@ -369,7 +370,8 @@ static Set<Type> getProducerFieldTypeClosure(FieldInfo producerField, BeanDeploy
throw new IllegalArgumentException("Unsupported return type");
}
}
return restrictBeanTypes(types, beanDeployment.getAnnotations(producerField), beanDeployment.getBeanArchiveIndex());
return restrictBeanTypes(types, beanDeployment.getAnnotations(producerField), beanDeployment.getBeanArchiveIndex(),
producerField);
}

static Set<Type> getClassBeanTypeClosure(ClassInfo classInfo, BeanDeployment beanDeployment) {
Expand All @@ -381,7 +383,8 @@ static Set<Type> getClassBeanTypeClosure(ClassInfo classInfo, BeanDeployment bea
types = getTypeClosure(classInfo, null, buildResolvedMap(typeParameters, typeParameters,
Collections.emptyMap(), beanDeployment.getBeanArchiveIndex()), beanDeployment, null);
}
return restrictBeanTypes(types, beanDeployment.getAnnotations(classInfo), beanDeployment.getBeanArchiveIndex());
return restrictBeanTypes(types, beanDeployment.getAnnotations(classInfo), beanDeployment.getBeanArchiveIndex(),
classInfo);
}

static Map<String, Type> resolveDecoratedTypeParams(ClassInfo decoratedTypeClass, DecoratorInfo decorator) {
Expand Down Expand Up @@ -577,7 +580,8 @@ static Map<ClassInfo, Map<String, Type>> resolvedTypeVariables(ClassInfo classIn
return resolvedTypeVariables;
}

static Set<Type> restrictBeanTypes(Set<Type> types, Collection<AnnotationInstance> annotations, IndexView index) {
static Set<Type> restrictBeanTypes(Set<Type> types, Collection<AnnotationInstance> annotations, IndexView index,
AnnotationTarget target) {
AnnotationInstance typed = null;
for (AnnotationInstance a : annotations) {
if (a.name().equals(DotNames.TYPED)) {
Expand All @@ -603,8 +607,7 @@ static Set<Type> restrictBeanTypes(Set<Type> types, Collection<AnnotationInstanc
if (DotNames.OBJECT.equals(next.name())) {
continue;
}
if (typed != null && !typedClasses.contains(next.name())) {
// Remove types restricted by @Typed
if (typed != null && !typedClasses.remove(next.name())) {
it.remove();
continue;
}
Expand All @@ -617,6 +620,12 @@ static Set<Type> restrictBeanTypes(Set<Type> types, Collection<AnnotationInstanc
}
}
}
// if the set of types we gathered from @Typed annotation isn't now empty, there are some illegal types in it
if (!typedClasses.isEmpty()) {
throw new DefinitionException(
"Cannot limit bean types to types outside of the transitive closure of bean types. Bean: " + target
+ " illegal bean types: " + typedClasses);
}
return types;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.arc.test.resolution.broken;

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

import javax.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 BrokenTypedBeanTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(MyBean.class, MyOtherBean.class).shouldFail()
.build();

@Test
public void testFailure() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.arc.test.resolution.broken;

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

import javax.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 BrokenTypedProducerFieldTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(FieldProducerBean.class, MyOtherBean.class, ProducedBean.class).shouldFail()
.build();

@Test
public void testFailure() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.arc.test.resolution.broken;

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

import javax.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 BrokenTypedProducerMethodTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(MethodProducerBean.class, MyOtherBean.class, ProducedBean.class).shouldFail()
.build();

@Test
public void testFailure() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.arc.test.resolution.broken;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Typed;

@Dependent
public class FieldProducerBean {

@Produces
@Typed(value = MyOtherBean.class)
public ProducedBean produceBean() {
return new ProducedBean();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.arc.test.resolution.broken;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Typed;

@Dependent
public class MethodProducerBean {

@Produces
@Typed(value = MyOtherBean.class)
public ProducedBean produceBean() {
return new ProducedBean();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.arc.test.resolution.broken;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Typed;

@Dependent
@Typed(value = MyOtherBean.class)
public class MyBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.arc.test.resolution.broken;

import javax.enterprise.context.Dependent;

@Dependent
public class MyOtherBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.quarkus.arc.test.resolution.broken;

public class ProducedBean {
}

0 comments on commit 07d0a51

Please sign in to comment.