Search in sources :

Example 76 with Statement

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

the class UpdateSetRatherThanTestingFirstRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Statement elseStmt = node.getElseStatement();
    final Statement thenStmt = node.getThenStatement();
    final PrefixExpression pe = as(node.getExpression(), PrefixExpression.class);
    if (hasOperator(pe, NOT)) {
        return maybeReplaceSetContains(node, pe.getOperand(), thenStmt, elseStmt, false);
    } else {
        return maybeReplaceSetContains(node, node.getExpression(), elseStmt, thenStmt, true);
    }
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression)

Example 77 with Statement

use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final SwitchStatement node) {
    this.appendLineWrapToBuffer();
    this.appendToBuffer("switch (");
    node.getExpression().accept(this);
    this.appendToBuffer(") ");
    this.appendToBuffer("{");
    this.increaseIndent();
    final Function2<Map<SwitchCase, ArrayList<Statement>>, Statement, Map<SwitchCase, ArrayList<Statement>>> _function = (Map<SwitchCase, ArrayList<Statement>> map, Statement currStatement) -> {
        if ((currStatement instanceof SwitchCase)) {
            map.put(((SwitchCase) currStatement), CollectionLiterals.<Statement>newArrayList());
        } else {
            map.get(IterableExtensions.<SwitchCase>last(map.keySet())).add(currStatement);
        }
        return map;
    };
    final Map<SwitchCase, ArrayList<Statement>> foldedCases = IterableExtensions.<Statement, Map<SwitchCase, ArrayList<Statement>>>fold(node.statements(), CollectionLiterals.<SwitchCase, ArrayList<Statement>>newLinkedHashMap(), _function);
    final BiConsumer<SwitchCase, ArrayList<Statement>> _function_1 = (SwitchCase switchCase, ArrayList<Statement> statements) -> {
        switchCase.accept(this);
        final boolean isLastCase = switchCase.equals(IterableExtensions.<SwitchCase>last(foldedCases.keySet()));
        if ((statements.isEmpty() && (!isLastCase))) {
            this.appendToBuffer(",");
        } else {
            this.appendToBuffer(":");
            final boolean probablyReturns = ((IterableExtensions.<Statement>last(statements) instanceof ReturnStatement) || ((IterableExtensions.<Statement>last(statements) instanceof Block) && (IterableExtensions.<Object>last(((Block) IterableExtensions.<Statement>last(statements)).statements()) instanceof ReturnStatement)));
            if (((!isLastCase) && (!probablyReturns))) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("/* FIXME unsupported fall-through */");
                this.appendToBuffer(_builder.toString());
                this.addProblem(node, "Unsupported fall-through case in switch expression");
            }
        }
        final boolean surround = ((isLastCase && statements.isEmpty()) || ((!statements.isEmpty()) && (!(statements.get(0) instanceof Block))));
        if (surround) {
            this.appendToBuffer("{");
            this.increaseIndent();
            this.appendLineWrapToBuffer();
        }
        this.visitAll(statements);
        if (surround) {
            this.decreaseIndent();
            this.appendLineWrapToBuffer();
            this.appendToBuffer("}");
        }
    };
    foldedCases.forEach(_function_1);
    this.decreaseIndent();
    this.appendLineWrapToBuffer();
    this.appendToBuffer("}");
    return false;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ArrayList(java.util.ArrayList) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Block(org.eclipse.jdt.core.dom.Block) Map(java.util.Map)

Example 78 with Statement

use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.

the class JavaASTFlattener method visit.

@Override
public boolean visit(final IfStatement node) {
    this.appendToBuffer("if (");
    node.getExpression().accept(this);
    this.appendToBuffer(") ");
    node.getThenStatement().accept(this);
    Statement _elseStatement = node.getElseStatement();
    boolean _tripleNotEquals = (_elseStatement != null);
    if (_tripleNotEquals) {
        this.appendToBuffer(" else ");
        node.getElseStatement().accept(this);
    }
    return false;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 79 with Statement

use of org.eclipse.jdt.core.dom.Statement in project xtext-xtend by eclipse.

the class JavaCodeAnalyzer method determinateJavaType.

public JavaCodeAnalyzer.JavaParseResult<? extends ASTNode> determinateJavaType(final String javaCode) {
    ASTParser parser = this.parserFactory.createDefaultJavaParser(this.parserFactory.minParserApiLevel);
    parser.setSource(javaCode.toCharArray());
    parser.setStatementsRecovery(true);
    ASTNode _createAST = parser.createAST(null);
    CompilationUnit unit = ((CompilationUnit) _createAST);
    int _size = unit.types().size();
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
        List<CompilationUnit> _singletonList = Collections.<CompilationUnit>singletonList(unit);
        return new JavaCodeAnalyzer.JavaParseResult<CompilationUnit>(javaCode, ASTParser.K_COMPILATION_UNIT, _singletonList);
    }
    parser.setSource(javaCode.toCharArray());
    parser.setStatementsRecovery(false);
    parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
    ASTNode root = parser.createAST(null);
    if ((root instanceof TypeDeclaration)) {
        List<BodyDeclaration> bodyDeclarations = ((TypeDeclaration) root).bodyDeclarations();
        int _size_1 = bodyDeclarations.size();
        boolean _greaterThan_1 = (_size_1 > 0);
        if (_greaterThan_1) {
            return new JavaCodeAnalyzer.JavaParseResult<BodyDeclaration>(javaCode, ASTParser.K_CLASS_BODY_DECLARATIONS, bodyDeclarations);
        }
    }
    parser.setSource(javaCode.toCharArray());
    parser.setStatementsRecovery(false);
    parser.setKind(ASTParser.K_STATEMENTS);
    root = parser.createAST(null);
    if ((root instanceof Block)) {
        List<Statement> statements = ((Block) root).statements();
        int _size_2 = statements.size();
        boolean _greaterThan_2 = (_size_2 > 0);
        if (_greaterThan_2) {
            return new JavaCodeAnalyzer.JavaParseResult<Statement>(javaCode, ASTParser.K_STATEMENTS, statements);
        }
    }
    parser.setSource(javaCode.toCharArray());
    parser.setStatementsRecovery(false);
    parser.setKind(ASTParser.K_EXPRESSION);
    root = parser.createAST(null);
    if ((root instanceof Expression)) {
        List<Expression> _singletonList_1 = Collections.<Expression>singletonList(((Expression) root));
        return new JavaCodeAnalyzer.JavaParseResult<Expression>(javaCode, ASTParser.K_EXPRESSION, _singletonList_1);
    }
    return null;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Statement(org.eclipse.jdt.core.dom.Statement) Expression(org.eclipse.jdt.core.dom.Expression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 80 with Statement

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

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final ForLoopContent loopContent = iterateOverContainer(node);
    final List<Statement> stmts = asList(node.getBody());
    if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
        final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
        final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
        // As we replace only one, there should be no more than one occurrence
        if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
            final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
            switch(loopContent.getContainerType()) {
                case COLLECTION:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
                    } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
                    }
                    break;
                case ARRAY:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
                        final Expression addArg0 = arg0(mi);
                        final ArrayAccess aa = as(addArg0, ArrayAccess.class);
                        if (isSameVariable(loopContent, aa)) {
                            replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
                            return DO_NOT_VISIT_SUBTREE;
                        }
                    }
                    break;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ForLoopContent(org.autorefactor.refactoring.ForLoopHelper.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)95 ForStatement (org.eclipse.jdt.core.dom.ForStatement)59 IfStatement (org.eclipse.jdt.core.dom.IfStatement)58 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)56 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)51 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)49 DoStatement (org.eclipse.jdt.core.dom.DoStatement)46 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)46 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)45 Block (org.eclipse.jdt.core.dom.Block)42 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)39 Expression (org.eclipse.jdt.core.dom.Expression)38 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)30 AST (org.eclipse.jdt.core.dom.AST)26 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)26 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 TryStatement (org.eclipse.jdt.core.dom.TryStatement)23 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)22