如何为自定义 Java 标记添加 Eclipse 快速修复程序?

2022-09-03 15:57:09

我想将 Java 文件的自定义问题报告给 Eclipse 的问题视图,并为其提供快速修复。

标准方法是使用扩展点声明自定义标记,并通过调用 来添加标记。然后,可以使用扩展点为自定义标记提供快速修复。org.eclipse.core.resources.markersorg.eclipse.core.resources.IResource.createMarker(String)org.eclipse.ui.ide.markerResolution

上述方法是一种与语言无关的创建和解析资源标记的方法。缺点是我必须编写一些样板代码来解决我的自定义Java问题。相反,我想重用 。也就是说,我想使用扩展点解决我的自定义Java标记。使用此扩展点,我不再需要解析在其中找到标记的Java文件,我不必构建绑定并找到覆盖标记的AST节点。如果我不重用及其依赖项,我最终会复制大部分内容。IQuickFixProcessororg.eclipse.jdt.ui.quickFixProcessorsorg.eclipse.jdt.internal.ui.text.correction.CorrectionMarkerResolutionGenerator

如何使用 JDT 基础结构为自定义 Java 标记提供快速修复?

尝试 1:

我定义了我的自定义标记,如下所示:

<extension
  id="custom.marker"
  name="Custom Java Problem"
  point="org.eclipse.core.resources.markers">
    <super type="org.eclipse.jdt.core.problem"/>
    <super type="org.eclipse.core.resources.problemmarker"/>
    <super type="org.eclipse.core.resources.textmarker"/>
    <persistent value="true"/>
</extension>

然后,我通过调用方法添加了上述标记的实例。IResource.createMarker("custom.marker")

接下来,我定义了一个自定义的快速修复处理器。

<extension
  point="org.eclipse.jdt.ui.quickFixProcessors">
  <quickFixProcessor
    class="quickfixes.CustomQuickFixProcessor"
    id="quickfixes.quickFixProcessor">
  </quickFixProcessor>
</extension>

我的自定义标记显示在 Eclipse 的问题视图中,但是当我右键单击自定义问题时,“快速修复”菜单项被禁用。

尝试 2:

我回复了.由于此更改,当我在“问题”视图中右键单击自定义问题时,“快速修复”菜单项将变为可用,但是,当我选择它时,会弹出一个对话框,指出没有可用于所选问题的修补程序。但是,我验证了 被调用并返回 ,但是,不会被调用。IMarker marker = resource.createMarker("custom.marker");IMarker marker = resource.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);CustomQuickFixProcessor.hasCorrections(ICompilationUnit, int)trueCustomQuickFixProcessor.getCorrections(IInvocationContext, IProblemLocation[])

尝试 3:

尝试 3 是尝试 2 的延续。我按如下方式设置自定义标记:IJavaModelMarker.ID

marker.setAttribute(IJavaModelMarker.ID, IProblem.ExternalProblemFixable);

因此,当我将鼠标悬停在编辑器中的自定义标记上或单击 Java 编辑器左边缘的轻量级构建时,将调用。但是,当我在问题视图中选择标记时,右键单击该标记,然后选择“快速修复”菜单项,不会被调用,并且会出现一个对话框,指出没有可用的快速修复。CustomQuickFixProcessor.getCorrectionsCustomQuickFixProcessor.getCorrections

我在调试模式下运行了 JDT,以了解为什么当我从问题视图调用快速修复时,它不调用。事实证明,没有找到解决方案,因为在编译单元的AST中没有找到自定义问题。我不确定如何将我的自定义标记与编译单元的 AST 相关联。CustomQuickFixProcessor.getCorrectionsCorrectionMarkerResolutionGenerator.internalGetResolutions(IMarker)CorrectionMarkerResolutionGenerator.hasProblem (context.getASTRoot().getProblems(), location)


答案 1

在这篇文章和调试器的大力帮助下,我得到了这个工作。以下是您必须执行的操作:

创建标记

插件.xml

声明标记,以便它扩展这三个现有标记(我认为所有这些都是必要的)

<extension
       id="mymarker"
       name="My Problem"
       point="org.eclipse.core.resources.markers">
    <super
          type="org.eclipse.jdt.core.problem">
    </super>
    <super
          type="org.eclipse.core.resources.problemmarker">
    </super>
    <super
          type="org.eclipse.core.resources.textmarker">
    </super>
    <persistent
          value="true">
    </persistent>
 </extension>

