Skip to content

Commit

Permalink
OWB-1441 fix exception if custom interceptor has no default ct
Browse files Browse the repository at this point in the history
  • Loading branch information
struberg committed Oct 14, 2024
1 parent 3dd9bbd commit 45e5787
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.webbeans.config.OWBLogConst;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.context.creational.CreationalContextImpl;
import org.apache.webbeans.exception.WebBeansCreationException;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.inject.InjectableConstructor;
import org.apache.webbeans.inject.InjectableField;
Expand Down Expand Up @@ -140,16 +139,20 @@ protected void defineLifecycleInterceptors(Bean<T> bean, AnnotatedType<T> annota
// no more needed
interceptorInfo.getClassCdiInterceptors().clear();

InterceptorResolutionService.BusinessMethodInterceptorInfo constructorInterceptorInfo =
interceptorInfo.getConstructorInterceptorInfos().get(getConstructor().getJavaMember());
Interceptor<?>[] constructorEjbInterceptorArray = constructorInterceptorInfo == null ?
null : constructorInterceptorInfo.getEjbInterceptors();
List<Interceptor<?>> constructorEjbInterceptors = constructorEjbInterceptorArray == null ?
Collections.<Interceptor<?>>emptyList() : asList(constructorEjbInterceptorArray);
aroundConstructInterceptors = getLifecycleInterceptors(
final AnnotatedConstructor<T> ct = getConstructor();
if (ct != null)
{
InterceptorResolutionService.BusinessMethodInterceptorInfo constructorInterceptorInfo =
interceptorInfo.getConstructorInterceptorInfos().get(ct.getJavaMember());
Interceptor<?>[] constructorEjbInterceptorArray = constructorInterceptorInfo == null ?
null : constructorInterceptorInfo.getEjbInterceptors();
List<Interceptor<?>> constructorEjbInterceptors = constructorEjbInterceptorArray == null ?
Collections.<Interceptor<?>>emptyList() : asList(constructorEjbInterceptorArray);
aroundConstructInterceptors = getLifecycleInterceptors(
constructorEjbInterceptors,
interceptorInfo.getConstructorCdiInterceptors(),
InterceptionType.AROUND_CONSTRUCT);
}
}

@Override
Expand Down Expand Up @@ -395,7 +398,8 @@ protected AnnotatedConstructor<T> createConstructor()
Constructor<T> defaultConstructor = getDefaultConstructor();
if (defaultConstructor == null)
{
throw new WebBeansCreationException("No default constructor for " + annotatedType.getJavaClass().getName());
// sometimes there is no default ct nor any injection point ct
return null;
}
return new AnnotatedConstructorImpl<>(webBeansContext, defaultConstructor, annotatedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class BigBrotherInterceptor
{
private static boolean observed = false;


public BigBrotherInterceptor(int totallyUselessParamJustToNotHaveADefaultCt)
{
// all fine ;)
}

public Object invoke(InvocationContext context) throws Exception
{
System.out.println("Big Brother is watching you " + context.getMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Set<InjectionPoint> getInjectionPoints()
@Override
public BigBrotherInterceptor create(CreationalContext<BigBrotherInterceptor> creationalContext)
{
return new BigBrotherInterceptor();
return new BigBrotherInterceptor(42);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import jakarta.enterprise.context.ApplicationScoped;
import org.apache.webbeans.test.AbstractUnitTest;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptor;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotheredExtension;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
Expand All @@ -47,7 +44,7 @@ public void testCustomInterceptor() throws Exception
}

@ApplicationScoped
@BigBrothered
@Watched
public static class LittleJohnDoe
{
public void makeACoffee() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.util.Nonbinding;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptorBean;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered;


public class WatchExtension implements Extension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.webbeans.test.interceptors.owb1441;

import jakarta.interceptor.AroundInvoke;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.interceptor.InvocationContext;
import org.apache.webbeans.annotation.DefaultLiteral;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptor;
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered;

public class WatchInterceptorBean implements Interceptor<WatchInterceptor>
{
// it's good performance practice to keep the sets static as they are requested tons of times!
public static final Set<Type> TYPES = Set.of(WatchInterceptor.class);
public static final Set<Annotation> QUALIFIERS = Set.of(DefaultLiteral.INSTANCE);
public static final Set<Annotation> INTERCEPTOR_BINDINGS = Set.of(new AnnotationLiteral<BigBrothered>() {});
public static final Set<Annotation> INTERCEPTOR_BINDINGS = Set.of(new AnnotationLiteral<Watched>() {});

public WatchInterceptorBean(int totallyUselessParamJustToNotHaveADefaultCt)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ public void testTomcatRequest() throws Exception
Assert.assertNotNull(response);
Assert.assertEquals("Got " + builder.toString(), 200, response.getStatusLine().getStatusCode());
}

}
}

0 comments on commit 45e5787

Please sign in to comment.