List of usage examples for org.eclipse.jdt.core.dom Statement delete
public final void delete()
From source file:org.decojer.cavaj.transformers.TrControlFlowStmts.java
License:Open Source License
@Nullable private TryStatement transformCatch(@Nonnull final Catch catchStruct) { final BB head = catchStruct.getHead(); final TryStatement tryStatement = getAst().newTryStatement(); final List<Statement> statements = tryStatement.getBody().statements(); assert statements != null; transformSequence(catchStruct, head, statements); // now add handlers for (final E catchE : head.getOuts()) { if (!catchE.isCatch()) { continue; }//from w w w .j a va2s. c o m final BB handler = catchE.getEnd(); final T[] catchTypes = (T[]) catchE.getValue(); assert catchTypes != null; if (!catchStruct.hasHandler(catchTypes, handler)) { continue; } // extract exception name from temporary throwable declaration, don't remove in CFG final Statement throwableDeclaration = handler.getStmt(0); assert throwableDeclaration instanceof VariableDeclarationStatement; final List<VariableDeclarationFragment> throwableDeclarationFragments = ((VariableDeclarationStatement) throwableDeclaration) .fragments(); assert throwableDeclarationFragments.size() == 1; final VariableDeclarationFragment throwableDeclarationFragment = throwableDeclarationFragments.get(0); final SimpleName exceptionName = throwableDeclarationFragment.getName(); assert exceptionName != null; if (catchE.isFinally()) { // finally handler tryStatement.setFinally(getAst().newBlock()); final List<Statement> finallyStatements = tryStatement.getFinally().statements(); assert finallyStatements != null; transformSequence(catchStruct, handler, finallyStatements); // remove temporary throwable declaration from finally block, could be nested Statement firstStatement = finallyStatements.get(0); if (firstStatement instanceof TryStatement) { firstStatement = (Statement) ((TryStatement) firstStatement).getBody().statements().get(0); } if (firstStatement instanceof VariableDeclarationStatement) { firstStatement.delete(); } // remove final throws from finally block, should never be nested if (!finallyStatements.isEmpty() && finallyStatements.get(finallyStatements.size() - 1) instanceof ThrowStatement) { finallyStatements.remove(finallyStatements.size() - 1); } continue; } // normal typed catch handler final CatchClause catchClause = getAst().newCatchClause(); final SingleVariableDeclaration singleVariableDeclaration = getAst().newSingleVariableDeclaration(); singleVariableDeclaration.setName((SimpleName) wrap(exceptionName)); if (catchTypes.length == 1) { final T handlerType = catchTypes[0]; assert handlerType != null; singleVariableDeclaration.setType(newType(handlerType, getM())); } else { // Multi-Catch final UnionType unionType = getAst().newUnionType(); for (final T t : catchTypes) { assert t != null; unionType.types().add(newType(t, getM())); } singleVariableDeclaration.setType(unionType); } catchClause.setException(singleVariableDeclaration); tryStatement.catchClauses().add(catchClause); final List<Statement> handlerStatements = catchClause.getBody().statements(); assert handlerStatements != null; transformSequence(catchStruct, handler, handlerStatements); // remove temporary throwable declaration from catch if (handlerStatements.isEmpty()) { log.warn(getM() + ": Missing temporary throwable declaration in handler statements for BB" + handler.getPc() + ":\n" + catchStruct); } else { handlerStatements.remove(0); } } return tryStatement; }