爪哇岛

创建标记时,设置字段非常重要,我认为此处列出的所有其他字段也很重要。IJavaModelMarker.ID

// Must match the "id" attribute from plugin.xml
String MY_MARKER_ID = "com.example.my.plugin.mymarker"
// Must not be -1 or any of the values in org.eclipse.jdt.core.compiler.IProblem
int MY_JDT_PROBLEM_ID = 1234

// ....
IMarker marker = resource.createMarker(MY_MARKER_ID);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.MESSAGE, msg);
marker.setAttribute(IMarker.CHAR_START, start);
marker.setAttribute(IMarker.CHAR_END, end);
marker.setAttribute(IJavaModelMarker.ID, MY_JDT_PROBLEM_ID);

创建快速修复处理器

插件.xml

首先在 中声明它。确保您的声明权利plugin.xmlidhandledMarkerTypes

<extension
      point="org.eclipse.jdt.ui.quickFixProcessors">
   <quickFixProcessor
         class="com.example.my.plugin.ui.MyQuickFixProcessor"
         id="org.eclipse.jdt.ui.text.correction.QuickFixProcessor"
         name="My Quick Fix Processor">
      <handledMarkerTypes>
         <markerType
               id="com.example.my.plugin.mymarker">
         </markerType>
      </handledMarkerTypes>
   </quickFixProcessor>
</extension>

爪哇岛

以下是快速修复处理器的基本框架。请注意,检查是否实际包含内容非常重要。locations

如果您创建自己的标记类型(如上所述),我认为您可以硬编码以返回true。但是要保存并遵循约定,请检查它是否与您的jdt问题ID匹配。hasCorrections

public class MyQuickFixProcessor implements IQuickFixProcessor {

  @Override
  public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    if (locations == null || locations.length == 0) {
      // https://bugs.eclipse.org/444120 Eclipse can call this method without
      // any locations, if a quick fix is requested without any problems.
      return null;
    }

    IJavaCompletionProposal[] proposals = ...
    //...
    return proposals;
  }

  @Override
  public boolean hasCorrections(ICompilationUnit unit, int problemId) {
    return problemId == MY_JDT_PROBLEM_ID;
  }
}

找到一个好的JDT ID

你需要一个独特的!运行下面的代码,以打印 中定义的所有当前 ID。在这些数字中选择一个相当大的范围,然后在该范围内选择您的 ID。MY_JDT_PROBLEM_IDIProblem

Field[] fields = org.eclipse.jdt.core.compiler.IProblem.class.getFields();
List<Integer> ints = new ArrayList<>();
for (Field field : fields) {
  ints.add(field.getInt(null));
}
sort(ints);
for (Integer integer : ints) {
  System.out.printf("%16d %16o %16x%n", integer, integer, integer);
}

我希望我已经记住了一切。祝你好运。


答案 2

您的尝试 1 将不起作用,原因:

JDT UI 定义了以下扩展。

   <extension
         point="org.eclipse.ui.ide.markerResolution">
      <markerResolutionGenerator
            markerType="org.eclipse.jdt.core.problem"
            class="org.eclipse.jdt.internal.ui.text.correction.CorrectionMarkerResolutionGenerator">
      </markerResolutionGenerator>

也就是说,来自JDT的快速修复仅适用于markerType=“org.eclipse.jdt.core.problem”。

尝试 2 :即使在 JDT UI 实现中,也存在一些情况,当 QuickFixProcessor#hasCorrections(...) 返回 true 但 QuickFixProcessor#getCorrections(...) 可能不会返回修复。这是因为我们总是为特定标记返回 true 或 false。但是,特定标记的所有实例可能不是“可修复的”。也许你遇到类似的事情?

尝试 3 : 您在标记上设置了哪些所有属性?由于会检查它,因此您需要至少设置IMarker.CHAR_START属性。看看 ,这是在JDT Core中创建标记的地方。CorrectionMarkerResolutionGenerator.hasProblem(...)org.eclipse.jdt.internal.core.eval.RequestorWrapper.acceptProblem(CategorizedProblem, char[], int)


推荐