users@glassfish.java.net

No-Interface EJB 3.1 breaks my getPersistentClass()

From: <glassfish_at_javadesktop.org>
Date: Tue, 19 Oct 2010 03:31:49 PDT

I just tried to play a little with these shiny new 'No-Interface' EJB 3.1 by removing 'inplements FooLocal'.

I was [i]very[/i] surprised to see that my EAR no longer deploys; server.log complains about an illegal cast.

I use the cool DAO pattern from the Hibernate book for my multitude EJBs;

public abstract class GenericEjb<T> implements GenericLocal<T>

It needs to figure out the actual subclass, used by e.g. entityManager.find(peristentClass, id)

private Class<T> getPersistentClass() {
   Class c = getClass();
   Type t = c.getGenericSuperclass();

   // Using EJB 3.1 generated interface results in a subclass... Try again!
   if (!(t instanceof ParameterizedType))
      t = c.getSuperclass().getGenericSuperclass();

   // This line caused a java.lang.ClassCastException with EJB 3.1:
   // java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
   ParameterizedType pt = (ParameterizedType) t;
   return (Class<T>) pt.getActualTypeArguments()[0];
}

After a lot of hair-pulling and head-to-wall-banging I found that the class returned by getClass() no longer is my subclass of GenericEjb but a sub-sub-class, named something like com.example.blah.ejbs.__EJB31_Generated__FooEjb__Intf____Bean__ !!

My workaround above works, but is this really how a No-Interface EJB 3.1 should work? Is this portable to other JEE 6 app servers?
[Message sent by forum member 'tmpsa']

http://forums.java.net/jive/thread.jspa?messageID=485563