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

ClassCastException when unwrapping proxied EntityManager

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.4.0.Final
    • Fix Version/s: None
    • Component/s: Proxies
    • Labels:
      None

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              mobe Moritz Becker (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: