Search in sources :

Example 96 with ASTNodeFactory

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

the class ObsoleteParsingRatherThanValueOfCleanUp method replaceMethodName.

private void replaceMethodName(final MethodInvocation visited, final String methodName) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteParsingRatherThanValueOfCleanUp_description);
    rewrite.set(visited, MethodInvocation.NAME_PROPERTY, ast.newSimpleName(methodName), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 97 with ASTNodeFactory

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

the class ObsoleteDoubleCompareRatherThanEqualityCleanUp method replace.

private void replace(final InfixExpression visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteDoubleCompareRatherThanEqualityCleanUp_description);
    MethodInvocation compareMethod = ast.newMethodInvocation();
    compareMethod.setExpression(ASTNodeFactory.newName(ast, Double.class.getSimpleName()));
    // $NON-NLS-1$
    compareMethod.setName(ast.newSimpleName("compare"));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getLeftOperand())));
    compareMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getRightOperand())));
    InfixExpression newInfixExpression = ast.newInfixExpression();
    newInfixExpression.setLeftOperand(compareMethod);
    newInfixExpression.setOperator(visited.getOperator());
    // $NON-NLS-1$
    newInfixExpression.setRightOperand(ast.newNumberLiteral("0"));
    ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 98 with ASTNodeFactory

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

the class ObsoleteEqualsNullableCleanUp method visit.

@Override
public boolean visit(final InfixExpression visited) {
    if (ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND)) {
        List<Expression> operands = ASTNodes.allOperands(visited);
        if (operands.size() > 2) {
            for (int i = 0; i < operands.size() - 1; i++) {
                Expression nullCheckedExpression = ASTNodes.getNullCheckedExpression(operands.get(i));
                if (nullCheckedExpression != null && isNullCheckRedundant(operands.get(i + 1), nullCheckedExpression)) {
                    operands.remove(i);
                    ASTRewrite rewrite = cuRewrite.getASTRewrite();
                    ASTNodeFactory ast = cuRewrite.getASTBuilder();
                    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteEqualsNullableCleanUp_description);
                    InfixExpression newInfixExpression = ast.newInfixExpression(visited.getOperator(), ASTNodes.createMoveTarget(rewrite, operands));
                    ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
                    return false;
                }
            }
        } else {
            Expression leftOperand = visited.getLeftOperand();
            Expression rightOperand = visited.getRightOperand();
            Expression nullCheckedExpressionLHS = ASTNodes.getNullCheckedExpression(leftOperand);
            if (nullCheckedExpressionLHS != null && isNullCheckRedundant(rightOperand, nullCheckedExpressionLHS)) {
                replaceBy(visited, rightOperand);
                return false;
            }
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 99 with ASTNodeFactory

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

the class BooleanCleanUp method getExpression.

private Expression getExpression(final Expression condition, final String expressionTypeName, final ASTNode context) {
    Name newBooleanName = getBooleanName(context);
    if (boolean.class.getSimpleName().equals(expressionTypeName)) {
        return condition;
    }
    if (Boolean.class.getCanonicalName().equals(expressionTypeName)) {
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        if (getJavaMinorVersion() >= 4) {
            MethodInvocation valueOfMethod = ast.newMethodInvocation();
            valueOfMethod.setExpression(newBooleanName);
            // $NON-NLS-1$
            valueOfMethod.setName(ast.newSimpleName("valueOf"));
            valueOfMethod.arguments().add(condition);
            return valueOfMethod;
        }
        return ast.newClassInstanceCreation(expressionTypeName, condition);
    }
    return null;
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 100 with ASTNodeFactory

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

the class DeclarationOutsideLoopRatherThanInsideCleanUp method moveDeclaration.

private void moveDeclaration(final Statement statement, final VariableDeclarationStatement varToMove) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.DeclarationOutsideLoopRatherThanInsideCleanUp_description);
    VariableDeclarationFragment fragment = (VariableDeclarationFragment) varToMove.fragments().get(0);
    if (fragment.getInitializer() != null) {
        Type copyOfType = ast.createCopyTarget(varToMove.getType());
        SimpleName name = fragment.getName();
        VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(ast.createCopyTarget(name));
        List<Dimension> extraDimensions = fragment.extraDimensions();
        List<Dimension> newExtraDimensions = newFragment.extraDimensions();
        newExtraDimensions.addAll(ASTNodes.createMoveTarget(rewrite, extraDimensions));
        VariableDeclarationStatement newDeclareStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
        List<IExtendedModifier> modifiers = varToMove.modifiers();
        List<IExtendedModifier> newModifiers = newDeclareStatement.modifiers();
        for (IExtendedModifier iExtendedModifier : modifiers) {
            Modifier modifier = (Modifier) iExtendedModifier;
            if (!modifier.isPrivate() && !modifier.isStatic()) {
                newModifiers.add(ASTNodes.createMoveTarget(rewrite, modifier));
            }
        }
        rewrite.insertBefore(newDeclareStatement, statement, group);
        ASTNodes.replaceButKeepComment(rewrite, varToMove, ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(name), Assignment.Operator.ASSIGN, ASTNodes.createMoveTarget(rewrite, fragment.getInitializer()))), group);
    } else {
        rewrite.insertBefore(ASTNodes.createMoveTarget(rewrite, varToMove), statement, group);
        rewrite.remove(varToMove, group);
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Dimension(org.eclipse.jdt.core.dom.Dimension) TextEditGroup(org.eclipse.text.edits.TextEditGroup) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

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