From 6eeafbb7654329a5058a7ff254a2ce2372b5397f Mon Sep 17 00:00:00 2001 From: Jan Martiska Date: Mon, 1 Apr 2019 13:37:24 +0200 Subject: [PATCH] Allow AroundConstruct methods to return Object. Fixes #1791 --- .../arc/processor/InterceptorGenerator.java | 9 ++- .../arc/processor/InterceptorInfo.java | 17 ++++- .../AroundConstructReturningObjectTest.java | 66 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/aroundconstruct/AroundConstructReturningObjectTest.java diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorGenerator.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorGenerator.java index 82e995c357814..5541038b8a422 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorGenerator.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorGenerator.java @@ -226,7 +226,14 @@ private void addIntercept(MethodCreator intercept, MethodInfo interceptorMethod, BranchResult result = intercept.ifNonZero( intercept.invokeVirtualMethod(MethodDescriptors.OBJECT_EQUALS, enumValue, intercept.getMethodParam(0))); BytecodeCreator trueBranch = result.trueBranch(); - Class retType = InterceptionType.AROUND_INVOKE.equals(interceptionType) ? Object.class : void.class; + Class retType = null; + if (InterceptionType.AROUND_INVOKE.equals(interceptionType)) { + retType = Object.class; + } else if (InterceptionType.AROUND_CONSTRUCT.equals(interceptionType)) { + retType = interceptorMethod.returnType().kind().equals(Type.Kind.VOID) ? void.class : Object.class; + } else { + retType = void.class; + } ResultHandle ret; if (Modifier.isPrivate(interceptorMethod.flags())) { privateMembers.add(isApplicationClass, diff --git a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java index 80c795942d0c1..4eb6fb3459d61 100644 --- a/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java +++ b/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java @@ -23,6 +23,7 @@ import javax.enterprise.inject.spi.InterceptionType; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; +import org.jboss.jandex.DotName; import org.jboss.jandex.MethodInfo; import org.jboss.jandex.Type; import org.jboss.jandex.Type.Kind; @@ -66,7 +67,21 @@ class InterceptorInfo extends BeanInfo implements Comparable { for (MethodInfo method : target.asClass().methods()) { if (aroundInvoke == null && method.hasAnnotation(DotNames.AROUND_INVOKE)) { aroundInvoke = method; - } else if (aroundConstruct == null && method.hasAnnotation(DotNames.AROUND_CONSTRUCT)) { + } else if (method.hasAnnotation(DotNames.AROUND_CONSTRUCT)) { + // validate compliance with rules for AroundConstruct methods + if (!method.parameters().equals(Collections.singletonList( + Type.create(DotName.createSimple("javax.interceptor.InvocationContext"), Type.Kind.CLASS)))) { + throw new IllegalStateException( + "@AroundConstruct must have exactly one argument of type javax.interceptor.InvocationContext, but method " + + method.asMethod() + " declared by " + method.declaringClass() + + " violates this."); + } + if (!method.returnType().kind().equals(Type.Kind.VOID) && + !method.returnType().name().equals(DotNames.OBJECT)) { + throw new IllegalStateException("Return type of @AroundConstruct method must be Object or void, but method " + + method.asMethod() + " declared by " + method.declaringClass() + + " violates this."); + } aroundConstruct = method; } else if (postConstruct == null && method.hasAnnotation(DotNames.POST_CONSTRUCT)) { postConstruct = method; diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/aroundconstruct/AroundConstructReturningObjectTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/aroundconstruct/AroundConstructReturningObjectTest.java new file mode 100644 index 0000000000000..f05712400d388 --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/aroundconstruct/AroundConstructReturningObjectTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2018 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.quarkus.arc.test.interceptors.aroundconstruct; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.ArcContainer; +import io.quarkus.arc.test.ArcTestContainer; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.inject.Singleton; +import javax.interceptor.AroundConstruct; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; +import org.junit.Rule; +import org.junit.Test; + +public class AroundConstructReturningObjectTest { + + @Rule + public ArcTestContainer container = new ArcTestContainer(MyTransactional.class, SimpleBean.class, + SimpleInterceptor.class); + + public static AtomicBoolean INTERCEPTOR_CALLED = new AtomicBoolean(false); + + @Test + public void testInterception() { + ArcContainer arc = Arc.container(); + SimpleBean simpleBean = arc.instance(SimpleBean.class).get(); + assertNotNull(simpleBean); + assertTrue(INTERCEPTOR_CALLED.get()); + } + + @Singleton + @MyTransactional + static class SimpleBean { + + } + + @MyTransactional + @Interceptor + public static class SimpleInterceptor { + + @AroundConstruct + Object mySuperCoolAroundConstruct(InvocationContext ctx) throws Exception { + INTERCEPTOR_CALLED.set(true); + return ctx.proceed(); + } + + } +}