Search in sources :

Example 91 with ASTNodeFactory

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method buildMethod.

private TypeMethodReference buildMethod(final ITypeBinding type, final MethodInvocation method) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TypeNameDecider typeNameDecider = new TypeNameDecider(method);
    TypeMethodReference typeMethodRef = ast.newTypeMethodReference();
    typeMethodRef.setType(ast.toType(type, typeNameDecider));
    typeMethodRef.setName(ASTNodes.createMoveTarget(rewrite, method.getName()));
    return typeMethodRef;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TypeMethodReference(org.eclipse.jdt.core.dom.TypeMethodReference) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 92 with ASTNodeFactory

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

the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method buildField.

private LambdaExpression buildField(final Expression visited, final ITypeBinding type, final boolean straightOrder, final Boolean isNullFirst, final QualifiedName field, final SimpleName name1) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TypeNameDecider typeNameDecider = new TypeNameDecider(field);
    LambdaExpression lambdaExpression = ast.newLambdaExpression();
    ITypeBinding destinationType = ASTNodes.getTargetType(visited);
    boolean isTypeKnown = destinationType != null && ASTNodes.hasType(destinationType, Comparator.class.getCanonicalName()) && destinationType.getTypeArguments() != null && destinationType.getTypeArguments().length == 1 && Utils.equalNotNull(destinationType.getTypeArguments()[0], type);
    if (isTypeKnown && straightOrder && isNullFirst == null) {
        lambdaExpression.parameters().add(ast.newVariableDeclarationFragment(ast.createCopyTarget(name1)));
    } else {
        lambdaExpression.parameters().add(ast.newSingleVariableDeclaration(name1.getIdentifier(), ast.toType(type, typeNameDecider)));
    }
    lambdaExpression.setBody(ast.newFieldAccess(ast.createCopyTarget(name1), ASTNodes.createMoveTarget(rewrite, field.getName())));
    lambdaExpression.setParentheses(false);
    return lambdaExpression;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Comparator(java.util.Comparator)

Example 93 with ASTNodeFactory

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

the class ObsoleteLiteralRatherThanBooleanConstantCleanUp method replaceWithBooleanLiteral.

private void replaceWithBooleanLiteral(final QualifiedName visited, final boolean val) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLiteralRatherThanBooleanConstantCleanUp_description);
    BooleanLiteral booleanLiteral = ast.newBooleanLiteral(val);
    ASTNodes.replaceButKeepComment(rewrite, visited, booleanLiteral, group);
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 94 with ASTNodeFactory

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

the class ObsoleteMergeConditionalBlocksCleanUp method mergeCode.

private void mergeCode(final List<IfStatement> duplicateIfBlocks, final List<Boolean> isThenStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteMergeConditionalBlocksCleanUp_description);
    List<Expression> newConditions = new ArrayList<>(duplicateIfBlocks.size());
    for (int i = 0; i < duplicateIfBlocks.size(); i++) {
        if (isThenStatement.get(i)) {
            newConditions.add(ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, duplicateIfBlocks.get(i).getExpression())));
        } else {
            newConditions.add(ASTNodeFactory.parenthesizeIfNeeded(ast, ast.negate(duplicateIfBlocks.get(i).getExpression(), true)));
        }
    }
    IfStatement lastBlock = duplicateIfBlocks.get(duplicateIfBlocks.size() - 1);
    Statement remainingStatement = isThenStatement.get(isThenStatement.size() - 1) ? lastBlock.getElseStatement() : lastBlock.getThenStatement();
    InfixExpression newCondition = ast.newInfixExpression(InfixExpression.Operator.CONDITIONAL_OR, newConditions);
    ASTNodes.replaceButKeepComment(rewrite, duplicateIfBlocks.get(0).getExpression(), newCondition, group);
    if (remainingStatement != null) {
        ASTNodes.replaceButKeepComment(rewrite, duplicateIfBlocks.get(0).getElseStatement(), ASTNodes.createMoveTarget(rewrite, remainingStatement), group);
    } else if (duplicateIfBlocks.get(0).getElseStatement() != null) {
        rewrite.remove(duplicateIfBlocks.get(0).getElseStatement(), group);
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 95 with ASTNodeFactory

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

the class ObsoleteCommonCodeInIfElseStatementCleanUp method removeIdenticalTrailingCode.

private void removeIdenticalTrailingCode(final IfStatement visited, final List<ASTNode> allCases, final List<List<Statement>> allCasesStatements, final List<Statement>[] caseStmtsToRemove, final List<Integer> casesToRefactor) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCommonCodeInIfElseStatementCleanUp_description);
    // Remove the nodes common to all cases
    boolean[] areCasesRemovable = new boolean[allCasesStatements.size()];
    Arrays.fill(areCasesRemovable, false);
    removeStmtsFromCases(allCases, allCasesStatements, caseStmtsToRemove, areCasesRemovable, casesToRefactor);
    List<Statement> oneCaseToRemove = caseStmtsToRemove[casesToRefactor.get(0)];
    if (allRemovable(areCasesRemovable, 0)) {
        if (ASTNodes.canHaveSiblings(visited)) {
            insertIdenticalCode(visited, oneCaseToRemove);
            ASTNodes.removeButKeepComment(rewrite, visited, group);
        } else {
            List<Statement> orderedStatements = new ArrayList<>(oneCaseToRemove.size());
            for (Statement stmtToRemove : oneCaseToRemove) {
                orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, stmtToRemove));
            }
            Block newBlock = ast.newBlock();
            newBlock.statements().addAll(orderedStatements);
            ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
        }
    } else {
        // Remove empty cases
        for (int i : casesToRefactor) {
            ASTNode parent = allCases.get(i);
            if (areCasesRemovable[i]) {
                if (i == areCasesRemovable.length - 2 && !areCasesRemovable[i + 1]) {
                    // Then clause is empty and there is only one else clause
                    // => revert if statement
                    IfStatement newIfStatement = ast.newIfStatement();
                    newIfStatement.setExpression(ast.negate(((IfStatement) parent).getExpression(), true));
                    newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, ((IfStatement) parent).getElseStatement()));
                    ASTNodes.replaceButKeepComment(rewrite, parent, newIfStatement, group);
                    break;
                }
                if (allRemovable(areCasesRemovable, i)) {
                    rewrite.remove(parent, group);
                    break;
                }
                ASTNodes.replaceButKeepComment(rewrite, ((IfStatement) parent).getThenStatement(), ast.newBlock(), group);
            }
        }
        if (ASTNodes.canHaveSiblings(visited)) {
            insertIdenticalCode(visited, oneCaseToRemove);
        } else {
            List<Statement> orderedStatements = new ArrayList<>(oneCaseToRemove.size() + 1);
            for (Statement stmtToRemove : oneCaseToRemove) {
                orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, stmtToRemove));
            }
            orderedStatements.add(0, ASTNodes.createMoveTarget(rewrite, visited));
            Block newBlock = ast.newBlock();
            newBlock.statements().addAll(orderedStatements);
            ASTNodes.replaceButKeepComment(rewrite, visited, newBlock, group);
        }
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

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