Search in sources :

Example 6 with SwitchStatement

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

the class ASTBuilder method switch0.

/**
 * Builds a new {@link SwitchStatement} instance.
 *
 * @param expr
 *            the switch expression
 * @return a new switch statement
 */
public SwitchStatement switch0(Expression expr) {
    final SwitchStatement ss = ast.newSwitchStatement();
    ss.setExpression(expr);
    return ss;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement)

Example 7 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method getParentLoopBody.

private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 8 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project eclipse.jdt.ls by eclipse.

the class FlowAnalyzer method createSwitchData.

// ---- Helper to process switch statement ----------------------------------------
protected SwitchData createSwitchData(SwitchStatement node) {
    SwitchData result = new SwitchData();
    List<Statement> statements = node.statements();
    if (statements.isEmpty()) {
        return result;
    }
    int start = -1, end = -1;
    GenericSequentialFlowInfo info = null;
    for (Iterator<Statement> iter = statements.iterator(); iter.hasNext(); ) {
        Statement statement = iter.next();
        if (statement instanceof SwitchCase) {
            SwitchCase switchCase = (SwitchCase) statement;
            if (switchCase.isDefault()) {
                result.setHasDefaultCase();
            }
            if (info == null) {
                info = createSequential();
                start = statement.getStartPosition();
            } else {
                if (info.isReturn() || info.isPartialReturn() || info.branches()) {
                    result.add(new Region(start, end - start + 1), info);
                    info = createSequential();
                    start = statement.getStartPosition();
                }
            }
        } else {
            info.merge(getFlowInfo(statement), fFlowContext);
        }
        end = statement.getStartPosition() + statement.getLength() - 1;
    }
    result.add(new Region(start, end - start + 1), info);
    return result;
}
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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) 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) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region)

Example 9 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.

the class SimpleEntityImplBuilder method addField.

public void addField(String fType, String fName, String name, boolean isId, boolean isOptional, boolean isReference, boolean isShared, boolean isDerived) {
    // if (StringUtils.isAmbiguous(fType))
    if (!StringUtils.isPrimitiveOrString(fType))
        addImportDeclaration(generator.modelPackage() + "." + fType);
    if (isId) {
        hashCodeMethodAddId(fType, fName);
        equalsMethodAddId(fType, fName);
        toStringMethodAddField(fType, fName);
    }
    // add field, getter and setter methods
    addBodyDeclaration(newFieldDeclaration(fType, fName));
    addBodyDeclaration(newGetterMethodWithNotification(((LanguageGenerator) generator).specificFeatureDescriptorEnumName(), fType, fName, name));
    addBodyDeclaration(newSetterMethodWithNotification(((LanguageGenerator) generator).specificFeatureDescriptorEnumName(), fType, fName, name, isReference));
    if (StringUtils.isPrimitiveOrString(fType))
        return;
    // add get(int index) case
    if (getByIndexMethod().getBody().statements().size() == 0) {
        SwitchStatement switchStm = newSwitchStatement(ast.newSimpleName("index"));
        getByIndexMethod().getBody().statements().add(switchStm);
        getByIndexSwitchStatements = switchStm.statements();
    }
    getByIndexSwitchStatements.add(newCaseStatement(newLiteral(featureIndex)));
    getByIndexSwitchStatements.add(newReturnStatement(newMethodInvocation(newMethodInvocation(StringUtils.getterName(StringUtils.isJavaKeyword(name) ? name : fName)), "wGetAdaptee", newLiteral(false))));
    // add set(int index, IEntity value) case
    if (setByIndexMethod().getBody().statements().size() == 0) {
        SwitchStatement switchStm = newSwitchStatement(ast.newSimpleName("index"));
        setByIndexMethod().getBody().statements().add(switchStm);
        setByIndexSwitchStatements = switchStm.statements();
    }
    setByIndexSwitchStatements.add(newCaseStatement(newLiteral(featureIndex)));
    MethodInvocation callExp = newMethodInvocation(StringUtils.setterName(StringUtils.isJavaKeyword(name) ? name : fName), newMethodInvocation(ast.newSimpleName("value"), "wGetAdapter", newFieldAccess(((LanguageGenerator) generator).specificEntityDescriptorEnumName(), fType)));
    setByIndexSwitchStatements.add(newExpressionStatement(callExp));
    setByIndexSwitchStatements.add(ast.newBreakStatement());
    featureIndex++;
    if (isReference)
        references++;
    else
        children++;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LanguageGenerator(org.whole.gen.lang.LanguageGenerator) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 10 with SwitchStatement

use of org.eclipse.jdt.core.dom.SwitchStatement in project whole by wholeplatform.

the class GenericBuilderAdapterBuilder method getFeatureMethodSwitch.

protected List getFeatureMethodSwitch(String type) {
    List cases = featureSwitchMap.get(type);
    if (cases == null) {
        MethodDeclaration method = newMethodDeclaration("void", type, newSingleVariableDeclaration(FeatureDescriptor.class.getName(), "featureDesc"));
        SwitchStatement switchStm = newSwitchStatement(newMethodInvocation("featureDesc", "getOrdinal"));
        method.getBody().statements().add(switchStm);
        addBodyDeclaration(method);
        featureSwitchMap.put(type, cases = switchStm.statements());
    }
    return cases;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) List(java.util.List)

Aggregations

SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)35 Statement (org.eclipse.jdt.core.dom.Statement)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 DoStatement (org.eclipse.jdt.core.dom.DoStatement)12 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 IfStatement (org.eclipse.jdt.core.dom.IfStatement)11 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)10 Expression (org.eclipse.jdt.core.dom.Expression)9 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Block (org.eclipse.jdt.core.dom.Block)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)5