Search in sources :

Example 71 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.

the class ObsoleteLambdaCleanUp method removeReturnAndBrackets.

private void removeReturnAndBrackets(final LambdaExpression node, final List<Statement> statements) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLambdaCleanUp_description);
    ReturnStatement returnStatement = (ReturnStatement) statements.get(0);
    ASTNodes.replaceButKeepComment(rewrite, node.getBody(), ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, returnStatement.getExpression())), group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 72 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.

the class ObsoleteLambdaCleanUp method visit.

@Override
public boolean visit(final LambdaExpression node) {
    if (node.hasParentheses() && node.parameters().size() == 1 && node.parameters().get(0) instanceof VariableDeclarationFragment) {
        // TODO it should also be possible to deal with a SingleVariableDeclaration
        // when the type matches the expected inferred type
        // To do this, we should visit the whole block and check the target type
        removeParamParentheses(node);
        return false;
    }
    if (node.getBody() instanceof Block) {
        List<Statement> statements = ASTNodes.asList((Block) node.getBody());
        if (statements.size() == 1 && statements.get(0) instanceof ReturnStatement) {
            removeReturnAndBrackets(node, statements);
            return false;
        }
    } else if (node.getBody() instanceof ClassInstanceCreation) {
        ClassInstanceCreation ci = (ClassInstanceCreation) node.getBody();
        List<Expression> arguments = ci.arguments();
        if (ci.resolveTypeBinding() != null && ci.getAnonymousClassDeclaration() == null && node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceByCreationReference(node, ci);
            return false;
        }
    } else if (node.getBody() instanceof SuperMethodInvocation) {
        SuperMethodInvocation smi = (SuperMethodInvocation) node.getBody();
        List<Expression> arguments = smi.arguments();
        if (node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceBySuperMethodReference(node, smi);
            return false;
        }
    } else if (node.getBody() instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) node.getBody();
        Expression calledExpression = methodInvocation.getExpression();
        ITypeBinding calledType = ASTNodes.getCalledType(methodInvocation);
        List<Expression> arguments = methodInvocation.arguments();
        if (node.parameters().size() == arguments.size()) {
            if (!areSameIdentifiers(node, arguments)) {
                return true;
            }
            if (isStaticMethod(methodInvocation)) {
                if (!arguments.isEmpty()) {
                    String[] remainingParams = new String[arguments.size() - 1];
                    for (int i = 0; i < arguments.size() - 1; i++) {
                        ITypeBinding argumentBinding = arguments.get(i + 1).resolveTypeBinding();
                        if (argumentBinding == null) {
                            return true;
                        }
                        remainingParams[i] = argumentBinding.getQualifiedName();
                    }
                    for (IMethodBinding methodBinding : calledType.getDeclaredMethods()) {
                        if ((methodBinding.getModifiers() & Modifier.STATIC) == 0 && ASTNodes.usesGivenSignature(methodBinding, calledType.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                            return true;
                        }
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
            if (calledExpression == null || calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral || calledExpression instanceof ThisExpression) {
                replaceByMethodReference(node, methodInvocation);
                return false;
            }
            if (calledExpression instanceof FieldAccess) {
                FieldAccess fieldAccess = (FieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            } else if (calledExpression instanceof SuperFieldAccess) {
                SuperFieldAccess fieldAccess = (SuperFieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            }
        } else if (calledExpression instanceof SimpleName && node.parameters().size() == arguments.size() + 1) {
            SimpleName calledObject = (SimpleName) calledExpression;
            if (isSameIdentifier(node, 0, calledObject)) {
                for (int i = 0; i < arguments.size(); i++) {
                    SimpleName expression = ASTNodes.as(arguments.get(i), SimpleName.class);
                    if (expression == null || !isSameIdentifier(node, i + 1, expression)) {
                        return true;
                    }
                }
                ITypeBinding klass = calledExpression.resolveTypeBinding();
                if (klass == null) {
                    return true;
                }
                String[] remainingParams = new String[arguments.size() + 1];
                remainingParams[0] = klass.getQualifiedName();
                for (int i = 0; i < arguments.size(); i++) {
                    ITypeBinding argumentBinding = arguments.get(i).resolveTypeBinding();
                    if (argumentBinding == null) {
                        return true;
                    }
                    remainingParams[i + 1] = argumentBinding.getQualifiedName();
                }
                for (IMethodBinding methodBinding : klass.getDeclaredMethods()) {
                    if ((methodBinding.getModifiers() & Modifier.STATIC) != 0 && ASTNodes.usesGivenSignature(methodBinding, klass.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                        return true;
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
        }
    }
    return true;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 73 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.

the class ObsoleteRedundantComparatorCleanUp method isClassToRemove.

private boolean isClassToRemove(final ClassInstanceCreation classInstanceCreation, final boolean isForward) {
    AnonymousClassDeclaration anonymousClassDecl = classInstanceCreation.getAnonymousClassDeclaration();
    Type type = classInstanceCreation.getType();
    if (type != null && type.resolveBinding() != null && type.resolveBinding().getTypeArguments() != null && type.resolveBinding().getTypeArguments().length == 1 && ASTNodes.hasType(type.resolveBinding(), Comparator.class.getCanonicalName()) && classInstanceCreation.arguments().isEmpty() && anonymousClassDecl != null) {
        List<BodyDeclaration> bodies = anonymousClassDecl.bodyDeclarations();
        ITypeBinding typeArgument = type.resolveBinding().getTypeArguments()[0];
        if (bodies != null && bodies.size() == 1 && typeArgument != null) {
            BodyDeclaration body = bodies.get(0);
            if (body instanceof MethodDeclaration) {
                MethodDeclaration methodDecl = (MethodDeclaration) body;
                ReturnStatement returnStatement = ASTNodes.as(methodDecl.getBody(), ReturnStatement.class);
                if (returnStatement != null && returnStatement.getExpression() != null && // $NON-NLS-1$
                ASTNodes.usesGivenSignature(// $NON-NLS-1$
                methodDecl, // $NON-NLS-1$
                Comparator.class.getCanonicalName(), // $NON-NLS-1$
                "compare", typeArgument.getQualifiedName(), typeArgument.getQualifiedName())) {
                    VariableDeclaration object1 = (VariableDeclaration) methodDecl.parameters().get(0);
                    VariableDeclaration object2 = (VariableDeclaration) methodDecl.parameters().get(1);
                    return isReturnedExpressionToRemove(object1.getName(), object2.getName(), returnStatement.getExpression(), isForward);
                }
            }
        }
    }
    return false;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 74 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isStmtValid.

private boolean isStmtValid(final CollectedData data) {
    Statement statement = data.getStmtIterator().next();
    ExpressionStatement exprStatement = ASTNodes.as(statement, ExpressionStatement.class);
    if (exprStatement != null) {
        return isAssignmentValid(data, exprStatement);
    }
    ReturnStatement returnStatement = ASTNodes.as(statement, ReturnStatement.class);
    if (returnStatement != null) {
        data.setHasReturnStatement(true);
        Expression expression = returnStatement.getExpression();
        return returnStatement != null && (isGivenVariable(expression, data.getResultId()) || isHashValid(data, expression));
    }
    VariableDeclarationStatement varStatement = ASTNodes.as(statement, VariableDeclarationStatement.class);
    if (varStatement != null && data.getTempVar() == null) {
        VariableDeclarationFragment fragment = ASTNodes.getUniqueFragment(varStatement);
        if (ASTNodes.hasType(varStatement.getType().resolveBinding(), long.class.getSimpleName()) && fragment != null) {
            data.setTempVar(fragment.getName());
            Expression initializer = fragment.getInitializer();
            if (fragment.getExtraDimensions() == 0) {
                if (initializer != null) {
                    SimpleName fieldToFind = isDoubleToLongBitsMethod(data, initializer);
                    data.setTempValueUsed(false);
                    if (fieldToFind != null && data.getStmtIterator().hasNext()) {
                        boolean assignmentValid = isStmtValid(data);
                        if (assignmentValid) {
                            data.getFields().add(ASTNodes.getUnparenthesedExpression(fieldToFind));
                            return true;
                        }
                    }
                } else if (data.getStmtIterator().hasNext()) {
                    return isStmtValid(data);
                }
            }
        }
    }
    return false;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 75 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement in project AutoRefactor by JnRouvignac.

the class ControlWorkflowMatcher method buildActualNodes.

private ControlWorkflowNode buildActualNodes(final List<Statement> actualStatements) {
    if (Utils.isEmpty(actualStatements)) {
        throw new AbortSearchException();
    }
    IfStatement ifStatement = ASTNodes.as(actualStatements.get(0), IfStatement.class);
    ReturnStatement returnStatement = ASTNodes.as(actualStatements.get(0), ReturnStatement.class);
    if (ifStatement != null) {
        ControlWorkflowNode node = new ControlWorkflowNode();
        node.setCondition(ifStatement.getExpression());
        node.setThenNode(buildActualNodes(ifStatement.getThenStatement()));
        if (ifStatement.getElseStatement() != null && actualStatements.size() == 1) {
            node.setElseNode(buildActualNodes(ifStatement.getElseStatement()));
            return node;
        }
        if (ifStatement.getElseStatement() != null || actualStatements.size() == 1 || !ASTNodes.fallsThrough(ifStatement.getThenStatement())) {
            throw new AbortSearchException();
        }
        List<Statement> elseStmts = new ArrayList<>(actualStatements);
        elseStmts.remove(0);
        node.setElseNode(buildActualNodes(elseStmts));
        return node;
    }
    if (returnStatement != null) {
        Expression condition = returnStatement.getExpression();
        return buildActualNodes(condition);
    }
    if (actualStatements.size() == 1) {
        ControlWorkflowNode node = new ControlWorkflowNode();
        node.setFinalStatement(actualStatements.get(0));
        return node;
    }
    throw new AbortSearchException();
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ArrayList(java.util.ArrayList)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)76 Block (org.eclipse.jdt.core.dom.Block)46 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 Expression (org.eclipse.jdt.core.dom.Expression)37 AST (org.eclipse.jdt.core.dom.AST)31 ASTNode (org.eclipse.jdt.core.dom.ASTNode)31 Type (org.eclipse.jdt.core.dom.Type)26 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)25 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)23 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 Statement (org.eclipse.jdt.core.dom.Statement)22 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)16 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)16 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)15 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)14