Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

2013-01-06T16:44:52

I am trying to implement Lazy Singleton with Generics and Inheritance. I have created a abstract super-class and declared a Map which will store all the instances of the child class of this class.

Here it is:

public abstract class AbstractXMLParser<T> {
    @SuppressWarnings("rawtypes")
    private static final Map<Class<? extends AbstractXMLParser>, AbstractXMLParser> INSTANCES = new HashMap<>();

    public AbstractXMLParser() {
        throw new UnsupportedOperationException("Cannot instantiate");
    }

    private static class SingletonHolder<T> {       
        @SuppressWarnings({ "unchecked"})
        private static <T> T getInstance() throws InstantiationException, IllegalAccessException {
            Class<T> clazz = (Class<T>) ((ParameterizedType) SingletonHolder.class.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            return clazz.newInstance();
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T extends AbstractXMLParser> T getInstance() throws InstantiationException, IllegalAccessException {
        Class<T> clazz = (Class<T>) ((ParameterizedType) AbstractXMLParser.class.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        if(INSTANCES.containsKey(clazz)) {
            return (T) INSTANCES.get(clazz);
        } else {
            T instance = SingletonHolder.getInstance();
            INSTANCES.put(clazz, instance);
            return instance;
        }
    }
}

And one of the child class is:

public class ActivityTypeXMLParser extends AbstractXMLParser<ActivityTypeXMLParser>{
    private ActivityTypesXMLModel activityTypes;

    private ActivityTypeXMLParser() {

    }

    public static void main(String... strings) throws InstantiationException, IllegalAccessException {
        ActivityTypeXMLParser.getInstance();

    }
}

The main method is for testing purpose. But I am getting exception:

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    at com.edfx.adb.xml.parser.AbstractXMLParser.getInstance(AbstractXMLParser.java:25)
    at com.edfx.adb.xml.parser.ActivityTypeXMLParser.main(ActivityTypeXMLParser.java:30)

Now after debugging I found that

Class<T> clazz = (Class<T>) ((ParameterizedType) AbstractXMLParser.class.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

is the reason for the exception, because AbstractXMLParser.class.getClass().getGenericSuperclass() is returning class java.lang.Object. I need to get the Class<T> here. How can I extract it in the class AbstractXMLParser and in SingletonHolder of AbstractXMLParser?

Copyright License:
Author:「Tapas Bose」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/14180619/design-pattern-lazy-singleton-generics-and-inheritance-java-lang-class-cannot

About “Design Pattern: Lazy Singleton, Generics and Inheritance: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType” questions

I am trying to implement Lazy Singleton with Generics and Inheritance. I have created a abstract super-class and declared a Map which will store all the instances of the child class of this class. ...
I'm trying to work with generics mostly for practice and for a little idea I had. At the moment I have a problem with: ClassCastException:java.lang.Class cannot be cast to java.lang.reflect.
I am stuck with this issue for a long time. I searched for this issue for sometime but none of solution worked. Structure: public interface GenericDAO&lt;T extends Serializable, ID extends
I am trying to use the Singleton design pattern via my abstract Charcter class so all sub classes can acces the object instance. Here is my singleton class: class GatewayAccess { private static
I'm testing Docusign embed signing with a Demo Account, with the Java SDK in an Android App. So far I was able to sign a PDF document and make the returnUrl send back to my app through deep linking...
Trying to create DAO layer but getting following exception everytime javax.ejb.EJBException: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType I ...
I have three Classes. A Base Class, a Sub Class who extends from the Base Class and a Sub Class who extends from the First Sub Class like This: @Service public abstract class BaseClass&lt;T&gt; {
Can anyone help me to understand the benefit of using System.Lazy with Singleton Design Pattern.
I have created hierarchy of interface and classes using generic and messed up everything. Topmost class is AbstractJpaEntity which is extended by all domain entity @MappedSuperclass @EntityListen...
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 const...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.