不能将eclipse项目投射到IJavaProject [英] Cannot cast eclipse project to IJavaProject

查看:1413
本文介绍了不能将eclipse项目投射到IJavaProject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

    IJavaProject targetProject = null;
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (IProject project : root.getProjects()) {
        if (project.getName().equals(projName)) {
            try {
                if (project.hasNature("org.eclipse.jdt.core.javanature")) {
                    targetProject = (IJavaProject) project;
                }
            } catch( ... ) {
             // etc ...
            }

我想要做的是基本上返回一个与特定名称匹配的项目作为IJavaProject。正如你所看到的,我检查确保有关的项目具有java性质,通过调用:

What I am trying to do is essentially return a project that matches a particular name as an IJavaProject. As you can see, I check to ensure that the project in question has a java nature by calling:

if (project.hasNature("org.eclipse.jdt.core.javanature")) {

唉,我得到一个' ClassCaseException'说明

Alas, I get a 'ClassCaseException' stating

java.lang.ClassCastException: 
    org.eclipse.core.internal.resources.Project cannot be cast to org.eclipse.jdt.core.IJavaProject

任何想法为什么?我会想到,一旦IProject具有java性质,它可以被转换为IJavaProject。当服务不可用时,我无法访问JDT Core API。这里

Any idea why? I would have thought that once an IProject has a java nature, it can be cast to a IJavaProject. I can't get access to the JDT Core API at the moment as the service is unavailable here.

推荐答案

您的答案中的代码不应该工作(打字错误?)。以下是如何创建IJavaProject:

The code in your answer shouldn't work (typo?). Here is how you can create an IJavaProject:

import org.eclipse.jdt.core.JavaCore
...
    if (project.hasNature(JavaCore.NATURE_ID)) {
        targetProject = JavaCore.create(project);
    }

IProject 键入Eclipse Resources API,IJavaProject是Eclipse Java Model中的一种类型。它们不一样的抽象,但所有的IJavaProjects都有一个IProject。

IProject is a type in the Eclipse Resources API and IJavaProject is a type in the Eclipse Java Model. They are not the same abstractions, but all IJavaProjects have an IProject.

这篇关于不能将eclipse项目投射到IJavaProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