Question

I use JDev 11.1.2.4 I have a custom Supplier class which is being load some items by invoking applicationScope bean method. I am trying to transform my object to appropriate selectItems. I could obtain right object list essentially, but suddenly faced ClassCastException. Unfortunatelly, i could not find any solution on internet. I know those classes are exactly same. (additionaly i see on debug time that package and classeses has no difference as seen) Where is the problem?? I read on internet something about different classloaders but i couldnt reach root cause or solution. please helpme brgds

   package com.accmee.mobile.supplier;

    import com.accmee.mobile.pojo.ServiceCategory;
    import com.acme.structure.util.datalist.SimpleListSupplier;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import javax.el.MethodExpression;
    import oracle.adfmf.framework.api.AdfmfJavaUtilities;
    import oracle.adfmf.javax.faces.model.SelectItem;

    public class ServiceCategorySupplier extends SimpleListSupplier
    {
        public ServiceCategorySupplier(boolean blankItemApplied)
        {
            super(blankItemApplied);
        }

        public ServiceCategorySupplier()
        {
            super();
        }

        public void loadList()
        {
            try
            {
                MethodExpression me = AdfmfJavaUtilities.getMethodExpression("#{applicationScope.loginBean.loadCategories}", List.class, new Class[] { }); /* this applicationScope bean method loads through webservice consume via JavaAPI, and works properly returns list with elements**/
                List categories = (List)me.invoke(AdfmfJavaUtilities.getAdfELContext(), new Object[] { });
                itemList.addAll(getConvertedToSelectItemList(categories, true)); // here passes parameter into method which faced exception
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        public String getListName()
        {
            return "categories";
        }

        public static Collection getConvertedToSelectItemList(List list, boolean blankItemApplied)
        {
            Collection convertedCollection = new ArrayList();
            SelectItem selectItem = null;

            if (blankItemApplied)
            {
                selectItem = new SelectItem();
                convertedCollection.add(selectItem);
            }
            for(int i=0;i<list.size();i++)
            {
                ServiceCategory superEntity  = (ServiceCategory)list.get(i); // here is the ClassCastException, this line throws exception
                selectItem = getConvertedToSelectItem(superEntity);
                convertedCollection.add(selectItem);
            }
            return convertedCollection;
        }


        public static SelectItem getConvertedToSelectItem(ServiceCategory superEntity)
        {
            SelectItem selectItem = new SelectItem();
            selectItem.setLabel(superEntity.getName());
            selectItem.setValue(superEntity);
            return selectItem;
        }
    }
Was it helpful?

Solution 2

I had to change my approach. So, i changed returnType of loadCategories method to GenericType instead of my custom class. Then it worked like that.

public class ServiceCategorySupplier extends SimpleListSupplier
{
    public ServiceCategorySupplier(boolean blankItemApplied)
    {
        super(blankItemApplied);
    }


    public ServiceCategorySupplier()
    {
        super();
    }


    public void loadList()
    {
        try
        {
            MethodExpression me = AdfmfJavaUtilities.getMethodExpression("#{applicationScope.loginBean.loadCategories}", List.class, new Class[] { });
            List categories = (List)me.invoke(AdfmfJavaUtilities.getAdfELContext(), new Object[] { });
            list.addAll(categories);
            loadItemList();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new AdfException(e.getMessage(), AdfException.ERROR);
        }
    }


    public void loadItemList()
    {
        SelectItem selectItem = null;

        itemList=new SelectItem[list.size()];
        ServiceCategory serviceCategory=null;
        for(int i=0;i<list.size();i++)
        {
            GenericType serviceCategoryType  = (GenericType)list.get(i);
            serviceCategory = (ServiceCategory)GenericTypeBeanSerializationHelper.fromGenericType(ServiceCategory.class, serviceCategoryType);
            selectItem = getConvertedToSelectItem(serviceCategory);
            itemList[i]=selectItem;
        }
    }


    public static SelectItem getConvertedToSelectItem(ServiceCategory superEntity)
    {
        SelectItem selectItem = new SelectItem();


        selectItem.setLabel(superEntity.getName());
        selectItem.setValue(superEntity.getId());


        return selectItem;
    }


    public String getListName()
    {
        return "categories";
    }
}

OTHER TIPS

The same class loaded by two different classloaders is considered at runtime as two different classes. Probably that's what's happening to you.

Watch this page: http://www.ibm.com/developerworks/java/library/j-dyn0429/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top