IPathVariableManager.validateValue(URI)返回null

时间:2012-03-22 22:09:05

标签: eclipse-plugin uri

在我正在研究的Eclipse插件中,我有一段基本上看起来像这样的代码:

public static void checkProblemV1() {
   try {
      String path = "C:\\temp";
      org.eclipse.core.runtime.IPath value = 
         new org.eclipse.core.runtime.Path(path);
      IPathVariableManager pathManager = 
         ResourcesPlugin.getWorkspace().getPathVariableManager();
      String name = "somename";
      IStatus statusName = pathManager.validateName(name);
      IStatus statusValue = pathManager.validateValue(value);

      if (statusName == null || statusValue == null) {
         System.err.println("checkProblemV1(): statusName is " +
            (statusName == null ? "null" : ("not null: '" + statusName + "'.")));
         System.err.println("checkProblemV1(): statusValue is " +
            (statusValue == null ? "null" : ("not null: '" + statusValue + "'.")));
      }
      else if (statusName.isOK() && statusValue.isOK()) {
         pathManager.setValue(name, value); // setValue is deprecated
         System.out.println("checkProblemV1(): Everything fine");
      }
      else {
         if (!statusName.isOK()) {
            System.err.println("checkProblemV1(): statusName is not OK.");
         }
         if (!statusValue.isOK()) {
            System.err.println("checkProblemV1(): statusValue is not OK.");
         }
      }
   }
   catch (CoreException e) {
      System.err.println("checkProblemV1(): CoreException: " + e.getMessage());
   }
}

执行上述方法时,没有问题,但编译时我收到setValue()的弃用警告。当我想通过调用setURIValue()替换对setValue()的调用来修复那个弃用警告时,我遇到了麻烦。首先,我必须调整如何编写Windows风格的路径以避免URISyntaxException但是在执行此操作后,setURIValue()返回null并且在查看文档后我看不到它应该这样做?我想URI毕竟是无效的,但不知道如何解决它。以下是与上述相同风格的测试方法,用于说明问题。

public static void checkProblemV2() {
   try {
      String path = "C:\\temp";
      // Have to replace \ with /, or we get URISyntaxExeption:
      // checkProblemV2(): URISyntaxException: Illegal character in opaque part at index 2: C:\temp
      path = path.replace('\\', '/');
      java.net.URI value = new java.net.URI(path);
      IPathVariableManager pathManager = 
         ResourcesPlugin.getWorkspace().getPathVariableManager();
      String name = "somename";
      IStatus statusName = pathManager.validateName(name);
      IStatus statusValue = pathManager.validateValue(value);

      if (statusName == null || statusValue == null) {
         System.err.println("checkProblemV2(): statusName is " +
            (statusName == null ? "null" : ("not null: '" + statusName + "'.")));
         System.err.println("checkProblemV2(): statusValue is " +
            (statusValue == null ? "null" : ("not null: '" + statusValue + "'.")));
      }
      else if (statusName.isOK() && statusValue.isOK()) {
         pathManager.setURIValue(name, value);
         System.out.println("checkProblemV2(): Everything fine");
      }
      else {
         if (!statusName.isOK()) {
            System.err.println("checkProblemV2(): statusName is not OK.");
         }
         if (!statusValue.isOK()) {
            System.err.println("checkProblemV2(): statusValue is not OK.");
         }
      }
   }
   catch (URISyntaxException e) {
      System.err.println("checkProblemV2(): URISyntaxException: " + e.getMessage());
   }
   catch (CoreException e) {
      System.err.println("checkProblemV2(): CoreException: " + e.getMessage());
   }
}

当我运行上述两种方法时,我得到以下输出。

checkProblemV1(): Everything fine
checkProblemV2(): statusName is not null: 'Status OK: unknown code=0 OK null'.
checkProblemV2(): statusValue is null

感谢阅读和任何帮助,我们非常感谢!

1 个答案:

答案 0 :(得分:0)

org.eclipse.core.internal.resources.PathVariableManager.validateValue(URI)中有一个错误,它使用其他内部方法,但无论发生什么,都会返回null。请打开Eclipse Platform/Resources的错误。

我同意你的评论,你应该使用:

URI uri = new File("C:\\temp").toURI();
pathManager.setURIValue(theName, uri);