4 Replies Latest reply on Oct 7, 2013 10:10 AM by alberto_souza

    Proxy is not created considering all class interfaces

    alberto_souza

      I have a strange situation... I have this class:

       

       

      [java]

      @Inject

        public MethodValidatorInterceptor(javax.validation.Validator bvalidator,) {

           this.bvalidator = bvalidator;

        }

       

       

      @Override

        public void intercept(InterceptorStack stack, ControllerMethod method, Object controllerInstance)

        throws InterceptionException {

        Set<ConstraintViolation<Object>> violations = bvalidator.forExecutables()

        .validateParameters(controllerInstance, method.getMethod(), methodInfo.getParameters());

      [/java]

       

       

      When i run a integration test with WeldSE i get a ClassCastException becasuse forExecutables() returns ExecutableValidator. The implementation is like this:

      [java]

      public class ValidatorImpl implements Validator, ExecutableValidator {

         @Override

           public ExecutableValidator forExecutables() {

             return this;

           }

         }

      [/java]

       

       

      When I debug code, the interfaces returned by reflection not contain ExecutableValidator that is why i  get ClassCastException. But, when I run the same code with WeldListener inside Tomcat, everything goes fine. Am i missing something?

       

       

      Alberto

        • 1. Re: Proxy is not created considering all class interfaces
          alberto_souza

          A more simple example:

           

          [java]

            Validator validator = cdiBasedContainer.instanceFor(Validator.class);

            ExecutableValidator evalidator = validator.forExecutables();

            System.out.println(evalidator.toString());

          [/java]

           

          This code throws ClassCastException:

          java.lang.ClassCastException: org.jboss.weld.proxies.Validator$742456564$Proxy$_$$_WeldClientProxy cannot be cast to javax.validation.executable.ExecutableValidator

            at org.jboss.weld.proxies.Validator$742456564$Proxy$_$$_WeldClientProxy.forExecutables(Unknown Source)

            at br.com.caelum.vraptor.test.VRaptorNavigationSimpleScenariosTest.shouldValidateObject(VRaptorNavigationSimpleScenariosTest.java:54)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:601)

           

          I think this a weldse bug... The correct behavior should be implement all class implementations... But when i run the webapp, and Tomcat7Container is used, the same code runs fine

          • 2. Re: Proxy is not created considering all class interfaces
            luksa

            The proxy is created based on the injection point's type, not the bean's actual type.

             

            Apparently, I shouldn't be answering questions until I'm fully awake

             

            The proxy is based on the bean type, of course. Your Validator is probably created by a producer method, so the bean type is based on the method's return type. But, IIRC, DeltaSpike does in fact scan all the injection points and create beans with bean types based on the injection points themselves.

             

            More info at: [WELD-1498] ClassCastException when proxied class return this - JBoss Issue Tracker

            • 3. Re: Proxy is not created considering all class interfaces
              alberto_souza

              I see... But, even so, i think the proxy should be created respecting implementation interfaces... Another doubt is the different behavior between Tomcat7Container and WeldContainer(se mode)... Do you know anything about? I tried to take a look inside the classes but I did not find any reason...

               

              Thanks!!

              • 4. Re: Proxy is not created considering all class interfaces
                alberto_souza

                I updated the weld version to rc1 and everything is working. Thanks for the tip .