List of usage examples for org.eclipse.jdt.core.dom TryStatement BODY_PROPERTY
ChildPropertyDescriptor BODY_PROPERTY
To view the source code for org.eclipse.jdt.core.dom TryStatement BODY_PROPERTY.
Click Source Link
From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java
License:Open Source License
private List<Name> calcThrownExceptions(MethodDeclaration method, List<Statement> statements2) { ArrayList<Name> result = new ArrayList<Name>(); result.addAll(method.thrownExceptions()); /*//from www . j a va 2 s .c o m * search ast upwards for try blocks that catch additional exceptions. * include those * * do not include the own Return* exceptions for emulating return * statements in hooks */ ASTNode node = statements2.get(0); while (node != null && node != method) { if (node.getLocationInParent() == TryStatement.BODY_PROPERTY) { TryStatement tryStmt = (TryStatement) node.getParent(); for (CatchClause cc : (List<CatchClause>) tryStmt.catchClauses()) { Type exceptionType = cc.getException().getType(); if (!containsException(result, exceptionType.toString())) if (!exceptionType.toString().startsWith("Return")) result.add(method.getAST().newSimpleName(exceptionType.toString())); } } node = node.getParent(); } return result; }
From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java
License:Open Source License
/** * Move statement to given {@link StatementTarget}. * //from ww w.j a v a 2 s . c o m * @param target * the new position for statement * @param statement * the statement to move * @param includePrefixComment * flag to mark that comment before statement also should be moved */ public void moveStatement(Statement statement, StatementTarget target) throws Exception { // statement or method declaration required Assert.isTrue(target.getBlock() != null || target.getStatement() != null); // check for no-op if (target.getStatement() != null) { Statement targetStatement = target.getStatement(); // adding relative same statement if (targetStatement == statement) { return; } // adding before current next statement Block targetBlock = (Block) targetStatement.getParent(); List<Statement> targetStatements = DomGenerics.statements(targetBlock); if (statement.getParent() == targetStatement.getParent()) { if (target.isBefore()) { // statement before target if (targetStatements.indexOf(statement) == targetStatements.indexOf(targetStatement) - 1) { return; } } else { // statement after target if (targetStatements.indexOf(statement) == targetStatements.indexOf(targetStatement) + 1) { return; } } } } else { Block targetBlock = target.getBlock(); List<Statement> targetStatements = DomGenerics.statements(targetBlock); if (statement.getParent() == targetBlock) { if (target.isBefore()) { if (targetStatements.indexOf(statement) == 0) { return; } } else { if (targetStatements.indexOf(statement) == targetStatements.size() - 1) { return; } } } } // prepare code generation constants AstCodeGeneration generation = getGeneration(); String singleIndent = generation.getIndentation(1); String eol = generation.getEndOfLine(); // prepare source location Block sourceBlock = (Block) statement.getParent(); int sourceBegin = skipWhitespaceAndPureEOLCToLeft(statement.getStartPosition()); int sourceLength = getStatementEndIndex(statement) - sourceBegin; // remove leading EOL { int leftEOLEnd = sourceBegin; int leftEOLBegin = skipSingleEOLToLeft(leftEOLEnd); int leftEOLLength = leftEOLEnd - leftEOLBegin; // replaceSubstring(leftEOLBegin, leftEOLLength, ""); sourceBegin -= leftEOLLength; } // if (target.getStatement() != null) { Statement targetStatement = target.getStatement(); String indent = getWhitespaceToLeft(targetStatement.getStartPosition(), false); // int position; if (target.isBefore()) { // move before any empty lines and EOLC lines position = skipWhitespaceAndPureEOLCToLeft(targetStatement.getStartPosition()); // move source position = moveSource(position, sourceBegin, sourceLength); // add EOL after statement replaceSubstring(position + sourceLength, 0, eol); } else { // move at the end of target position = getStatementEndIndex(targetStatement); // move source position = moveSource(position, sourceBegin, sourceLength); // add EOL before statement replaceSubstring(position, 0, eol); position += eol.length(); } // move node { Block targetBlock = (Block) targetStatement.getParent(); int index = targetBlock.statements().indexOf(targetStatement); if (!target.isBefore()) { index++; } if (sourceBlock == targetBlock && sourceBlock.statements().indexOf(statement) < index) { index--; } sourceBlock.statements().remove(statement); DomGenerics.statements(targetBlock).add(index, statement); } // re-indent reindentSource(position, sourceLength, indent, eol); } else { Block targetBlock = target.getBlock(); // prepare indentation String indent; { ASTNode indentNode = targetBlock; if (targetBlock.getLocationInParent() == MethodDeclaration.BODY_PROPERTY || targetBlock.getLocationInParent() == TryStatement.BODY_PROPERTY) { indentNode = targetBlock.getParent(); } indent = getWhitespaceToLeft(indentNode.getStartPosition(), false); indent += singleIndent; } // int position; if (target.isBefore()) { // prepare position as position after '{' of block { position = targetBlock.getStartPosition(); Assert.isTrue(position != -1); position++; } // move source position = moveSource(position, sourceBegin, sourceLength); // add EOL before statement replaceSubstring(position, 0, eol); position += eol.length(); // move node sourceBlock.statements().remove(statement); DomGenerics.statements(targetBlock).add(0, statement); } else { // prepare position as position of '}' of block position = AstNodeUtils.getSourceEnd(targetBlock) - 1; String endMethodIndent = getWhitespaceToLeft(position, false); position -= endMethodIndent.length(); // move source position = moveSource(position, sourceBegin, sourceLength); // add EOL after statement replaceSubstring(position + sourceLength, 0, eol); // move node sourceBlock.statements().remove(statement); DomGenerics.statements(targetBlock).add(statement); } // re-indent reindentSource(position, sourceLength, indent, eol); } }