Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2521

NullPointerException using Stateless with configured interceptors

XMLWordPrintable

    • Hide

      you can start the test case from github https://github.com/flashboss/interceptors-bug on the master branch. So execute by console:

      git clone https://github.com/flashboss/interceptors-bug
      mvn test

      Show
      you can start the test case from github https://github.com/flashboss/interceptors-bug on the master branch. So execute by console: git clone https://github.com/flashboss/interceptors-bug mvn test
    • Workaround Exists
    • Hide

      Do not declare the OKInterceptor in beans.xml.
      According to spec, interceptors declared via @Interceptors annotation do not require this for activation anyway. Hence the interceptor will still get invoked and the error will disappear.

      Show
      Do not declare the OKInterceptor in beans.xml . According to spec, interceptors declared via @Interceptors annotation do not require this for activation anyway. Hence the interceptor will still get invoked and the error will disappear.

      I report a strange behavior on WildFly 13 when configuring interceptors within stateless. Below I describe the scenario:

      Here a simple interceptor:

      package it.vige.injection.interceptors;
      
      import javax.interceptor.AroundInvoke;
      import javax.interceptor.Interceptor;
      import javax.interceptor.InvocationContext;
      
      @Interceptor
      public class OKInterceptor {
      
      	@AroundInvoke
      	public Object aroundInvoke(InvocationContext ic) throws Exception {
      		return ic.proceed();
      	}
      }
      

      Here an annotation used as interceptor binding:

      package it.vige.injection.interceptors;
      
      import static java.lang.annotation.ElementType.CONSTRUCTOR;
      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.Retention;
      import java.lang.annotation.Target;
      
      import javax.interceptor.InterceptorBinding;
      
      @Retention(RUNTIME)
      @Target({ METHOD, TYPE, CONSTRUCTOR })
      @InterceptorBinding
      public @interface NotOK {
      
      }
      

      Here an interceptor annotated with the interceptor binding:

      package it.vige.injection.interceptors;
      
      import javax.interceptor.AroundInvoke;
      import javax.interceptor.Interceptor;
      import javax.interceptor.InvocationContext;
      
      @Interceptor
      @NotOK
      public class NotOKInterceptor {
      
      	@AroundInvoke
      	public Object aroundInvoke(InvocationContext ic) throws Exception {
      		return ic.proceed();
      	}
      }
      

      Here the stateless service configured with both the interceptors:

      package it.vige.injection.interceptors;
      
      import javax.ejb.Stateless;
      import javax.interceptor.Interceptors;
      
      @Stateless
      public class SimpleService {
      
      	@Interceptors({ OKInterceptor.class })
      	public void ok() {
      	}
      
      	@NotOK
      	public void notOk() {
      	}
      
      }
      

      This service must have two methods, one attached to the simple interceptor and the other attached to the interceptor binding.

      Here the beans.xml configuration:

      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                 http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
      	version="2.0" bean-discovery-mode="all">
      	<interceptors>
      		<class&gt;it.vige.injection.interceptors.OKInterceptor</class&gt;
      		<class&gt;it.vige.injection.interceptors.NotOKInterceptor</class&gt;
      	</interceptors>
      </beans>
      

      And in the end the client who call the service:

      ....
      	@Inject
      	private SimpleService simpleService;
      
      ...
              // this call works:
              simpleService.ok();
      
              // this call starts a NullPointerException:
              simpleService.notOk();
      ...
      

      when I try to call the notOk method I get this exception:

      javax.ejb.EJBException: java.lang.NullPointerException
      at deployment.test.war//it.vige.injection.test.InterceptorsTestCase.testNotOk(InterceptorsTestCase.java:52)
      Caused by: java.lang.NullPointerException
      at deployment.test.war//it.vige.injection.test.InterceptorsTestCase.testNotOk(InterceptorsTestCase.java:52)
      

      The same thing was tested on WildFly 11.0.0.Final and it was ok.
      If on WildFfly 13.0.0.Final I remove the @Stateless annotation from the service it works

              manovotn Matěj Novotný
              luca.stancapiano@vige.it Luca Stancapiano (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: