Search in sources :

Example 56 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class FormattedNumberRatherThanPackedNumberCleanUp method refactorNumber.

private void refactorNumber(final NumberLiteral visited, final String token) {
    // $NON-NLS-1$ //$NON-NLS-2$
    StringBuilder integers = new StringBuilder(token.replaceFirst("^(\\d{4,})[lLdDfF]?$", "$1"));
    // $NON-NLS-1$ //$NON-NLS-2$
    String suffix = token.replaceFirst("^\\d{4,}([lLdDfF]?)$", "$1");
    int position = integers.length() - 3;
    while (position > 0) {
        integers.insert(position, '_');
        position = position - 3;
    }
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    NumberLiteral replacement = ast.newNumberLiteral(integers + suffix);
    TextEditGroup group = new TextEditGroup(MultiFixMessages.FormattedNumberRatherThanPackedNumberCleanUp_description);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    rewrite.replace(visited, replacement, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 57 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class InlineCodeRatherThanPeremptoryConditionCleanUp method remove.

private void remove(final Statement visited) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.InlineCodeRatherThanPeremptoryConditionCleanUp_description);
    if (ASTNodes.canHaveSiblings(visited) || visited.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
        rewrite.remove(visited, group);
    } else {
        ASTNodeFactory ast = cuRewrite.getASTBuilder();
        ASTNodes.replaceButKeepComment(rewrite, visited, ast.newBlock(), 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 58 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class LogParametersRatherThanLogMessageCleanUp method replaceConcatenation.

private void replaceConcatenation(final MethodInvocation visited, final String methodName, final StringBuilder messageBuilder, final List<Expression> params) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.LogParametersRatherThanLogMessageCleanUp_description);
    params.add(0, ast.newStringLiteral(messageBuilder.toString()));
    MethodInvocation newMethodInvocation = ast.newMethodInvocation();
    newMethodInvocation.setExpression(ASTNodes.createMoveTarget(rewrite, visited.getExpression()));
    newMethodInvocation.setName(ast.newSimpleName(methodName));
    newMethodInvocation.arguments().addAll(params);
    rewrite.replace(visited, newMethodInvocation, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 59 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class NewClassImportCleanUp method visit.

@Override
public boolean visit(final CompilationUnit node) {
    if (!super.visit(node)) {
        return false;
    }
    Set<String> classesToUse = getClassesToImport();
    if (classesToUse.isEmpty()) {
        return true;
    }
    Map<String, String> importsByClassname = new HashMap<>();
    Map<String, String> importsByPackage = new HashMap<>();
    for (String klass : classesToUse) {
        importsByClassname.put(getSimpleName(klass), klass);
        importsByPackage.put(getPackageName(klass), klass);
    }
    Set<String> alreadyImportedClasses = new HashSet<>();
    Set<String> classesToImport = new HashSet<>(classesToUse);
    for (Object anObject : node.imports()) {
        ImportDeclaration anImport = (ImportDeclaration) anObject;
        if (!anImport.isStatic()) {
            if (anImport.isOnDemand()) {
                String fullName = importsByPackage.get(anImport.getName().getFullyQualifiedName());
                if (fullName != null) {
                    alreadyImportedClasses.add(fullName);
                }
            } else if (classesToUse.contains(anImport.getName().getFullyQualifiedName())) {
                alreadyImportedClasses.add(anImport.getName().getFullyQualifiedName());
                classesToImport.remove(anImport.getName().getFullyQualifiedName());
            } else if (importsByClassname.containsKey(getSimpleName(anImport.getName().getFullyQualifiedName()))) {
                classesToImport.remove(importsByClassname.get(getSimpleName(anImport.getName().getFullyQualifiedName())));
            }
        }
    }
    filterLocallyUsedNames(node, importsByClassname, classesToImport);
    if (alreadyImportedClasses.size() < classesToUse.size() && !classesToImport.isEmpty()) {
        CleanUpWithNewClassImport refactoringClass = getRefactoringClassInstance();
        refactoringClass.getClassesToUseWithImport().addAll(alreadyImportedClasses);
        refactoringClass.getClassesToUseWithImport().addAll(classesToImport);
        node.accept(refactoringClass);
        if (!refactoringClass.getImportsToAdd().isEmpty()) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            for (String importToAdd : refactoringClass.getImportsToAdd()) {
                rewrite.getImportRewrite().addImport(importToAdd);
            }
            return false;
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) HashSet(java.util.HashSet)

Example 60 with ASTRewrite

use of org.autorefactor.jdt.core.dom.ASTRewrite in project AutoRefactor by JnRouvignac.

the class ObsoleteIfElseIfCleanUp method removeBlock.

private void removeBlock(final IfStatement node, final IfStatement ifStatement) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfElseIfCleanUp_description);
    rewrite.replace(node.getElseStatement(), ASTNodes.createMoveTarget(rewrite, ifStatement), group);
}
Also used : ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)195 TextEditGroup (org.eclipse.text.edits.TextEditGroup)167 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)133 Expression (org.eclipse.jdt.core.dom.Expression)56 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)48 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 Statement (org.eclipse.jdt.core.dom.Statement)24 ArrayList (java.util.ArrayList)22 IfStatement (org.eclipse.jdt.core.dom.IfStatement)20 Block (org.eclipse.jdt.core.dom.Block)15 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 Modifier (org.eclipse.jdt.core.dom.Modifier)11 Type (org.eclipse.jdt.core.dom.Type)11 List (java.util.List)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 CastExpression (org.eclipse.jdt.core.dom.CastExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7