Search in sources :

Example 1 with CompilationUnitRewrite

use of org.autorefactor.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project AutoRefactor by JnRouvignac.

the class ApplyRefactoringsJob method applyRefactoring.

/**
 * Applies the cleanups provided inside the {@link AggregateASTVisitor} to
 * the provided {@link ICompilationUnit}.
 *
 * @param document        the document where the compilation unit comes from
 * @param compilationUnit the compilation unit to refactor
 * @param refactoring     the {@link AggregateASTVisitor} to apply to the
 *                        compilation unit
 * @param options         the Java project options used to compile the project
 * @param monitor         the progress monitor of the current job
 * @param hasToSave       hasToSave
 * @return TextEdit
 * @throws Exception if any problem occurs
 *
 * @see <a href=
 *      "http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_manip.htm"
 *      >Eclipse JDT core - Manipulating Java code</a>
 * @see <a href=
 *      "http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench_cmd_menus.htm"
 *      > Eclipse Platform Plug-in Developer Guide > Plugging into the workbench
 *      > Basic workbench extension points using commands >
 *      org.eclipse.ui.menus</a>
 * @see <a href=
 *      "http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html"
 *      >Abstract Syntax Tree > Write it down</a>
 */
public List<TextEdit> applyRefactoring(final IDocument document, final ICompilationUnit compilationUnit, final AggregateASTVisitor refactoring, final JavaProjectOptions options, final SubMonitor monitor, final boolean hasToSave) throws Exception {
    // Creation of DOM/AST from a ICompilationUnit
    @SuppressWarnings("deprecation") ASTParser parser = ASTParser.newParser(AST.JLS8);
    int maxIterations = 100;
    int iterationCount = 0;
    Set<ASTVisitor> lastLoopVisitors = Collections.emptySet();
    int nbLoopsWithSameVisitors = 0;
    List<TextEdit> textEdits = new ArrayList<>();
    monitor.setWorkRemaining(maxIterations);
    CompilationUnit astRoot;
    do {
        // I did not find any other way to directly modify the AST
        // while still keeping the resolved type bindings working.
        // Using astRoot.recordModifications() did not work:
        // type bindings were lost. Is there a way to recover them?
        // FIXME we should find a way to apply all the changes at
        // the AST level and refresh the bindings
        resetParser(compilationUnit, parser, options);
        astRoot = (CompilationUnit) parser.createAST(null);
        if (iterationCount > maxIterations) {
            // Oops! Something went wrong.
            String errorMsg = // $NON-NLS-1$ //$NON-NLS-2$
            "An infinite loop has been detected for file " + ASTNodes.getFileName(astRoot) + "." + // $NON-NLS-1$
            " A possible cause is that code is being incorrectly" + " refactored one way then refactored back to what it was." + // $NON-NLS-1$ //$NON-NLS-2$
            " Fix the code before pursuing." + getPossibleCulprits(nbLoopsWithSameVisitors, lastLoopVisitors);
            environment.getLogger().error(errorMsg, new IllegalStateException(astRoot, errorMsg));
            break;
        }
        CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite(compilationUnit, astRoot, options, monitor, environment);
        refactoring.setRefactoringContext(cuRewrite);
        ASTRewrite refactorings = refactoring.getRefactorings(astRoot);
        if (!refactorings.hasRefactorings()) {
            // We are done with applying the cleanups.
            break;
        }
        // Apply the cleanups and save the compilation unit
        refactorings.applyTo(document, hasToSave);
        textEdits.add(refactorings.getEdits());
        if (!hasToSave) {
            return textEdits;
        }
        boolean hadUnsavedChanges = compilationUnit.hasUnsavedChanges();
        compilationUnit.getBuffer().setContents(document.get());
        // , null, null);
        if (!hadUnsavedChanges && hasToSave) {
            compilationUnit.save(null, true);
        }
        iterationCount++;
        Set<ASTVisitor> thisLoopVisitors = refactoring.getVisitorsContributingRefactoring();
        if (thisLoopVisitors.equals(lastLoopVisitors)) {
            nbLoopsWithSameVisitors++;
        } else {
            lastLoopVisitors = new HashSet<>(thisLoopVisitors);
            nbLoopsWithSameVisitors = 0;
        }
    } while (true);
    return textEdits;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompilationUnitRewrite(org.autorefactor.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IllegalStateException(org.autorefactor.util.IllegalStateException) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AggregateASTVisitor(org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ArrayList (java.util.ArrayList)1 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)1 CompilationUnitRewrite (org.autorefactor.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)1 AggregateASTVisitor (org.autorefactor.jdt.internal.ui.fix.AggregateASTVisitor)1 IllegalStateException (org.autorefactor.util.IllegalStateException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 ASTParser (org.eclipse.jdt.core.dom.ASTParser)1 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 TextEdit (org.eclipse.text.edits.TextEdit)1