Java: instanceof 在 Eclipse 上返回 false CDT IncludeRefContainer class
Java: instanceof returning false on Eclipse CDT IncludeRefContainer class
我有以下代码:
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(element instanceof IncludeRefContainer)
returns 错误,尽管
element.getClass()
returns:
class org.eclipse.cdt.internal.ui.cview.IncludeRefContainer
。
尝试将元素对象转换为 IncludeRefContainer 会引发异常:
java.lang.ClassCastException: org.eclipse.cdt.internal.ui.cview.IncludeRefContainer cannot be cast to org.eclipse.cdt.internal.ui.cview.IncludeRefContainer
我听起来很奇怪。我该如何解决这个问题?
感谢我收到的评论,我设法解决了这个问题:
当元素的 class 对象被不同的 class 加载器加载为 class 加载器加载试图执行转换的 class 时,就会发生这种情况。
为了修复它,我在 Dependencies->Imported Packages 部分而不是在 Runtime->ClassPath
中添加了 lib
我有以下代码:
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(element instanceof IncludeRefContainer)
returns 错误,尽管
element.getClass()
returns:
class org.eclipse.cdt.internal.ui.cview.IncludeRefContainer
。 尝试将元素对象转换为 IncludeRefContainer 会引发异常:
java.lang.ClassCastException: org.eclipse.cdt.internal.ui.cview.IncludeRefContainer cannot be cast to org.eclipse.cdt.internal.ui.cview.IncludeRefContainer
我听起来很奇怪。我该如何解决这个问题?
感谢我收到的评论,我设法解决了这个问题: 当元素的 class 对象被不同的 class 加载器加载为 class 加载器加载试图执行转换的 class 时,就会发生这种情况。 为了修复它,我在 Dependencies->Imported Packages 部分而不是在 Runtime->ClassPath
中添加了 lib