Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:35:43+00:00 2026-06-08T04:35:43+00:00

I have looked for an answer on Stack for a while. All the answers

  • 0

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() {
    }
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:35:46+00:00Added an answer on June 8, 2026 at 4:35 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked on here and tried every answer available, but it is still
Okay, I have looked hard for this answer, but all the websites, even the
I have looked at many different peoples answers to this problem but they only
I have looked around like crazy but don't get a real answer. I got
I have looked within Stack Overflow and online elsewhere but can't find an answer
Looked at some other similar answers on stack overflow, but no relevant answer was
I have looked all over stack overflow, for the answer to this question, and
I've looked around on stack overflow for days and have not seen an answer
Have looked quite hard for this answer but having no luck. I have 3
I have looked but have not found a specific answer for changing just one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.