0

我使用 JDev 11.1.2.4 我有一个自定义供应商类,它正在通过调用 applicationScope bean 方法加载一些项目。我正在尝试将我的对象转换为适当的 selectItems。我基本上可以获得正确的对象列表,但突然遇到ClassCastException。不幸的是,我在互联网上找不到任何解决方案。我知道这些课程完全相同。(另外我在调试时看到包和类没有区别)问题出在哪里?我在互联网上阅读了一些关于不同类加载器的信息,但我无法找到根本原因或解决方案。请帮助我 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;
        }
    }
4

2 回答 2

0

我不得不改变我的方法。因此,我将 loadCategories 方法的 returnType 更改为 GenericType 而不是我的自定义类。然后它就这样工作了。

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";
    }
}
于 2014-01-05T19:42:35.573 回答
0

由两个不同的类加载器加载的同一个类在运行时被视为两个不同的类。可能这就是发生在你身上的事情。

观看此页面:http ://www.ibm.com/developerworks/java/library/j-dyn0429/

于 2014-01-02T23:02:19.827 回答