Example usage for org.eclipse.jdt.core.dom CatchClause getException

List of usage examples for org.eclipse.jdt.core.dom CatchClause getException

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom CatchClause getException.

Prototype

public SingleVariableDeclaration getException() 

Source Link

Document

Returns the exception variable declaration of this catch clause.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(CatchClause node) {
    this.fBuffer.append("catch (");//$NON-NLS-1$
    node.getException().accept(this);
    this.fBuffer.append(") ");//$NON-NLS-1$
    node.getBody().accept(this);
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(CatchClause node) {
    boa.types.Ast.Statement.Builder b = boa.types.Ast.Statement.newBuilder();
    //      b.setPosition(pos.build());
    List<boa.types.Ast.Statement> list = statements.peek();
    b.setKind(boa.types.Ast.Statement.StatementKind.CATCH);
    SingleVariableDeclaration ex = node.getException();
    Variable.Builder vb = Variable.newBuilder();
    //      vb.setPosition(pos.build());// FIXME
    vb.setName(ex.getName().getFullyQualifiedName());
    for (Object m : ex.modifiers()) {
        if (((IExtendedModifier) m).isAnnotation())
            ((Annotation) m).accept(this);
        else/*from  w  w  w  .  jav a2  s.  c o  m*/
            ((org.eclipse.jdt.core.dom.Modifier) m).accept(this);
        vb.addModifiers(modifiers.pop());
    }
    boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
    String name = typeName(ex.getType());
    for (int i = 0; i < ex.getExtraDimensions(); i++)
        name += "[]";
    tb.setName(getIndex(name));
    tb.setKind(boa.types.Ast.TypeKind.OTHER);
    vb.setVariableType(tb.build());
    if (ex.getInitializer() != null) {
        ex.getInitializer().accept(this);
        vb.setInitializer(expressions.pop());
    }
    b.setVariableDeclaration(vb.build());
    statements.push(new ArrayList<boa.types.Ast.Statement>());
    for (Object s : node.getBody().statements())
        ((org.eclipse.jdt.core.dom.Statement) s).accept(this);
    for (boa.types.Ast.Statement s : statements.pop())
        b.addStatements(s);
    list.add(b.build());
    return false;
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping/*  w w  w  .  j  a v a 2  s .  co  m*/
 */
private void updateGrouping(MappedMessageGrouping grouping) {
    ASTNode node = (ASTNode) grouping.getKey();
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        //it could be an else-if, make sure
        if (ifStatement.getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            if (ifStatement.equals(((IfStatement) ifStatement.getParent()).getElseStatement())) {
                text = "else " + text;
            }
        }
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }

    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.JavaMessage.java

License:Open Source License

/**
 * Returns true if this message was passed within a try block that catches the given
 * type./*  w  w w .ja va 2 s.  com*/
 * @param type
 * @return
 */
public boolean catches(IType type) {
    if (tries == null) {
        return false;
    }
    for (TryStatement statement : tries) {
        for (Object o : statement.catchClauses()) {
            CatchClause catcher = (CatchClause) o;
            ITypeBinding binding = catcher.getException().getType().resolveBinding();
            if (binding != null) {
                IType caughtType = (IType) binding.getJavaElement();
                if (caughtType != null) {
                    try {
                        ITypeHierarchy hierarchy = caughtType.newSupertypeHierarchy(new NullProgressMonitor());
                        if (caughtType.equals(type) || hierarchy.contains(type)) {
                            return true;
                        }
                    } catch (JavaModelException e) {

                    }
                }
            }
        }
    }
    return false;
}

From source file:ca.uvic.chisel.javasketch.ui.internal.presentation.ASTMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping/*from   w ww  .  j a va 2 s .  co  m*/
 */
private void updateGrouping(ASTMessageGrouping grouping, ASTNode node) {
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        //it could be an else-if, make sure
        if (ifStatement.getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            if (ifStatement.equals(((IfStatement) ifStatement.getParent()).getElseStatement())) {
                text = "else " + text;
            }
        }
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }
    if (grouping.node.isLoop()) {
        ASTMessageGroupingTree[] siblings = grouping.node.getSiblings();
        text = text + "[" + grouping.node.getIteration() + " of " + (siblings.length + 1) + "]";
    }

    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:ca.uvic.chisel.javasketch.ui.internal.presentation.CopyOfASTMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping//from   w ww. j  a  v  a2  s.  co  m
 */
private void updateGrouping(ASTMessageGrouping grouping, ASTNode node) {
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }
    if (grouping.node.isLoop()) {
        ASTMessageGroupingTree[] siblings = grouping.node.getSiblings();
        text = text + "[" + grouping.node.getIteration() + " of " + (siblings.length + 1) + "]";
    }
    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(TryStatement node) {
    if (this.mtbStack.isEmpty()) {
        return true;
    }/*from www  . j a  va2 s . c  o  m*/
    String bodyStr = node.getBody() != null ? node.getBody().toString() : "";
    bodyStr = edit_str(bodyStr);
    StringBuilder catchClauses = new StringBuilder();
    for (Object o : node.catchClauses()) {
        if (catchClauses.length() > 0) {
            catchClauses.append(",");
        }
        CatchClause c = (CatchClause) o;
        catchClauses.append(getQualifiedName(c.getException().getType().resolveBinding()));
        catchClauses.append(":");
        if (c.getBody() != null) {
            catchClauses.append(edit_str(c.getBody().toString()));
        }
    }
    String finallyStr = node.getFinally() != null ? node.getFinally().toString() : "";
    finallyStr = edit_str(finallyStr);

    IMethodBinding mtb = (IMethodBinding) this.mtbStack.peek();
    String methodStr = getQualifiedName(mtb);

    this.facts.add(Fact.makeTryCatchFact(bodyStr, catchClauses.toString(), finallyStr, methodStr));

    return true;
}

From source file:chibi.gumtreediff.gen.jdt.cd.CdJdtVisitor.java

License:Open Source License

@Override
public boolean visit(CatchClause node) {
    pushNode(node, ((SimpleType) node.getException().getType()).getName().getFullyQualifiedName());
    // since exception type is used as value, visit children by hand
    node.getBody().accept(this);
    return false;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(CatchClause node) {
    this.buffer.append("catch (");//$NON-NLS-1$
    node.getException().accept(this);
    this.buffer.append(") ");//$NON-NLS-1$
    node.getBody().accept(this);
    return false;
}

From source file:com.drgarbage.ast.ASTGraphUtil.java

License:Apache License

/**
 * Returns nodes description./*from ww w  .jav a2s  .  c o  m*/
 * 
 * @param node the AST-node
 * @return description string
 */
public static String getNodeDescr(ASTNode node) {
    StringBuffer elementDescr = new StringBuffer(node.getClass().getSimpleName());
    elementDescr.append(": ");

    int nodeType = node.getNodeType();
    switch (nodeType) {
    case ASTNode.COMPILATION_UNIT:
        CompilationUnit cu = (CompilationUnit) node;
        elementDescr.append(cu.getJavaElement().getElementName());
        break;

    case ASTNode.PACKAGE_DECLARATION:
        PackageDeclaration pd = (PackageDeclaration) node;
        elementDescr.append(pd.getName());
        break;

    case ASTNode.TYPE_DECLARATION:
        TypeDeclaration td = (TypeDeclaration) node;
        appendModifiers(td.getModifiers(), elementDescr);
        elementDescr.append(" class ");
        elementDescr.append(td.getName());
        break;

    case ASTNode.METHOD_DECLARATION:
        MethodDeclaration md = (MethodDeclaration) node;
        appendModifiers(md.getModifiers(), elementDescr);
        elementDescr.append(md.getReturnType2() == null ? "" : md.getReturnType2().toString());
        elementDescr.append(' ');
        elementDescr.append(md.getName());
        elementDescr.append("()");
        break;

    case ASTNode.BLOCK:
        elementDescr.append("{...}");
        break;

    case ASTNode.IF_STATEMENT:
        IfStatement is = (IfStatement) node;
        elementDescr.append("if( ");
        elementDescr.append(is.getExpression().toString());
        elementDescr.append(")");
        break;

    case ASTNode.FOR_STATEMENT:
        ForStatement fs = (ForStatement) node;
        elementDescr.append("for (...; ");
        elementDescr.append(fs.getExpression().toString());
        elementDescr.append("; ...){...}");
        break;

    case ASTNode.WHILE_STATEMENT:
        WhileStatement ws = (WhileStatement) node;
        elementDescr.append("while ( ");
        elementDescr.append(ws.getExpression().toString());
        elementDescr.append("){...}");
        break;

    case ASTNode.DO_STATEMENT:
        DoStatement ds = (DoStatement) node;
        elementDescr.append("do {...} while (");
        elementDescr.append(ds.getExpression().toString());
        elementDescr.append(")");
        break;

    case ASTNode.LABELED_STATEMENT:
        LabeledStatement ls = (LabeledStatement) node;
        elementDescr.append(ls.getLabel().toString());
        elementDescr.append(":");
        break;

    case ASTNode.CATCH_CLAUSE:
        CatchClause cs = (CatchClause) node;
        elementDescr.append("catch (");
        elementDescr.append(cs.getException().toString());
        elementDescr.append("){...}");
        break;

    case ASTNode.SWITCH_STATEMENT:
        SwitchStatement ss = (SwitchStatement) node;
        elementDescr.append("switch (");
        elementDescr.append(ss.getExpression().toString());
        elementDescr.append("){...}");
        break;

    case ASTNode.SWITCH_CASE:
        SwitchCase sc = (SwitchCase) node;
        elementDescr.append("case ");
        elementDescr.append(sc.getExpression() == null ? "default" : sc.getExpression().toString());
        elementDescr.append(":");
        break;
    case ASTNode.JAVADOC:
    case ASTNode.BLOCK_COMMENT:
    case ASTNode.LINE_COMMENT:
    case ASTNode.TRY_STATEMENT:
        /* nothing to do */
        break;

    default:
        elementDescr.append(node.toString());
    }

    /* cut the string if it is too long */
    String str = elementDescr.toString();
    if (str.length() > 128) {
        str = str.substring(0, 128) + " ... ";
    }
    str = str.replaceAll("\n", "");
    return str;
}