Search in sources :

Example 26 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class SwitchRefactoring method mergeCases.

private void mergeCases(Merge merge, SwitchCaseSection sectionToKeep, SwitchCaseSection sectionToRemove) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final Statement caseKept;
    if (merge == Merge.BEFORE_SWITCH_CASES) {
        caseKept = sectionToKeep.existingCases.get(0);
    } else {
        // move == Move.AFTER_SWITCH_CASES
        caseKept = sectionToKeep.stmts.get(0);
    }
    for (final SwitchCase caseToMove : sectionToRemove.existingCases) {
        r.insertBefore(b.move(caseToMove), caseKept);
    }
    r.remove(sectionToRemove.stmts);
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 27 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class UseMultiCatchRefactoring method visit.

@Override
public boolean visit(TryStatement node) {
    List<CatchClause> catchClauses = catchClauses(node);
    Binding[] typeBindings = resolveTypeBindings(catchClauses);
    for (int i = 0; i < catchClauses.size(); i++) {
        CatchClause catchClause1 = catchClauses.get(i);
        for (int j = i + 1; j < catchClauses.size(); j++) {
            CatchClause catchClause2 = catchClauses.get(j);
            MergeDirection direction = mergeDirection(typeBindings, i, j);
            if (!MergeDirection.NONE.equals(direction) && matchMultiCatch(catchClause1, catchClause2)) {
                Refactorings r = this.ctx.getRefactorings();
                UnionType ut = unionTypes(catchClause1.getException().getType(), catchClause2.getException().getType());
                if (MergeDirection.UP.equals(direction)) {
                    r.set(catchClause1.getException(), SingleVariableDeclaration.TYPE_PROPERTY, ut);
                    r.remove(catchClause2);
                } else if (MergeDirection.DOWN.equals(direction)) {
                    r.remove(catchClause1);
                    r.set(catchClause2.getException(), SingleVariableDeclaration.TYPE_PROPERTY, ut);
                }
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) UnionType(org.eclipse.jdt.core.dom.UnionType) Refactorings(org.autorefactor.refactoring.Refactorings) CatchClause(org.eclipse.jdt.core.dom.CatchClause)

Aggregations

Refactorings (org.autorefactor.refactoring.Refactorings)27 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)23 Expression (org.eclipse.jdt.core.dom.Expression)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 Statement (org.eclipse.jdt.core.dom.Statement)5 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)3 ArrayList (java.util.ArrayList)2 TypeNameDecider (org.autorefactor.refactoring.TypeNameDecider)2 Variable (org.autorefactor.refactoring.Variable)2 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 DoStatement (org.eclipse.jdt.core.dom.DoStatement)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2