简体   繁体   中英

Class Cast Exception Generics Reflection ParameterizedType

I have looked for an answer on Stack for a while. All the answers look like they say I already have the right answer, but I still keep getting a class cast exception for the first line in the constructor below.

SEVERE: Exception while loading the app : EJB Container initialization error
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    at com.domain.security.logging.ElsAbstractCrudClass.<init>(ElsAbstractCrudClass.java:54)

Here's the code. After looking at the documentation I still can't figure it out. I'm relatively new to generics and reflection so need some help. TIA.

    public abstract class ElsAbstractCrudClass<T> {
        Class<T> entity;

        public ElsAbstractCrudClass() {

[line 54]   ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
            Type type = genericSuperclass.getActualTypeArguments()[0];
            this.entity = (Class<T>) type;

        }
    }

Here is a subclass of the abstract crud class (SessionLog is a JPA entity):

@Stateless
public class SessionLogger extends ElsAbstractCrudClass<SessionLog> {

    @PersistenceContext(unitName = "ELS_Soulard_PU")
    private EntityManager em;
    @EJB
    DozerInstantiator di;
    //SessionLog entity;
    //SessionLog sessionLog = new SessionLog();
    static final Logger logger = Logger.getLogger(SessionLogger.class.getSimpleName());

    public SessionLogger() {
    }
...

getGenericSuperclass returns an instance of ParameterizedType if the super class is generic, and an instance of Class if it is not. Presumably you have something like:

class A extends B { ... }
class B extends ElsAbstractCrudClass<Person> { ... }

Now, getClass() return A.class with superclass B.class , which is not generic ...

You could generalize your code snippet to work as long as the runtime class is not generic (recursively walking the type hierarchy, replacing type parameters by their definitions as you go). However, unless you have dozens of crud classes, requiring the subclass to pass the proper class object is easier:

public abstract class ElsAbstractCrudClass<T> {
    final Class<T> entityClass;

    public ElsAbstractCrudClass(Class<T> entityClass) {
        this.entityClass = entityClass;
    }
}

You got this error because EJB Container extend your Stateless bean to wrap EJB specific logic on method invocation. So at deployment time you have somthing like this:

ContainerSubclass extends SessionLogger {}

Solutions:

1) In your constructor first call

... = getClass().getSuperClass();
...

2) or code against interfaces so EJB container would create Dynamic Proxy through reflection.

Another way to run into trouble is if your extending class uses the raw type, not the generic type. In other words, this subclass will generate your exception, because its supertype is simply the raw type ElsAbstractCrudClass.

public class EE extends ElsAbstractCrudClass { ... }

But this one will not because its supertype is the generic type ElsAbstractCrudClass

public class EE extends ElsAbstractCrudClass<String> { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM