Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow AroundConstruct methods to return Object. Fixes #1791 #1794

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,21 @@ class InterceptorInfo extends BeanInfo implements Comparable<InterceptorInfo> {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
}