类org.eclipse.jdt.core.dom.TryStatement源码实例Demo

下面列出了怎么用org.eclipse.jdt.core.dom.TryStatement的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@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;
}
 
源代码2 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@Override
public void endVisit(TryStatement node) {
	if (skipNode(node)) {
		return;
	}
	TryFlowInfo info = createTry();
	setFlowInfo(node, info);
	for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
		info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
	}
	info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
	for (Iterator<CatchClause> iter = node.catchClauses().iterator(); iter.hasNext();) {
		CatchClause element = iter.next();
		info.mergeCatch(getFlowInfo(element), fFlowContext);
	}
	info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
 
private RefactoringStatus checkSelection(VariableDeclaration decl) {
	ASTNode parent= decl.getParent();
	if (parent instanceof MethodDeclaration) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter);
	}

	if (parent instanceof CatchClause) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources);
	}

	if (decl.getInitializer() == null) {
		String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
		return RefactoringStatus.createFatalErrorStatus(message);
	}

	return checkAssignments(decl);
}
 
@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;
}
 
private RefactoringStatus checkExpression() throws JavaModelException {
	Expression selectedExpression= getSelectedExpression().getAssociatedExpression();
	if (selectedExpression != null) {
		final ASTNode parent= selectedExpression.getParent();
		if (selectedExpression instanceof NullLiteral) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
		} else if (selectedExpression instanceof ArrayInitializer) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
		} else if (selectedExpression instanceof Assignment) {
			if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression))
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
			else
				return null;
		} else if (selectedExpression instanceof SimpleName) {
			if ((((SimpleName) selectedExpression)).isDeclaration())
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
			if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
		} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
		}
	}

	return null;
}
 
@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);
}
 
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;
}
 
源代码8 项目: JDeodorant   文件: ASTNodeMatcher.java
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;
}
 
protected ListRewrite createTryStatementIfNeeded(ASTRewrite sourceRewriter, AST ast, ListRewrite bodyRewrite, PDGNode node) {
	Statement statement = node.getASTStatement();
	ASTNode statementParent = statement.getParent();
	if(statementParent != null && statementParent instanceof Block)
		statementParent = statementParent.getParent();
	if(statementParent != null && statementParent instanceof TryStatement) {
		TryStatement tryStatementParent = (TryStatement)statementParent;
		if(tryStatementsToBeRemoved.contains(tryStatementParent) || tryStatementsToBeCopied.contains(tryStatementParent)) {
			if(tryStatementBodyRewriteMap.containsKey(tryStatementParent)) {
				bodyRewrite = tryStatementBodyRewriteMap.get(tryStatementParent);
			}
			else {
				TryStatement newTryStatement = copyTryStatement(sourceRewriter, ast, tryStatementParent);
				Block tryMethodBody = ast.newBlock();
				sourceRewriter.set(newTryStatement, TryStatement.BODY_PROPERTY, tryMethodBody, null);
				ListRewrite tryBodyRewrite = sourceRewriter.getListRewrite(tryMethodBody, Block.STATEMENTS_PROPERTY);
				tryStatementBodyRewriteMap.put(tryStatementParent, tryBodyRewrite);
				bodyRewrite.insertLast(newTryStatement, null);
				bodyRewrite = tryBodyRewrite;
			}
		}
	}
	return bodyRewrite;
}
 
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;
}
 
源代码11 项目: JDeodorant   文件: ExtractMethodRefactoring.java
private void processTryStatement(TryStatement tryStatement) {
	List<Statement> nestedStatements = getStatements(tryStatement);
	Set<Statement> removableStatements = slice.getRemovableStatements();
	Set<Statement> sliceStatements = slice.getSliceStatements();
	boolean allNestedStatementsAreRemovable = true;
	boolean sliceStatementThrowsException = false;
	for(Statement nestedStatement : nestedStatements) {
		if(!removableStatements.contains(nestedStatement)) {
			allNestedStatementsAreRemovable = false;
		}
		if(sliceStatements.contains(nestedStatement)) {
			Set<ITypeBinding> thrownExceptionTypes = getThrownExceptionTypes(nestedStatement);
			if(thrownExceptionTypes.size() > 0)
				sliceStatementThrowsException = true;
		}
	}
	if(slice.getSliceStatements().contains(tryStatement)) {
		if(allNestedStatementsAreRemovable)
			tryStatementsToBeRemoved.add(tryStatement);
		else if(sliceStatementThrowsException)
			tryStatementsToBeCopied.add(tryStatement);
	}
}
 
源代码12 项目: JDeodorant   文件: StyledStringVisitor.java
public boolean visit(TryStatement stmnt){
	/*
	 * JLS 4:
	 * 	try [ ( Resources ) ]
	        Block
        	    [ { CatchClause } ]
        	    [ finally Block ]
	 */
	styledString.append("try", new StyledStringStyler(keywordStyle));
	if(!stmnt.resources().isEmpty()) {
		appendSpace();
		appendOpenParenthesis();
		for(int i=0; i<stmnt.resources().size(); i++) {
			handleExpression((VariableDeclarationExpression) stmnt.resources().get(i));
			if(i < stmnt.resources().size() - 1) {
				appendSemicolon();
				appendSpace();
			}
		}
		appendClosedParenthesis();
	}
	return false;
}
 
源代码13 项目: SimFix   文件: TryPurifyByException.java
private ASTNode tryCatchStmt(AST ast, ASTNode node){
	Block block = ast.newBlock();
	block.statements().add(ASTNode.copySubtree(ast, node));
	TryStatement tryStatement = ast.newTryStatement();
	tryStatement.setBody(block);
	CatchClause catchClause = ast.newCatchClause();
	SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
	svd.setType(ast.newSimpleType(ast.newSimpleName("Exception")));
	svd.setName(ast.newSimpleName("mException"));
	catchClause.setException(svd);
	tryStatement.catchClauses().add(catchClause);
	return tryStatement;
}
 
源代码14 项目: SimFix   文件: CodeBlock.java
private TryStmt visit(TryStatement node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	TryStmt tryStmt = new TryStmt(startLine, endLine, node);
	
	Blk blk = (Blk) process(node.getBody());
	blk.setParent(tryStmt);
	tryStmt.setBody(blk);
	
	return tryStmt;
}
 
public List<ImplementationCodeSmell> detectEmptyCatchBlock() {
	MethodControlFlowVisitor visitor = new MethodControlFlowVisitor();
	methodMetrics.getMethod().getMethodDeclaration().accept(visitor);
	for (TryStatement tryStatement : visitor.getTryStatements()) {
		for (Object catchClause : tryStatement.catchClauses()) {
			if (!hasBody((CatchClause) catchClause)) {
				addToSmells(initializeCodeSmell(EMPTY_CATCH_CLAUSE));
			}
		}
	}
	return smells;
}
 
源代码16 项目: eclipse.jdt.ls   文件: FlowContext.java
void pushExcptions(TryStatement node) {
	List<CatchClause> catchClauses= node.catchClauses();
	if (catchClauses == null) {
		catchClauses= EMPTY_CATCH_CLAUSE;
	}
	fExceptionStack.add(catchClauses);
}
 
源代码17 项目: eclipse.jdt.ls   文件: ExtractTempRefactoring.java
private RefactoringStatus checkExpression() throws JavaModelException {
	Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
	if (selectedExpression != null) {
		final ASTNode parent = selectedExpression.getParent();
		if (selectedExpression instanceof NullLiteral) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
		} else if (selectedExpression instanceof ArrayInitializer) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
		} else if (selectedExpression instanceof Assignment) {
			if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
			} else {
				return null;
			}
		} else if (selectedExpression instanceof SimpleName) {
			if ((((SimpleName) selectedExpression)).isDeclaration()) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
			}
			if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
			}
		} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
		}
	}

	return null;
}
 
源代码18 项目: eclipse.jdt.ls   文件: ExtractMethodAnalyzer.java
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
		if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
			invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_resource_in_try_with_resources, JavaStatusContext.create(fCUnit, getSelection()));
		}
	}
	checkTypeInDeclaration(node.getType());
	super.endVisit(node);
}
 
源代码19 项目: eclipse.jdt.ls   文件: ExtractFieldRefactoring.java
private RefactoringStatus checkExpression() throws JavaModelException {
	Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
	if (selectedExpression != null) {
		final ASTNode parent = selectedExpression.getParent();
		if (selectedExpression instanceof NullLiteral) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
		} else if (selectedExpression instanceof ArrayInitializer) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
		} else if (selectedExpression instanceof Assignment) {
			if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression)) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
			} else {
				return null;
			}
		} else if (selectedExpression instanceof SimpleName) {
			if ((((SimpleName) selectedExpression)).isDeclaration()) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
			}
			if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
			}
		} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
		}
	}

	return null;
}
 
源代码20 项目: eclipse.jdt.ls   文件: AbstractExceptionAnalyzer.java
@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;
}
 
源代码21 项目: eclipse.jdt.ls   文件: AbstractExceptionAnalyzer.java
@Override
public boolean visit(VariableDeclarationExpression node) {
	if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
		Type type = node.getType();
		ITypeBinding resourceTypeBinding = type.resolveBinding();
		if (resourceTypeBinding != null) {
			IMethodBinding methodBinding = Bindings.findMethodInHierarchy(resourceTypeBinding, "close", new ITypeBinding[0]); //$NON-NLS-1$
			if (methodBinding != null) {
				addExceptions(methodBinding.getExceptionTypes(), node.getAST());
			}
		}
	}
	return super.visit(node);
}
 
源代码22 项目: xtext-xtend   文件: JavaASTFlattener.java
@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;
}
 
源代码23 项目: compiler   文件: TestQ25.java
@Override
public boolean visit(TryStatement node) {
	tryStatements ++;
	try2 ++;
	if (node.getFinally() != null)
		finallys ++;
	return true;
}
 
@Override
public void endVisit(TryStatement node) {
	if (skipNode(node))
		return;
	TryFlowInfo info= createTry();
	setFlowInfo(node, info);
	info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
	for (Iterator<CatchClause> iter= node.catchClauses().iterator(); iter.hasNext();) {
		CatchClause element= iter.next();
		info.mergeCatch(getFlowInfo(element), fFlowContext);
	}
	info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
 
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
		if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
			invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_resource_in_try_with_resources, JavaStatusContext.create(fCUnit, getSelection()));
		}
	}
	checkTypeInDeclaration(node.getType());
	super.endVisit(node);
}
 
@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;
}
 
@Override
public boolean visit(VariableDeclarationExpression node) {
	if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
		Type type= node.getType();
		ITypeBinding resourceTypeBinding= type.resolveBinding();
		if (resourceTypeBinding != null) {
			IMethodBinding methodBinding= Bindings.findMethodInHierarchy(resourceTypeBinding, "close", new ITypeBinding[0]); //$NON-NLS-1$
			if (methodBinding != null) {
				addExceptions(methodBinding.getExceptionTypes(), node.getAST());
			}
		}
	}
	return super.visit(node);
}
 
private void handleResourceDeclarations(TryStatement tryStatement) {
	List<VariableDeclarationExpression> resources= tryStatement.resources();
	for (Iterator<VariableDeclarationExpression> iterator= resources.iterator(); iterator.hasNext();) {
		iterator.next().accept(this);
	}

	//check if the exception is thrown as a result of resource#close()
	boolean exitMarked= false;
	for (VariableDeclarationExpression variable : resources) {
		Type type= variable.getType();
		IMethodBinding methodBinding= Bindings.findMethodInHierarchy(type.resolveBinding(), "close", new ITypeBinding[0]); //$NON-NLS-1$
		if (methodBinding != null) {
			ITypeBinding[] exceptionTypes= methodBinding.getExceptionTypes();
			for (int j= 0; j < exceptionTypes.length; j++) {
				if (matches(exceptionTypes[j])) { // a close() throws the caught exception
					// mark name of resource
					for (VariableDeclarationFragment fragment : (List<VariableDeclarationFragment>) variable.fragments()) {
						SimpleName name= fragment.getName();
						fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fDescription));
					}
					if (!exitMarked) {
						// mark exit position
						exitMarked= true;
						Block body= tryStatement.getBody();
						int offset= body.getStartPosition() + body.getLength() - 1; // closing bracket of try block
						fResult.add(new OccurrenceLocation(offset, 1, 0, Messages.format(SearchMessages.ExceptionOccurrencesFinder_occurrence_implicit_close_description,
								BasicElementLabels.getJavaElementName(fException.getName()))));
					}
				}
			}
		}
	}
}
 
@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;
}
 
public static TryStatement findParentTryStatement(ASTNode node) {
	while ((node != null) && (!(node instanceof TryStatement))) {
		node= node.getParent();
		if (node instanceof BodyDeclaration) {
			return null;
		}
	}
	return (TryStatement) node;
}
 
 类所在包
 同包方法