Search in sources :

Example 6 with IllegalStateException

use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.

the class BooleanRefactoring method getReturnExpression.

private Expression getReturnExpression(MethodDeclaration md, Expression ifCondition) {
    final IMethodBinding methodBinding = md.resolveBinding();
    if (methodBinding == null) {
        return null;
    }
    final String qualifiedName = methodBinding.getReturnType().getQualifiedName();
    final Expression newE = getExpression(ifCondition, qualifiedName, getBooleanName(md));
    if (newE != null) {
        return newE;
    }
    // compilationUnit.java:line number: error message
    throw new IllegalStateException(md, "Did not expect any other return type than boolean or java.lang.Boolean for method " + md.getName().getIdentifier() + ", but found " + qualifiedName);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IllegalStateException(org.autorefactor.util.IllegalStateException) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression)

Example 7 with IllegalStateException

use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.

the class ASTHelper method getEnclosingType.

/**
 * Returns the enclosing type of the provided node.
 * <p>
 * i.e. this returns the most immediate type declaration surrounding the provided node.
 *
 * @param node the start node
 * @return the enclosing type of the provided node, or {@code null}
 */
public static ASTNode getEnclosingType(ASTNode node) {
    final Class<?>[] ancestorClasses = { AbstractTypeDeclaration.class, AnonymousClassDeclaration.class };
    final ASTNode ancestor = getFirstAncestorOrNull(node, ancestorClasses);
    if (ancestor == null) {
        throw new IllegalStateException(node, "Could not find any ancestor for " + Arrays.toString(ancestorClasses) + " and node type " + (node != null ? node.getClass().getSimpleName() : null) + " node.toString() " + node);
    }
    return ancestor;
}
Also used : IllegalStateException(org.autorefactor.util.IllegalStateException) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 8 with IllegalStateException

use of org.autorefactor.util.IllegalStateException 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)

Example 9 with IllegalStateException

use of org.autorefactor.util.IllegalStateException in project AutoRefactor by JnRouvignac.

the class ASTNodes method getEnclosingType.

/**
 * Returns the enclosing type of the provided node.
 * <p>
 * i.e. this returns the most immediate type declaration surrounding the
 * provided node.
 *
 * @param node the start node
 * @return the enclosing type of the provided node, or {@code null}
 */
public static ASTNode getEnclosingType(final ASTNode node) {
    Class<? extends ASTNode>[] ancestorClasses = new Class[] { AbstractTypeDeclaration.class, AnonymousClassDeclaration.class };
    ASTNode ancestor = getFirstAncestorOrNull(node, ancestorClasses);
    if (ancestor == null) {
        throw new IllegalStateException(node, // $NON-NLS-1$ //$NON-NLS-2$
        "Could not find any ancestor for " + Arrays.toString(ancestorClasses) + " and node type " + (node != null ? node.getClass().getSimpleName() : null) + " node.toString() " + // $NON-NLS-1$
        node);
    }
    return ancestor;
}
Also used : IllegalStateException(org.autorefactor.util.IllegalStateException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

IllegalStateException (org.autorefactor.util.IllegalStateException)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 AggregateASTVisitor (org.autorefactor.refactoring.rules.AggregateASTVisitor)2 RefactoringContext (org.autorefactor.refactoring.rules.RefactoringContext)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ArrayList (java.util.ArrayList)1 MatchResult (java.util.regex.MatchResult)1 Matcher (java.util.regex.Matcher)1 CFGEdgeBuilder (org.autorefactor.cfg.CFGEdgeBuilder)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 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)1 ASTHelper.asExpression (org.autorefactor.refactoring.ASTHelper.asExpression)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1 AST (org.eclipse.jdt.core.dom.AST)1