From f6f704ed137434250ab9a3667f988e1fe99596b7 Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Fri, 10 Feb 2023 18:33:28 +0100 Subject: [PATCH] Arc - validate Event raw type injection points and throw DefinitionException when found --- .../java/io/quarkus/arc/processor/Beans.java | 14 ++++++ .../invalid/ConstructorEventRawTypeTest.java | 36 ++++++++++++++ .../DisposerMethodEventRawTypeTest.java | 47 +++++++++++++++++++ .../invalid/EventRawTypeInjectionTest.java | 38 +++++++++++++++ .../invalid/InitMethodEventRawTypeTest.java | 38 +++++++++++++++ .../invalid/ObserverMethodEventRawType.java | 37 +++++++++++++++ .../ProducerMethodEventRawTypeTest.java | 41 ++++++++++++++++ 7 files changed, 251 insertions(+) create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ConstructorEventRawTypeTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/DisposerMethodEventRawTypeTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/EventRawTypeInjectionTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/InitMethodEventRawTypeTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ObserverMethodEventRawType.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ProducerMethodEventRawTypeTest.java diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Beans.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Beans.java index 008b41ffee585..bedb44374e7ea 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Beans.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Beans.java @@ -650,6 +650,20 @@ static void validateInterceptorDecorator(BeanInfo bean, List errors, static void validateBean(BeanInfo bean, List errors, Consumer bytecodeTransformerConsumer, Set classesReceivingNoArgsCtor) { + // validate injection points, this includes producer method params, disposer method params and so on + List 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; diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ConstructorEventRawTypeTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ConstructorEventRawTypeTest.java new file mode 100644 index 0000000000000..3fa015925a628 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ConstructorEventRawTypeTest.java @@ -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) { + } + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/DisposerMethodEventRawTypeTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/DisposerMethodEventRawTypeTest.java new file mode 100644 index 0000000000000..3f9e89a7334f0 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/DisposerMethodEventRawTypeTest.java @@ -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 { + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/EventRawTypeInjectionTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/EventRawTypeInjectionTest.java new file mode 100644 index 0000000000000..80b8922fec4cd --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/EventRawTypeInjectionTest.java @@ -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; + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/InitMethodEventRawTypeTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/InitMethodEventRawTypeTest.java new file mode 100644 index 0000000000000..30a851465045b --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/InitMethodEventRawTypeTest.java @@ -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) { + } + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ObserverMethodEventRawType.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ObserverMethodEventRawType.java new file mode 100644 index 0000000000000..407226f03f87e --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ObserverMethodEventRawType.java @@ -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) { + } + + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ProducerMethodEventRawTypeTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ProducerMethodEventRawTypeTest.java new file mode 100644 index 0000000000000..dd313d7903805 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/event/injection/invalid/ProducerMethodEventRawTypeTest.java @@ -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 { + + } +}