首页 > 解决方案 > Eclipse插件开发:问题视图中没有出现自定义问题标记

问题描述

我正在开发一个 Eclipse 插件,我在其中声明了一个自定义问题标记。它具有org.eclipse.core.resources.problemmarker其超类型,并且persistent属性设置为 true。我贡献了一个自定义CategorizedProblem类,其中getMarkerType返回我的自定义标记的 id,我将它添加到类中,使用该方法reconcile的自定义方法中存在问题。还声明了属性下的标记类型:CompilationParticipantReconcileContext.putProblemsCompilationParticipantmanagedMarker

plugin.xml 的相关部分:

<extension
      point="org.eclipse.jdt.core.compilationParticipant">
   <compilationParticipant
         class="no.tobask.sb4e.FxControllerValidator"
         id="FxControllerValidator"
         createsProblems="true">
         <managedMarker
               markerType="no.tobask.sb4e.fxcontrollerproblemmarker">
         </managedMarker>
   </compilationParticipant>
</extension>
<extension
      id="no.tobask.sb4e.fxcontrollerproblemmarker"
      point="org.eclipse.core.resources.markers">
   <super
         type="org.eclipse.core.resources.problemmarker">
   </super>
   <persistent
         value="true">
   </persistent>
</extension>

来自 CompilationParticipant 的相关代码:

public void reconcile(ReconcileContext context) {
    ICompilationUnit clazz = context.getWorkingCopy();
    String className = clazz.findPrimaryType().getFullyQualifiedName();
    if (documentListener.isAssignedController(className)) {
        URL documentLocation = documentListener.getDocument(className);
        try {
            String fxmlContent = FXOMDocument.readContentFromURL(documentLocation);
            FXOMDocument document = new FXOMDocument(fxmlContent, documentLocation, Activator.getClassLoader(),
                    I18N.getBundle());
            CategorizedProblem[] problems = getProblems(context.getAST(AST.JLS11), document);
            if (problems != null) {
                context.putProblems("no.tobask.sb4e.fxcontrollerproblemmarker", problems);
            }
        } catch (IOException | JavaModelException e) {
            e.printStackTrace();
        }
    }
}

分类问题:

public class FxControllerProblem extends CategorizedProblem {

    List<String> missingIds;
    IResource resource;

    public FxControllerProblem(List<String> missingIds, IResource resource) {
        this.missingIds = missingIds;
        this.resource = resource;
    }

    @Override
    public String[] getArguments() {
        return missingIds.toArray(new String[0]);
    }

    @Override
    public int getID() {
        return 1234;
    }

    @Override
    public String getMessage() {
        return "One or more fx:ids from associated FXML document do not have corresponding fields";
    }

    @Override
    public char[] getOriginatingFileName() {
        return resource.getName().toCharArray();
    }

    @Override
    public int getSourceEnd() {
        return -1;
    }

    @Override
    public int getSourceLineNumber() {
        return 1;
    }

    @Override
    public int getSourceStart() {
        return 0;
    }

    @Override
    public boolean isError() {
        return false;
    }

    @Override
    public boolean isWarning() {
        return true;
    }

    @Override
    public void setSourceEnd(int sourceEnd) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setSourceLineNumber(int lineNumber) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setSourceStart(int sourceStart) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getCategoryID() {
        return CAT_MEMBER;
    }

    @Override
    public String getMarkerType() {
        return "no.tobask.sb4e.fxcontrollerproblemmarker";
    }

}

问题是,虽然警告确实在 Java 编辑器中正确显示,但它没有显示在问题视图中。

标签: eclipse-plugin

解决方案


推荐阅读