From 1e3096bc9dc75ffa5c41d65fb7caea1b23e85f2f Mon Sep 17 00:00:00 2001 From: Johan Westerlund Date: Tue, 6 Apr 2021 15:49:35 +0200 Subject: [PATCH] Fix 16274 --- .../arc/impl/AbstractInvocationContext.java | 4 +- .../parameters/ParamInterceptor.java | 23 ++++++++++ .../parameters/ParamInterceptorTest.java | 42 +++++++++++++++++++ .../test/interceptors/parameters/Simple.java | 18 ++++++++ .../interceptors/parameters/SimpleBean.java | 23 ++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptor.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptorTest.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/Simple.java create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/SimpleBean.java diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInvocationContext.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInvocationContext.java index 05bace9b6dbd7..ef917d0f9e647 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInvocationContext.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/AbstractInvocationContext.java @@ -77,9 +77,9 @@ protected void validateParameters(Object[] params) { + ", type: " + parameterTypes[i] + "]"); } if (params[i] != null) { - if (!params[i].getClass().equals(parameterTypes[i])) { + if (!parameterTypes[i].isAssignableFrom(params[i].getClass())) { throw new IllegalArgumentException("The parameter type [" + params[i].getClass() - + "] does not match the type for the target method [" + parameterTypes[i] + "]"); + + "] can not be assigned to the type for the target method [" + parameterTypes[i] + "]"); } } } diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptor.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptor.java new file mode 100644 index 0000000000000..6e63f18fc2423 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptor.java @@ -0,0 +1,23 @@ +package io.quarkus.arc.test.interceptors.parameters; + +import javax.annotation.Priority; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; + +@Simple +@Priority(1) +@Interceptor +public class ParamInterceptor { + + @AroundInvoke + Object interceptParameters(InvocationContext ctx) throws Exception { + + Object[] params = ctx.getParameters(); + if (params.length == 1 && params[0] != null) { + params[0] = params[0].getClass().getSimpleName(); + ctx.setParameters(params); + } + return ctx.proceed(); + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptorTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptorTest.java new file mode 100644 index 0000000000000..30b7059967532 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/ParamInterceptorTest.java @@ -0,0 +1,42 @@ +package io.quarkus.arc.test.interceptors.parameters; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.ArcContainer; +import io.quarkus.arc.InstanceHandle; +import io.quarkus.arc.test.ArcTestContainer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class ParamInterceptorTest { + + @RegisterExtension + public ArcTestContainer container = new ArcTestContainer(SimpleBean.class, Simple.class, ParamInterceptor.class); + + @Test + public void testInterception() { + + ArcContainer arc = Arc.container(); + + InstanceHandle handle = arc.instance(SimpleBean.class); + SimpleBean simpleBean = handle.get(); + assertNull(simpleBean.getVal()); + + simpleBean.setVal("intercept"); + assertEquals(String.class.getSimpleName(), simpleBean.getVal()); + + simpleBean.setVal(null); + assertNull(simpleBean.getVal()); + + simpleBean.setVal(new StringBuilder("intercept")); + assertEquals(StringBuilder.class.getSimpleName(), simpleBean.getVal()); + + assertThrows(IllegalArgumentException.class, () -> { + simpleBean.setStringBuilderVal(new StringBuilder("intercept")); + }); + handle.destroy(); + } +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/Simple.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/Simple.java new file mode 100644 index 0000000000000..9695156d2156d --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/Simple.java @@ -0,0 +1,18 @@ +package io.quarkus.arc.test.interceptors.parameters; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.interceptor.InterceptorBinding; + +@Target({ TYPE, METHOD }) +@Retention(RUNTIME) +@Documented +@InterceptorBinding +public @interface Simple { + +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/SimpleBean.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/SimpleBean.java new file mode 100644 index 0000000000000..fb638e8e94f0d --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/parameters/SimpleBean.java @@ -0,0 +1,23 @@ +package io.quarkus.arc.test.interceptors.parameters; + +import javax.enterprise.context.Dependent; + +@Dependent +public class SimpleBean { + + private CharSequence val; + + @Simple + void setVal(CharSequence val) { + this.val = val; + } + + @Simple + void setStringBuilderVal(StringBuilder val) { + this.val = val; + } + + String getVal() { + return val != null ? val.toString() : null; + } +}