How to write Eclipse QuickFix for multiple problems in Java-Source files

During the refactoring of some legacy codes, it became necessary to create your own Eclipse Quick Fix in order to make minor adjustments to the code. This (in itself) was fairly simple after this article (in German): http://jaxenter.de/artikel/Eclipse-JDT-um-eigene-Quickfixes-erweitern

A quick fix ( IQuickFixProcessor) is added through the extension point org.eclipse.jdt.ui.quickFixProcessorsthat it creates IJavaCompletionProposalto do the job. IQuickFixProcessorhas an AST available for code changes.

The problem that I am facing right now is that I can only apply Quick Fix to one problem at a time. If I select several problems (all of the same types, therefore my own quick fix is ​​applicable), I get the error message "The selected problems do not have a common applicable quick fix."

How to create a Quick Fix that can be used for several problems of the same type?

Using an extension point org.eclipse.ui.ide.markerResolution, as suggested by Acanda, seems very difficult to implement Java source files. For one, there is no AST, only an instance IMarker. How to get AST CompilationUnitand insult ASTNodefor IMarker?

More general: is there an API in JDT for working with IMarkerinstances ??

+4
2

, org.eclipse.ui.views.markers.WorkbenchMarkerResolution org.eclipse.ui.ide.markerResolution. Eclipse eclipse-pmd:

<extension
     point="org.eclipse.ui.ide.markerResolution">
   <markerResolutionGenerator
        class="ch.acanda.eclipse.pmd.java.resolution.PMDMarkerResolutionGenerator"
        markerType="ch.acanda.eclipse.pmd.core.pmdMarker">
   </markerResolutionGenerator>
</extension>

CompilationUnit IMarker ASTNode. ASTQuickFix eclipse-pmd. CompilationUnit IMarker ASTNode , node, . NodeWithinPositionNodeFinder.

+4

ASTNode IMarker:

public ASTNode getASTNodeFromMarker(IMarker marker) {
    IResource res = marker.getResource();
    if (res.getType() == IResource.FILE) {
        IFile f = (IFile)res;
        ICompilationUnit cu = (ICompilationUnit)JavaCore.create(f);
        CompilationUnit astRoot = getAstRoot(cu);
        int start = marker.getAttribute(IMarker.CHAR_START, 0);
        int end = marker.getAttribute(IMarker.CHAR_END, 0);
        NodeFinder nf = new NodeFinder(astRoot, start, end-start);
        return nf.getCoveringNode();
    }
    return null;
}

private CompilationUnit getAstRoot(ITypeRoot typeRoot) {
    CompilationUnit root = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
    if (root == null) {
        ASTParser astParser = ASTParser.newParser(AST.JLS8);
        astParser.setSource(typeRoot);
        astParser.setResolveBindings(true);
        astParser.setStatementsRecovery(true);
        astParser.setBindingsRecovery(true);
        root = (CompilationUnit)astParser.createAST(null);
    }
    return root;  // may return null if no source available for typeRoot
}
+1

Source: https://habr.com/ru/post/1525766/


All Articles