Java:Eclipse CDT IncludeRefContainer类上返回false的实例

Java:Eclipse CDT IncludeRefContainer类上返回false的实例,java,eclipse-cdt,instanceof,Java,Eclipse Cdt,Instanceof,我有以下代码: private static IProject getProjectFromActivePart(IWorkbenchWindow activeWindow, ISelection selection) { if (selection != null && selection instanceof IStructuredSelection) { Object element = ((IStructuredSelection) selecti

我有以下代码:

private static IProject getProjectFromActivePart(IWorkbenchWindow activeWindow, ISelection selection) {
    if (selection != null && selection instanceof IStructuredSelection) {
        Object element = ((IStructuredSelection) selection).getFirstElement();
        if (element != null) {
            if (element instanceof IncludeRefContainer) {
                IncludeRefContainer cont = (IncludeRefContainer) element;
                return cont.getCProject().getProject();
            }
        }
    }
    return null;
}
车站
if(IncludeRefContainer的元素实例)
返回false,即使
element.getClass()
返回:

类org.eclipse.cdt.internal.ui.cview.IncludeRefContainer

。 尝试将元素对象强制转换为IncludeRefContainer会抛出一个exeption:

java.lang.ClassCastException: org.eclipse.cdt.internal.ui.cview.IncludeRefContainer cannot be cast to org.eclipse.cdt.internal.ui.cview.IncludeRefContainer

听起来很奇怪。如何解决这个问题?

多亏了我得到的评论,我成功地解决了这个问题: 当元素的类对象由另一个类加载器加载时会发生这种情况,该类加载器加载了试图执行强制转换的类。 为了解决这个问题,我在Dependencies->Imported Packages部分添加了lib,而不是在Runtime->ClassPath

中添加了lib。当两个不同的类加载器加载同一个类时,会出现这样一条“奇怪”的错误消息。这意味着您元素的类对象是由另一个类加载器加载的,该类加载器加载了您所显示代码的类。不幸的是,我对Eclipse内部的了解还不够,无法帮助您。可能您对所讨论的库有多个依赖项?请检查以下问题: