Java Code Examples for org.eclipse.jdt.core.dom.TryStatement#getFinally()

The following examples show how to use org.eclipse.jdt.core.dom.TryStatement#getFinally() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
			iterator.next().accept(this);
		}
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses = node.catchClauses();
		for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock = node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
Example 2
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
protected TryStatement copyTryStatement(ASTRewrite sourceRewriter, AST ast, TryStatement tryStatementParent) {
	TryStatement newTryStatement = ast.newTryStatement();
	ListRewrite resourceRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.RESOURCES_PROPERTY);
	List<VariableDeclarationExpression> resources = tryStatementParent.resources();
	for(VariableDeclarationExpression expression : resources) {
		resourceRewrite.insertLast(expression, null);
	}
	ListRewrite catchClauseRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
	List<CatchClause> catchClauses = tryStatementParent.catchClauses();
	for(CatchClause catchClause : catchClauses) {
		catchClauseRewrite.insertLast(catchClause, null);
	}
	if(tryStatementParent.getFinally() != null) {
		sourceRewriter.set(newTryStatement, TryStatement.FINALLY_PROPERTY, tryStatementParent.getFinally(), null);
	}
	return newTryStatement;
}
 
Example 3
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses= node.catchClauses();
		for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock= node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
Example 4
Source File: StatementAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void endVisit(TryStatement node) {
	ASTNode firstSelectedNode= getFirstSelectedNode();
	if (getSelection().getEndVisitSelectionMode(node) == Selection.AFTER) {
		if (firstSelectedNode == node.getBody() || firstSelectedNode == node.getFinally()) {
			invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
		} else {
			List<CatchClause> catchClauses= node.catchClauses();
			for (Iterator<CatchClause> iterator= catchClauses.iterator(); iterator.hasNext();) {
				CatchClause element= iterator.next();
				if (element == firstSelectedNode || element.getBody() == firstSelectedNode) {
					invalidSelection(RefactoringCoreMessages.StatementAnalyzer_try_statement);
				} else if (element.getException() == firstSelectedNode) {
					invalidSelection(RefactoringCoreMessages.StatementAnalyzer_catch_argument);
				}
			}
		}
	}
	super.endVisit(node);
}
 
Example 5
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean match(TryStatement node, Object other) {
	if (!(other instanceof TryStatement)) {
		return false;
	}
	TryStatement o = (TryStatement) other;
	if(isNestedUnderAnonymousClassDeclaration(node) && isNestedUnderAnonymousClassDeclaration(o)) {
		return super.match(node, o);
	}
	boolean resourceMatch = safeSubtreeListMatch(node.resources(), o.resources());
	boolean catchClauseMatch = safeSubtreeListMatch(node.catchClauses(), o.catchClauses());
	boolean finallyMatch;
	if(node.getFinally() == null && o.getFinally() == null)
		finallyMatch = true;
	else if(node.getFinally() != null && o.getFinally() != null)
		finallyMatch = safeSubtreeListMatch(node.getFinally().statements(), o.getFinally().statements());
	else
		finallyMatch = false;
	return resourceMatch && catchClauseMatch && finallyMatch;
}
 
Example 6
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getAddFinallyProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	TryStatement tryStatement= ASTResolving.findParentTryStatement(node);
	if (tryStatement == null || tryStatement.getFinally() != null) {
		return false;
	}
	Statement statement= ASTResolving.findParentStatement(node);
	if (tryStatement != statement && tryStatement.getBody() != statement) {
		return false; // an node inside a catch or finally block
	}

	if (resultingCollections == null) {
		return true;
	}

	AST ast= tryStatement.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	Block finallyBody= ast.newBlock();

	rewrite.set(tryStatement, TryStatement.FINALLY_PROPERTY, finallyBody, null);

	String label= CorrectionMessages.QuickAssistProcessor_addfinallyblock_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_FINALLY_BLOCK, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example 7
Source File: TryStatementWriter.java    From juniversal with MIT License 5 votes vote down vote up
private void writeTryWithResources(TryStatement tryStatement) {
    matchAndWrite("try", "using");

    copySpaceAndComments();
    matchAndWrite("(");

    // TODO: Check handling multiple variables, with same type, included as part of single declaration
    forEach(tryStatement.resources(), (VariableDeclarationExpression resourceDeclaration, boolean first) -> {
        if (!first) {
            copySpaceAndComments();
            matchAndWrite(";", ",");
        }

        writeNode(resourceDeclaration);
    });

    copySpaceAndComments();
    matchAndWrite(")");

    copySpaceAndComments();
    writeNode(tryStatement.getBody());

    if (tryStatement.catchClauses().size() > 0)
        throw sourceNotSupported("try-with-resources with a catch clause isn't supported; use nested try statements instead");

    if (tryStatement.getFinally() != null)
        throw sourceNotSupported("try-with-resources with a finally clause isn't supported; use nested try statements instead");
}
 
Example 8
Source File: TryStatementWriter.java    From juniversal with MIT License 5 votes vote down vote up
@Override
public void write(TryStatement tryStatement) {
    if (tryStatement.resources().size() > 0)
        writeTryWithResources(tryStatement);
    else {
        matchAndWrite("try");

        copySpaceAndComments();
        writeNode(tryStatement.getBody());

        forEach(tryStatement.catchClauses(), (CatchClause catchClause) -> {
            copySpaceAndComments();
            matchAndWrite("catch");

            copySpaceAndComments();
            matchAndWrite("(");

            copySpaceAndComments();
            writeNode(catchClause.getException());

            copySpaceAndComments();
            matchAndWrite(")");

            copySpaceAndComments();
            writeNode(catchClause.getBody());
        });

        @Nullable Block finallyBlock = tryStatement.getFinally();
        if (finallyBlock != null) {
            copySpaceAndComments();
            matchAndWrite("finally");

            copySpaceAndComments();
            writeNode(finallyBlock);
        }
    }
}
 
Example 9
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isFinallyBlockOfTryStatement(ASTNode node) {
	ASTNode parent = node.getParent();
	if(parent != null && parent instanceof TryStatement) {
		TryStatement tryStatement = (TryStatement)parent;
		Block finallyBlock = tryStatement.getFinally();
		if(node instanceof Block && finallyBlock != null) {
			return finallyBlock.equals((Block)node);
		}
	}
	return false;
}
 
Example 10
Source File: ExtractStatementsVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(Block node) {
	if(node.getParent() instanceof TryStatement) {
		TryStatement statement = (TryStatement) node.getParent();
		Block finallyBlock = statement.getFinally();
		if(finallyBlock != null && finallyBlock.equals(node))
			return false;
	}
	return true;
}
 
Example 11
Source File: CloneInstanceMapper.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isFinallyBlockOfTryStatement(ASTNode node) {
	ASTNode parent = node.getParent();
	if(parent != null && parent instanceof TryStatement) {
		TryStatement tryStatement = (TryStatement)parent;
		Block finallyBlock = tryStatement.getFinally();
		if(node instanceof Block && finallyBlock != null) {
			return finallyBlock.equals((Block)node);
		}
	}
	return false;
}
 
Example 12
Source File: StatementCollector.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(Block node) {
	if(node.getParent() instanceof TryStatement) {
		TryStatement statement = (TryStatement) node.getParent();
		Block finallyBlock = statement.getFinally();
		if(finallyBlock != null && finallyBlock.equals(node))
			return false;
	}
	return true;
}
 
Example 13
Source File: ExceptionOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	int currentSize= fCaughtExceptions.size();
	List<CatchClause> catchClauses= node.catchClauses();
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
		Type type= iter.next().getException().getType();
		if (type instanceof UnionType) {
			List<Type> types= ((UnionType) type).types();
			for (Iterator<Type> iterator= types.iterator(); iterator.hasNext();) {
				addCaughtException(iterator.next());
			}
		} else {
			addCaughtException(type);
		}
	}

	node.getBody().accept(this);

	handleResourceDeclarations(node);

	int toRemove= fCaughtExceptions.size() - currentSize;
	for (int i= toRemove; i > 0; i--) {
		fCaughtExceptions.remove(currentSize);
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null)
		node.getFinally().accept(this);

	// return false. We have visited the body by ourselves.
	return false;
}
 
Example 14
Source File: AbstractExceptionAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	fCurrentExceptions= new ArrayList<ITypeBinding>(1);
	fTryStack.push(fCurrentExceptions);

	// visit try block
	node.getBody().accept(this);

	List<VariableDeclarationExpression> resources= node.resources();
	for (Iterator<VariableDeclarationExpression> iterator= resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	// Remove those exceptions that get catch by following catch blocks
	List<CatchClause> catchClauses= node.catchClauses();
	if (!catchClauses.isEmpty())
		handleCatchArguments(catchClauses);
	List<ITypeBinding> current= fTryStack.pop();
	fCurrentExceptions= fTryStack.peek();
	for (Iterator<ITypeBinding> iter= current.iterator(); iter.hasNext();) {
		addException(iter.next(), node.getAST());
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext(); ) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null)
		node.getFinally().accept(this);

	// return false. We have visited the body by ourselves.
	return false;
}
 
Example 15
Source File: TestQ25.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	tryStatements ++;
	try2 ++;
	if (node.getFinally() != null)
		finallys ++;
	return true;
}
 
Example 16
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final TryStatement node) {
  this.appendToBuffer("try ");
  final List<ASTNode> resources = this._aSTFlattenerUtils.genericChildListProperty(node, "resources");
  boolean _isNullOrEmpty = IterableExtensions.isNullOrEmpty(resources);
  boolean _not = (!_isNullOrEmpty);
  if (_not) {
    this.appendToBuffer("(");
    for (final ASTNode child : resources) {
      child.accept(this);
    }
    this.appendToBuffer(")");
    this.addProblem(node, "Try with resource is not yet supported.");
  }
  node.getBody().accept(this);
  final Consumer<Object> _function = (Object it) -> {
    ((ASTNode) it).accept(this);
  };
  node.catchClauses().forEach(_function);
  Block _finally = node.getFinally();
  boolean _tripleNotEquals = (_finally != null);
  if (_tripleNotEquals) {
    this.appendToBuffer(" finally ");
    node.getFinally().accept(this);
  } else {
    this.appendLineWrapToBuffer();
  }
  return false;
}
 
Example 17
Source File: AbstractExceptionAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	fCurrentExceptions = new ArrayList<>(1);
	fTryStack.push(fCurrentExceptions);

	// visit try block
	node.getBody().accept(this);

	List<VariableDeclarationExpression> resources = node.resources();
	for (Iterator<VariableDeclarationExpression> iterator = resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	// Remove those exceptions that get catch by following catch blocks
	List<CatchClause> catchClauses = node.catchClauses();
	if (!catchClauses.isEmpty()) {
		handleCatchArguments(catchClauses);
	}
	List<ITypeBinding> current = fTryStack.pop();
	fCurrentExceptions = fTryStack.peek();
	for (Iterator<ITypeBinding> iter = current.iterator(); iter.hasNext();) {
		addException(iter.next(), node.getAST());
	}

	// visit catch and finally
	for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
		iter.next().accept(this);
	}
	if (node.getFinally() != null) {
		node.getFinally().accept(this);
	}

	// return false. We have visited the body by ourselves.
	return false;
}