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

ClassCastException when unwrapping proxied EntityManager

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: Major Major
    • None
    • 2.4.0.Final
    • Proxies
    • None

      Following scenario:

      public class MyService {
          @Inject
          private EntityManager em; // injects a WELD proxy
      
          public void test() {
                em.unwrap(org.hibernate.engine.spi.SessionImplementor.class).getFactory();
          }
      }
      

      Calling MyService#test results in a ClassCastException:

      java.lang.ClassCastException: org.jboss.weldx.persistence.EntityManager$470189163$Proxy$_$$_WeldClientProxy cannot be cast to org.hibernate.engine.spi.SessionImplementor
      

      This is becuase the condition at org.jboss.weld.bean.proxy.ProxyMethodHandler:103 causes the method handler to return self instead of this. The return type of EntityManager#unwrap is unbounded generic so

      thisMethod.getReturnType().isAssignableFrom(self.getClass()))
      

      always is true.

              Unassigned Unassigned
              moritz.becker@gmx.at Moritz Becker (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: