Search in sources :

Example 16 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class StringBuilderCleanUp method maybeReplaceWithNewStringAppends.

/**
 * Rewrite the successive calls to append()
 *
 * @param visited                     The node to replace.
 * @param allAppendedStrings          All appended strings.
 * @param lastExpression              The expression on which the methods are
 *                                    called.
 * @param isInstanceCreationToRewrite
 * @return
 */
private boolean maybeReplaceWithNewStringAppends(final Expression visited, final List<Pair<ITypeBinding, Expression>> allAppendedStrings, final Expression lastExpression, final boolean isInstanceCreationToRewrite) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
    Expression result = null;
    List<Expression> tempStringLiterals = new ArrayList<>();
    List<Expression> finalStrings = new ArrayList<>();
    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (Pair<ITypeBinding, Expression> appendedString : allAppendedStrings) {
        if (appendedString == null) {
            return true;
        }
        if (isValuedStringLiteralOrConstant(appendedString.getSecond())) {
            tempStringLiterals.add(ASTNodes.createMoveTarget(rewrite, appendedString.getSecond()));
        } else {
            result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
            if (isFirst.get()) {
                isFirst.lazySet(false);
                if (!isInstanceCreationToRewrite) {
                    result = ASTNodes.createMoveTarget(rewrite, lastExpression);
                    finalStrings.add(getTypedExpression(appendedString));
                } else if (appendedString.getFirst() != null ? ASTNodes.hasType(appendedString.getFirst(), String.class.getCanonicalName()) : ASTNodes.hasType(appendedString.getSecond(), String.class.getCanonicalName())) {
                    result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()), getTypedExpression(appendedString));
                } else {
                    result = ast.newClassInstanceCreation(ASTNodes.createMoveTarget(rewrite, ((ClassInstanceCreation) lastExpression).getType()));
                    finalStrings.add(getTypedExpression(appendedString));
                }
            } else {
                finalStrings.add(getTypedExpression(appendedString));
            }
        }
    }
    result = handleTempStringLiterals(lastExpression, isInstanceCreationToRewrite, result, tempStringLiterals, finalStrings, isFirst);
    for (Expression finalString : finalStrings) {
        if (result == null) {
            result = finalString;
        } else {
            MethodInvocation appendMethod = ast.newMethodInvocation();
            appendMethod.setExpression(result);
            // $NON-NLS-1$
            appendMethod.setName(ast.newSimpleName("append"));
            appendMethod.arguments().add(finalString);
            result = appendMethod;
        }
    }
    rewrite.replace(visited, result, group);
    return false;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 17 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class StringBuilderCleanUp method getTypedExpression.

private Expression getTypedExpression(final Pair<ITypeBinding, Expression> typeAndValue) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    if (typeAndValue.getFirst() != null) {
        CastExpression newCastExpression = ast.newCastExpression();
        newCastExpression.setType(ast.type(typeAndValue.getFirst().getQualifiedName()));
        newCastExpression.setExpression(ASTNodeFactory.parenthesizeIfNeeded(ast, ast.createCopyTarget(typeAndValue.getSecond())));
        return newCastExpression;
    }
    if (typeAndValue.getFirst() == null) {
        return ast.createCopyTarget(typeAndValue.getSecond());
    }
    return null;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 18 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class RemoveEmptyStatementCleanUp method visit.

@Override
public boolean visit(final EmptyStatement visited) {
    if (isEmptyCode(visited)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.RemoveEmptyStatementCleanUp_description);
        if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            rewrite.remove(visited, group);
            return false;
        }
        if (visited instanceof EmptyStatement) {
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), group);
            return false;
        }
    }
    return true;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 19 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class SeparateAssertionsRatherThanBooleanExpressionCleanUp method maybeRefactorMethod.

private boolean maybeRefactorMethod(final ExpressionStatement visited, final MethodInvocation originalMethod, final InfixExpression.Operator operator, final int parameterIndex) {
    InfixExpression booleanExpression = ASTNodes.as((Expression) originalMethod.arguments().get(parameterIndex), InfixExpression.class);
    if (booleanExpression != null && ASTNodes.hasOperator(booleanExpression, operator)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.SeparateAssertionsRatherThanBooleanExpressionCleanUp_description);
        List<Expression> allOperands = ASTNodes.allOperands(booleanExpression);
        rewrite.replace(booleanExpression, ASTNodes.createMoveTarget(rewrite, allOperands.remove(0)), group);
        List<Statement> expressionStatements = new ArrayList<>(allOperands.size());
        for (Expression operand : allOperands) {
            List<Expression> newArguments = new ArrayList<>(originalMethod.arguments().size());
            for (Object argument : originalMethod.arguments()) {
                newArguments.add(rewrite.createCopyTarget(ASTNodes.getUnparenthesedExpression((Expression) argument)));
            }
            newArguments.set(parameterIndex, ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(operand)));
            MethodInvocation newMethod;
            if (originalMethod.getExpression() != null) {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(rewrite.createCopyTarget(originalMethod.getExpression()));
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            } else {
                MethodInvocation newMethodInvocation = ast.newMethodInvocation();
                newMethodInvocation.setExpression(null);
                newMethodInvocation.setName(ast.newSimpleName(originalMethod.getName().getIdentifier()));
                newMethodInvocation.arguments().addAll(newArguments);
                newMethod = newMethodInvocation;
            }
            ExpressionStatement newStatement = ast.newExpressionStatement(newMethod);
            expressionStatements.add(newStatement);
        }
        if (ASTNodes.canHaveSiblings(visited)) {
            Collections.reverse(expressionStatements);
            for (Statement expressionStatement : expressionStatements) {
                rewrite.insertAfter(expressionStatement, visited, group);
            }
        } else {
            expressionStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
            Block newBlock = ast.newBlock();
            newBlock.statements().addAll(expressionStatements);
            ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
        }
        return false;
    }
    return true;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 20 with ASTNodeFactory

use of org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory in project AutoRefactor by JnRouvignac.

the class SetRatherThanMapCleanUp method substituteType.

@Override
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    ITypeBinding origTypeBinding = origType.resolveBinding();
    if (origTypeBinding == null) {
        return null;
    }
    String substitutingType = getSubstitutingClassName(origTypeBinding.getErasure().getQualifiedName());
    if (classesToUseWithImport.contains(substitutingType)) {
        importsToAdd.add(substitutingType);
        substitutingType = getSimpleName(substitutingType);
    }
    TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
    if (origTypeBinding.isParameterizedType()) {
        ITypeBinding[] origTypeArgs = origTypeBinding.getTypeArguments();
        Type[] newTypes;
        if (origTypeArgs.length > 0 && !((ParameterizedType) origType).typeArguments().isEmpty()) {
            newTypes = new Type[1];
            newTypes[0] = ast.toType(origTypeArgs[0], typeNameDecider);
        } else {
            newTypes = new Type[0];
        }
        return ast.newParameterizedType(substitutingType, newTypes);
    }
    return ast.type(substitutingType);
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Aggregations

ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)152 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)133 TextEditGroup (org.eclipse.text.edits.TextEditGroup)115 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 Expression (org.eclipse.jdt.core.dom.Expression)44 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)37 ArrayList (java.util.ArrayList)19 Block (org.eclipse.jdt.core.dom.Block)17 Statement (org.eclipse.jdt.core.dom.Statement)17 IfStatement (org.eclipse.jdt.core.dom.IfStatement)16 Type (org.eclipse.jdt.core.dom.Type)14 TypeNameDecider (org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 List (java.util.List)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)8 Name (org.eclipse.jdt.core.dom.Name)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)6