Search in sources :

Example 91 with ASTRewrite

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

the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method visit.

@Override
public boolean visit(final MethodInvocation visited) {
    Expression expression = visited.getExpression();
    IMethodBinding methodBinding = visited.resolveMethodBinding();
    if (methodBinding != null && expression instanceof Name && hasKind((Name) expression, IBinding.TYPE) && visited.typeArguments().isEmpty()) {
        ITypeBinding declaringClass = methodBinding.getDeclaringClass();
        QName qname = QName.valueOf(declaringClass.getErasure().getQualifiedName(), methodBinding.getName());
        if (methods.canReplaceFqnWithSimpleName(visited, qname, FqnType.METHOD)) {
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSimpleNameRatherThanQualifiedNameCleanUp_description);
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            rewrite.remove(expression, group);
            return false;
        }
    }
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 92 with ASTRewrite

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

the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method maybeReplaceFqnWithSimpleName.

private boolean maybeReplaceFqnWithSimpleName(final QualifiedName visited, final Set<SimpleName> localIdentifiers) {
    ASTNode ancestor = ASTNodes.getFirstAncestorOrNull(visited, PackageDeclaration.class, ImportDeclaration.class);
    QName qname = getFullyQualifiedNameOrNull(visited);
    if (ancestor != null || qname == null) {
        return true;
    }
    if (types.canReplaceFqnWithSimpleName(visited, qname, FqnType.TYPE) || fields.canReplaceFqnWithSimpleName(visited, qname, FqnType.FIELD) && !containsLocalName(localIdentifiers, qname)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSimpleNameRatherThanQualifiedNameCleanUp_description);
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getName()), group);
        return false;
    }
    return true;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 93 with ASTRewrite

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

the class ObsoleteSwitchCleanUp method addCaseWithStatements.

private void addCaseWithStatements(final SwitchStatement switchStatement, final List<Expression> caseValuesOrNullForDefault, final List<Statement> innerStatements) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    List<Statement> switchStatements = switchStatement.statements();
    // Add the case statement(s)
    if (caseValuesOrNullForDefault != null) {
        for (Expression caseValue : caseValuesOrNullForDefault) {
            switchStatements.add(ast.newSwitchCase(ASTNodes.createMoveTarget(rewrite, caseValue)));
        }
    } else {
        switchStatements.add(ast.default0());
    }
    // Add the statement(s) for this case(s)
    boolean isBreakNeeded = true;
    if (!innerStatements.isEmpty()) {
        for (Statement statement : innerStatements) {
            switchStatements.add(ASTNodes.createMoveTarget(rewrite, statement));
        }
        isBreakNeeded = !ASTNodes.fallsThrough(innerStatements.get(innerStatements.size() - 1));
    }
    // When required: end with a break
    if (isBreakNeeded) {
        switchStatements.add(ast.newBreakStatement());
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 94 with ASTRewrite

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

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method refactorHash.

private void refactorHash(final MethodDeclaration node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final CollectedData data) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp_description);
    List<Statement> statements = node.getBody().statements();
    String classname = addImport(Objects.class, classesToUseWithImport, importsToAdd);
    Name objectsClassName = ASTNodeFactory.newName(ast, classname);
    MethodInvocation hashMethod = ast.newMethodInvocation();
    hashMethod.setExpression(objectsClassName);
    // $NON-NLS-1$
    hashMethod.setName(ast.newSimpleName("hash"));
    hashMethod.arguments().addAll(ASTNodes.createMoveTarget(rewrite, data.getFields()));
    ReturnStatement newReturnStatement = ast.newReturnStatement(hashMethod);
    ASTNodes.replaceButKeepComment(rewrite, statements.get(0), newReturnStatement, group);
    for (int i = 1; i < statements.size(); i++) {
        rewrite.remove(statements.get(i), group);
    }
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 95 with ASTRewrite

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

the class ObsoleteLambdaCleanUp method removeParamParentheses.

private void removeParamParentheses(final LambdaExpression node) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    LambdaExpression copyOfLambdaExpression = ast.newLambdaExpression();
    ASTNode copyOfParameter = ASTNodes.createMoveTarget(rewrite, (ASTNode) node.parameters().get(0));
    copyOfLambdaExpression.parameters().add(copyOfParameter);
    copyOfLambdaExpression.setBody(ASTNodes.createMoveTarget(rewrite, node.getBody()));
    copyOfLambdaExpression.setParentheses(false);
    ASTNodes.replaceButKeepComment(rewrite, node, copyOfLambdaExpression, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

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