Skip to content

Commit

Permalink
Arc - validate Event raw type injection points and throw DefinitionEx…
Browse files Browse the repository at this point in the history
…ception when found
  • Loading branch information
manovotn committed Feb 10, 2023
1 parent a57e936 commit f6f704e
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,20 @@ static void validateInterceptorDecorator(BeanInfo bean, List<Throwable> errors,

static void validateBean(BeanInfo bean, List<Throwable> errors, Consumer<BytecodeTransformer> bytecodeTransformerConsumer,
Set<DotName> classesReceivingNoArgsCtor) {
// validate injection points, this includes producer method params, disposer method params and so on
List<InjectionPointInfo> injectionPoints = new ArrayList<>();
injectionPoints.addAll(bean.getAllInjectionPoints());
if (bean.getDisposer() != null) {
injectionPoints.addAll(bean.getDisposer().getAllInjectionPoints());
}
for (InjectionPointInfo ipi : injectionPoints) {
Type ipType = ipi.getType();
if (DotNames.EVENT.equals(ipType.name()) && ipType.kind() == Kind.CLASS) {
throw new DefinitionException(
"Event injection point can never be raw type - please specify the type parameter. Bean: "
+ bean + " " + ipi);
}
}
if (bean.isClassBean()) {
ClassInfo beanClass = bean.getTarget().get().asClass();
String classifier = bean.getScope().isNormal() ? "Normal scoped" : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.event.Event;
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 ConstructorEventRawTypeTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(InvalidBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Dependent
public static class InvalidBean {

// raw event type
public InvalidBean(Event event) {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.Disposes;
import jakarta.enterprise.inject.Produces;
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 DisposerMethodEventRawTypeTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(DisposerMethodInjectionBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Dependent
public static class DisposerMethodInjectionBean {

@Produces
public Foo produceFoo() {
return new Foo();
}

// rawtype Event
public void disposeFoo(@Disposes Foo foo, Event event) {
}

}

static class Foo {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.spi.DefinitionException;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

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

import io.quarkus.arc.Unremovable;
import io.quarkus.arc.test.ArcTestContainer;

public class EventRawTypeInjectionTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(WrongBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Singleton
@Unremovable
static class WrongBean {

@Inject
Event rawEvent;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.spi.DefinitionException;
import jakarta.inject.Inject;

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

import io.quarkus.arc.test.ArcTestContainer;

public class InitMethodEventRawTypeTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(InvalidBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Dependent
public static class InvalidBean {

// raw event type
@Inject
public void initMethod(Event event) {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.event.Observes;
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 ObserverMethodEventRawType {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(InvalidBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Dependent
public static class InvalidBean {

// raw event type
public void observe(@Observes String something, Event event) {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.arc.test.event.injection.invalid;

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

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.Produces;
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 ProducerMethodEventRawTypeTest {

@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder().beanClasses(ProducerMethodInjectionBean.class).shouldFail()
.build();

@Test
public void testExceptionIsThrown() {
Throwable error = container.getFailure();
assertNotNull(error);
assertTrue(error instanceof DefinitionException);
}

@Dependent
public static class ProducerMethodInjectionBean {

@Produces
public DisposerMethodEventRawTypeTest.Foo produceFoo(Event event) { // rawtype event injection point
return new DisposerMethodEventRawTypeTest.Foo();
}
}

static class Foo {

}
}

0 comments on commit f6f704e

Please sign in to comment.