Search in sources :

Example 6 with Refactorings

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

the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.

private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
    final List<Statement> stmts = asList(stmt);
    final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
    if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
        final Statement firstStmt = getFirst(stmts);
        final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
        final ASTMatcher astMatcher = new ASTMatcher();
        if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            final Refactorings r = this.ctx.getRefactorings();
            if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
                // Only one statement: replace if statement with col.add() (or col.remove())
                r.replace(ifStmtToReplace, b.move(firstStmt));
                return DO_NOT_VISIT_SUBTREE;
            } else {
                // There are other statements, replace the if condition with col.add() (or col.remove())
                r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
                r.remove(firstStmt);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 7 with Refactorings

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

the class MapEliminateKeySetCallsRefactoring method replaceEntryIterationByKeyIteration.

private void replaceEntryIterationByKeyIteration(EnhancedForStatement enhancedFor, final Expression mapExpression, final SingleVariableDeclaration parameter, final List<MethodInvocation> getValueMis) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    final VariableDefinitionsUsesVisitor keyUseVisitor = new VariableDefinitionsUsesVisitor(parameter);
    enhancedFor.getBody().accept(keyUseVisitor);
    int keyUses = keyUseVisitor.getUses().size();
    final int insertionPoint = asList(enhancedFor.getBody()).get(0).getStartPosition() - 1;
    final Variable entryVar = new Variable(new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("entry", "mapEntry"), b);
    final TypeNameDecider typeNameDecider = new TypeNameDecider(parameter);
    final MethodInvocation getValueMi0 = getValueMis.get(0);
    final ITypeBinding typeBinding = getValueMi0.getExpression().resolveTypeBinding();
    if (typeBinding != null && typeBinding.isRawType()) {
        // for (Object key : map.keySet()) => for (Object key : map.entrySet())
        r.set(enhancedFor, EXPRESSION_PROPERTY, b.invoke(b.move(mapExpression), "entrySet"));
        final Type objectType = b.type(typeNameDecider.useSimplestPossibleName("java.lang.Object"));
        final Variable objectVar = new Variable(new VariableNameDecider(enhancedFor.getBody(), insertionPoint).suggest("obj"), b);
        r.set(enhancedFor, PARAMETER_PROPERTY, b.declareSingleVariable(objectVar.varNameRaw(), objectType));
        // for (Map.Entry<K, V> mapEntry : map.entrySet()) {
        // Map.Entry mapEntry = (Map.Entry) obj; // <--- add this statement
        // Object key = mapEntry.getKey(); // <--- add this statement
        final Type mapKeyType = b.copy(parameter.getType());
        final VariableDeclarationStatement newKeyDecl = b.declareStmt(mapKeyType, b.move(parameter.getName()), b.invoke(entryVar.varName(), "getKey"));
        r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newKeyDecl);
        if (keyUses > getValueMis.size()) {
            String mapEntryTypeName = typeNameDecider.useSimplestPossibleName("java.util.Map.Entry");
            final VariableDeclarationStatement newEntryDecl = b.declareStmt(b.type(mapEntryTypeName), entryVar.varName(), b.cast(b.type(mapEntryTypeName), objectVar.varName()));
            r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newEntryDecl);
        }
    } else {
        // for (K key : map.keySet()) => for (K key : map.entrySet())
        r.set(enhancedFor, EXPRESSION_PROPERTY, b.invoke(b.move(mapExpression), "entrySet"));
        // for (K key : map.entrySet()) => for (Map.Entry<K, V> mapEntry : map.entrySet())
        final Type mapEntryType = createMapEntryType(parameter, getValueMi0, typeNameDecider);
        r.set(enhancedFor, PARAMETER_PROPERTY, b.declareSingleVariable(entryVar.varNameRaw(), mapEntryType));
        if (keyUses > getValueMis.size()) {
            // for (Map.Entry<K, V> mapEntry : map.entrySet()) {
            // K key = mapEntry.getKey(); // <--- add this statement
            final Type mapKeyType = b.copy(parameter.getType());
            final VariableDeclarationStatement newKeyDeclaration = b.declareStmt(mapKeyType, b.move(parameter.getName()), b.invoke(entryVar.varName(), "getKey"));
            r.insertFirst(enhancedFor.getBody(), Block.STATEMENTS_PROPERTY, newKeyDeclaration);
        }
    }
    // Replace all occurrences of map.get(key) => mapEntry.getValue()
    for (MethodInvocation getValueMi : getValueMis) {
        r.replace(getValueMi, b.invoke(entryVar.varName(), "getValue"));
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) Variable(org.autorefactor.refactoring.Variable) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Refactorings(org.autorefactor.refactoring.Refactorings) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TypeNameDecider(org.autorefactor.refactoring.TypeNameDecider) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 8 with Refactorings

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

the class RemoveUselessBlockRefactoring method replaceBlock.

@SuppressWarnings("unchecked")
private void replaceBlock(final Block node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    r.replace(node, b.copyRange(node.statements()));
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 9 with Refactorings

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

the class SimplifyExpressionRefactoring method maybeReduceBooleanExpression.

private boolean maybeReduceBooleanExpression(final InfixExpression node, final Expression leftExpr, final Expression rightExpr) {
    final Boolean leftBoolean = getBooleanLiteral(leftExpr);
    final Boolean rightBoolean = getBooleanLiteral(rightExpr);
    if (leftBoolean != null) {
        return replace(node, leftBoolean.booleanValue(), rightExpr);
    } else if (rightBoolean != null) {
        return replace(node, rightBoolean.booleanValue(), leftExpr);
    }
    Expression leftOppositeExpr = null;
    final PrefixExpression leftPrefix = as(leftExpr, PrefixExpression.class);
    if (leftPrefix != null && hasOperator(leftPrefix, NOT)) {
        leftOppositeExpr = leftPrefix.getOperand();
    }
    Expression rightOppositeExpr = null;
    final PrefixExpression rightPrefix = as(rightExpr, PrefixExpression.class);
    if (rightPrefix != null && hasOperator(rightPrefix, NOT)) {
        rightOppositeExpr = rightPrefix.getOperand();
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    if (leftOppositeExpr != null && rightOppositeExpr != null) {
        r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), getAppropriateOperator(node), b.copy(rightOppositeExpr)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (leftOppositeExpr != null) {
        final Operator reverseOp = getReverseOperator(node);
        r.replace(node, b.infixExpr(b.copy(leftOppositeExpr), reverseOp, b.copy(rightExpr)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (rightOppositeExpr != null) {
        final Operator reverseOp = getReverseOperator(node);
        r.replace(node, b.infixExpr(b.copy(leftExpr), reverseOp, b.copy(rightOppositeExpr)));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 10 with Refactorings

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

the class StaticConstantRatherThanInstanceConstantRefactoring method addStaticModifier.

private void addStaticModifier(final Modifier finalModifier) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    r.insertBefore(b.static0(), finalModifier);
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

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