Search in sources :

Example 46 with ASTNodeFactory

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

the class BigNumberCleanUp method getClassInstanceCreatorNode.

private ASTNode getClassInstanceCreatorNode(final Expression expression, final String numberLiteral) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    String fullyQualifiedName;
    if (expression instanceof Name) {
        fullyQualifiedName = ((Name) expression).getFullyQualifiedName();
    } else if (expression instanceof FieldAccess) {
        fullyQualifiedName = ((FieldAccess) expression).getName().getFullyQualifiedName();
    } else {
        throw new IllegalArgumentException();
    }
    return ast.newClassInstanceCreation(fullyQualifiedName, ast.newStringLiteral(numberLiteral));
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) Name(org.eclipse.jdt.core.dom.Name)

Example 47 with ASTNodeFactory

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

the class BooleanConstantRatherThanValueOfCleanUp method replaceMethod.

private void replaceMethod(final MethodInvocation visited, final boolean literal) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.BooleanConstantRatherThanValueOfCleanUp_description);
    FieldAccess fieldAccess = ast.getAST().newFieldAccess();
    Name expression = ASTNodes.as(visited.getExpression(), Name.class);
    if (expression != null) {
        fieldAccess.setExpression(ASTNodes.createMoveTarget(rewrite, expression));
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    fieldAccess.setName(ast.newSimpleName(literal ? "TRUE" : "FALSE"));
    rewrite.replace(visited, fieldAccess, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup) Name(org.eclipse.jdt.core.dom.Name)

Example 48 with ASTNodeFactory

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

the class CollectionsAddAllRatherThanAsListCleanUp method refactorMethod.

private void refactorMethod(final MethodInvocation visited, final MethodInvocation asListMethod, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.CollectionsAddAllRatherThanAsListCleanUp_description);
    String collectionsName = addImport(Collections.class, classesToUseWithImport, importsToAdd);
    List<Expression> copyOfArguments = new ArrayList<>(asListMethod.arguments().size() + 1);
    copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())));
    for (Object argument : asListMethod.arguments()) {
        copyOfArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) argument)));
    }
    MethodInvocation newMethodInvocation = ast.newMethodInvocation();
    newMethodInvocation.setExpression(ASTNodeFactory.newName(ast, collectionsName));
    newMethodInvocation.setName(ast.newSimpleName(ADD_ALL_METHOD));
    newMethodInvocation.arguments().addAll(copyOfArguments);
    MethodInvocation newCollectionsAddAllMethod = newMethodInvocation;
    rewrite.replace(visited, newCollectionsAddAllMethod, group);
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) 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 49 with ASTNodeFactory

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

the class ObsoleteAddAllRatherThanLoopCleanUp method replaceWithCollectionsAddAll.

private void replaceWithCollectionsAddAll(final Statement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Expression iterable, final MethodInvocation addMethod) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteAddAllRatherThanLoopCleanUp_description);
    String classname = addImport(Collections.class, classesToUseWithImport, importsToAdd);
    MethodInvocation addAllMethod = ast.newMethodInvocation();
    addAllMethod.setExpression(ASTNodeFactory.newName(ast, classname));
    // $NON-NLS-1$
    addAllMethod.setName(ast.newSimpleName("addAll"));
    addAllMethod.arguments().add(addMethod.getExpression() != null ? ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(addMethod.getExpression())) : ast.newThisExpression());
    addAllMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(iterable)));
    ASTNodes.replaceButKeepComment(rewrite, node, ast.newExpressionStatement(addAllMethod), 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 50 with ASTNodeFactory

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

the class ObsoleteCharacterParameterRatherThanStringCleanUp method refactorWithCharacter.

private void refactorWithCharacter(final StringLiteral stringLiteral, final String value) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteCharacterParameterRatherThanStringCleanUp_description);
    CharacterLiteral replacement = ast.newCharacterLiteral();
    replacement.setCharValue(value.charAt(0));
    rewrite.replace(stringLiteral, 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) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

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