Search in sources :

Example 41 with ASTNodeFactory

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

the class ObsoleteFillRatherThanLoopCleanUp method replaceWithArraysFill.

private void replaceWithArraysFill(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final Assignment assignment, final ArrayAccess arrayAccess) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteFillRatherThanLoopCleanUp_description);
    String classname = addImport(Arrays.class, classesToUseWithImport, importsToAdd);
    MethodInvocation fillMethod = ast.newMethodInvocation();
    fillMethod.setExpression(ASTNodeFactory.newName(ast, classname));
    // $NON-NLS-1$
    fillMethod.setName(ast.newSimpleName("fill"));
    fillMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arrayAccess.getArray())));
    fillMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(assignment.getRightHandSide())));
    ASTNodes.replaceButKeepComment(rewrite, node, ast.newExpressionStatement(fillMethod), 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 42 with ASTNodeFactory

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

the class ObsoleteGenericListRatherThanRawListCleanUp method substituteType.

@Override
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    if (origType.isParameterizedType()) {
        return null;
    }
    TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
    ParameterizedType parameterizedType = ast.getAST().newParameterizedType(ast.createCopyTarget(origType));
    parameterizedType.typeArguments().clear();
    parameterizedType.typeArguments().add(ast.toType(elementType, typeNameDecider));
    return parameterizedType;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 43 with ASTNodeFactory

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

the class AssertJCleanUp method invokeMethod.

@Override
protected MethodInvocation invokeMethod(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final MethodInvocation originalMethod, final String methodName, final Expression copyOfActual, final Expression copyOfExpected, final Expression delta, final Expression failureMessage) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    String qualifiedClassName = originalMethod.resolveMethodBinding().getDeclaringClass().getQualifiedName();
    Expression qualifiedClass;
    if (!FAIL_METHOD.equals(methodName) && FAIL_CLASS.equals(qualifiedClassName)) {
        qualifiedClassName = ASSERTIONS_CLASS;
        qualifiedClass = null;
    } else if (originalMethod.getExpression() != null) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        qualifiedClass = ASTNodes.createMoveTarget(rewrite, originalMethod.getExpression());
    } else {
        qualifiedClass = null;
    }
    if (// $NON-NLS-1$
    originalMethod.getExpression() == null && !staticImports.contains(qualifiedClassName + "." + methodName) && !staticImports.contains(qualifiedClassName + ".*")) {
        // $NON-NLS-1$
        qualifiedClass = ASTNodeFactory.newName(ast, qualifiedClassName);
    }
    if (FAIL_METHOD.equals(methodName)) {
        return invokeFail(failureMessage, qualifiedClass);
    }
    return invokeQualifiedMethod(classesToUseWithImport, importsToAdd, qualifiedClass, methodName, copyOfActual, copyOfExpected, delta, failureMessage);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite)

Example 44 with ASTNodeFactory

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

the class AssertJCleanUp method invokeFail.

private MethodInvocation invokeFail(final Expression failureMessage, final Expression qualifiedClass) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    if (failureMessage != null) {
        MethodInvocation failureMethod = (MethodInvocation) failureMessage;
        List<Expression> copyOfMessages = new ArrayList<>(failureMethod.arguments().size());
        for (Object message : failureMethod.arguments()) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            copyOfMessages.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((Expression) message)));
        }
        MethodInvocation newMethodInvocation = ast.newMethodInvocation();
        newMethodInvocation.setExpression(qualifiedClass);
        newMethodInvocation.setName(ast.newSimpleName(FAIL_METHOD));
        newMethodInvocation.arguments().addAll(copyOfMessages);
        return newMethodInvocation;
    }
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(qualifiedClass);
    methodInvocation.setName(ast.newSimpleName(FAIL_METHOD));
    methodInvocation.arguments().add(ast.newNullLiteral());
    return methodInvocation;
}
Also used : 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)

Example 45 with ASTNodeFactory

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

the class BigNumberCleanUp method replaceWithQualifiedName.

private void replaceWithQualifiedName(final ASTNode visited, final ITypeBinding typeBinding, final String field) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.BigNumberCleanUp_description);
    ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.newName(ast, typeBinding.getName(), field), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) 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