Java 检查是否已存在具有不同案例的资源

Java 检查是否已存在具有不同案例的资源,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,在一个向导中,我创建了一个包,并试图检查是否已经存在一个具有不同大小写的资源,以避免org.eclipse.core.internal.resources.resource\checkDoesNotExist引发ResourceException。例如,当我试图创建一个包com.example.test时,当com.example.test已经存在时,我会遇到这个异常。所以我想检查一下包名的每个部分。我的代码中已经处理了现有com.Example.test的情况 由于方法checkDoesNot

在一个向导中,我创建了一个包,并试图检查是否已经存在一个具有不同大小写的资源,以避免
org.eclipse.core.internal.resources.resource\checkDoesNotExist
引发
ResourceException
。例如,当我试图创建一个包com.example.test时,当com.example.test已经存在时,我会遇到这个异常。所以我想检查一下包名的每个部分。我的代码中已经处理了现有com.Example.test的情况

由于方法
checkDoesNotExist
在内部类中,并且不是由
IResource
声明的,因此它不在公共API中,我无法在调用
IFolder\create
之前使用它进行检查。在这种情况下,
IResource\exists
方法是无用的,因为它区分大小写

目前我有以下解决方案:

/**
 * This method checks if a package or its part exists in the given source folder with a different case.
 * 
 * @param pfr A source folder where to look package in.
 * @param packageName Name of the package, e.g. "com.example.test"
 * @return A String containing path of the existing resource relative to the project, null if the package name has no conflicts.
 * @throws CoreException
 * @throws IOException
 */
public static String checkPackageDoesExistWithDifferentCase(IPackageFragmentRoot pfr, String packageName)
    throws CoreException, IOException
{
    IPath p = pfr.getResource().getLocation();
    String[] packagePathSegments = packageName.split("\\.");
    for (int i = 0; i < packagePathSegments.length; i++)
    {
        p = p.append(packagePathSegments[i]);
        File f = new File(p.toString());
        String canonicalPath = f.getCanonicalPath();
        if (f.exists() && !canonicalPath.equals(p.toOSString()))
            return canonicalPath.substring(pfr.getJavaProject().getResource().getLocation().toOSString().length() + 1);
    }
    return null;
}
/**
*此方法检查给定源文件夹中是否存在大小写不同的包或其部分。
* 
*@param pfr查找包的源文件夹。
*@param packageName包的名称,例如“com.example.test”
*@返回一个包含现有资源相对于项目的路径的字符串,如果包名没有冲突,则返回null。
*@core异常
*@抛出异常
*/
公共静态字符串检查包DoesexistWithDifferentCase(IPackageFragmentRoot pfr,String packageName)
抛出CoreException,IOException
{
IPath p=pfr.getResource().getLocation();
字符串[]packagePathSegments=packageName.split(“\\”);
对于(int i=0;i

这个解决方案的问题是它只能在Windows上工作,因为
f.exists()
会在区分大小写的文件系统上返回
false

正如您所指出的,没有API,但是如果父资源有一个名称相同(不区分大小写)的成员,您可以检查它,如下所示:

IContainer parent = newFolder.getParent();
IResource[] members = parent.members();
for( IResource resource : members ) {
  if( resource.getName().equalsIgnoreCase( newFolder.getName() ) ) {
    ...
  }
}

可以在不使用内部方法的情况下检查ResourceException是否存在该错误,但是如果方法成功,我需要删除创建的文件夹,因为我只需要在向导页面上检查验证的存在性,并且在每个键入的字符之后执行该操作将效率低下。感谢您的建议。此解决方案的问题在于,它无法确定是否存在名称完全相同的成员或其名称大小写不同的成员。除此之外,我想知道的不仅仅是资源,而是它的完整路径。请看编辑后的答案。