lundi 25 mars 2019

java.lang.ClassCastException: com.sun.proxy.$Proxy11 cannot be cast to

My Goal is to create an instance from a class that implements an interface and extends another class.

extent Screen

public class EntityDesignDialog extends Screen {

 }

extent entity Design dialog

public class EventDesignDialog extends EntityDesignDialog{
 }

method class:

public class Example{
        public static <T extends Screen> T getInstance(Class<? extends Screen> type)
                    throws InstantiationException, IllegalAccessException {

                List<Class<?>> interfaces = new ArrayList<>();
                interfaces.addAll(Arrays.asList(type.getInterfaces()));
                return (T) Proxy.newProxyInstance(type.getClassLoader(),
                         interfaces.toArray(new Class<?>[interfaces.size()])
                        , new IPageProxy(type));
        }
    }

invoking the method:

 Example.getInstance(EventDesignDialog.class);

the following execption thrown:

java.lang.ClassCastException: com.sun.proxy.$Proxy11 cannot be cast to com.abc.infrastructure.pages.navigation.content.products.events.newEvent.EventDesignDialog

at this part of code: interfaces.toArray(new Class[interfaces.size()])





Aucun commentaire:

Enregistrer un commentaire