Java 类org.eclipse.jdt.core.dom.TryStatement 实例源码
项目:o3smeasures-tool
文件:WeightMethodsPerClassVisitor.java
/**
* Method that check statement type.
* @author Mariana Azevedo
* @since 13/07/2014
* @param itStatement
*/
private void getStatementType(Object itStatement) {
if (itStatement instanceof CatchClause){
this.visitor.visit((CatchClause)itStatement);
}else if (itStatement instanceof ForStatement){
this.visitor.visit((ForStatement)itStatement);
}else if (itStatement instanceof IfStatement){
this.visitor.visit((IfStatement)itStatement);
}else if (itStatement instanceof WhileStatement){
this.visitor.visit((WhileStatement)itStatement);
}else if (itStatement instanceof TryStatement){
this.visitor.visit((TryStatement)itStatement);
}else if (itStatement instanceof ConditionalExpression){
this.visitor.visit((ConditionalExpression)itStatement);
}else if (itStatement instanceof SwitchCase){
this.visitor.visit((SwitchCase)itStatement);
}else if (itStatement instanceof DoStatement){
this.visitor.visit((DoStatement)itStatement);
}else if (itStatement instanceof ExpressionStatement){
this.visitor.visit((ExpressionStatement)itStatement);
}
}
项目: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;
}
项目: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);
}
项目:eclipse.jdt.ls
文件:StatementAnalyzer.java
@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);
}
项目:che
文件: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);
}
项目:che
文件:StatementAnalyzer.java
@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);
}
项目:Eclipse-Postfix-Code-Completion
文件:InlineTempRefactoring.java
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);
}
项目:Eclipse-Postfix-Code-Completion
文件:FlowAnalyzer.java
@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;
}
项目:Eclipse-Postfix-Code-Completion
文件: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;
}
项目:Eclipse-Postfix-Code-Completion
文件:StatementAnalyzer.java
@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);
}
项目:Eclipse-Postfix-Code-Completion
文件:QuickAssistProcessor.java
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;
}
项目:Eclipse-Postfix-Code-Completion-Juno38
文件:InlineTempRefactoring.java
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);
}
项目:Eclipse-Postfix-Code-Completion-Juno38
文件:FlowAnalyzer.java
@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;
}
项目:Eclipse-Postfix-Code-Completion-Juno38
文件: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;
}
项目:Eclipse-Postfix-Code-Completion-Juno38
文件:StatementAnalyzer.java
@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);
}
项目:Eclipse-Postfix-Code-Completion-Juno38
文件:QuickAssistProcessor.java
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, 1, image);
resultingCollections.add(proposal);
return true;
}
项目:fb-contrib-eclipse-quick-fixes
文件:HTTPClientResolution.java
@SuppressWarnings("unchecked")
private static TryStatement findLastTryStatementUsingVariable(SimpleName variable) {
// looks for the last try statement that has a reference to the variable referred to
// by the included simpleName.
// If we can't find such a try block, we give up trying to fix
Block parentBlock = TraversalUtil.findClosestAncestor(variable, Block.class);
List<Statement> statements = parentBlock.statements();
for (int i = statements.size() - 1; i >= 0; i--) {
Statement s = statements.get(i);
if (s instanceof TryStatement) {
TryStatement tryStatement = (TryStatement) s;
if (tryRefersToVariable(tryStatement, variable)) {
return tryStatement;
}
}
}
return null;
}
项目:junit2spock
文件:WrapperDecorator.java
public static Object wrap(Object statement, int indentation, Applicable applicable) {
if (statement instanceof IfStatement) {
return new IfStatementWrapper((IfStatement) statement, indentation, applicable);
} else if (statement instanceof TryStatement) {
return new TryStatementWrapper((TryStatement) statement, indentation, applicable);
}
return statement;
}
项目:junit2spock
文件:TryStatementWrapper.java
TryStatementWrapper(TryStatement statement, int indentationLevel, Applicable applicable) {
super(indentationLevel, applicable);
body.addAll(statement.resources());
ofNullable(statement.getBody()).ifPresent(block -> body.addAll(block.statements()));
this.catchClauses = (LinkedList<CatchClauseWrapper>) statement.catchClauses().stream()
.map(catchClause -> new CatchClauseWrapper((CatchClause) catchClause, indentationLevel, applicable))
.collect(toCollection(LinkedList::new));
finallyBody = ofNullable(statement.getFinally())
.map(Block::statements);
applicable.applyFeaturesToStatements(body);
finallyBody.ifPresent(applicable::applyFeaturesToStatements);
}
项目:RefDiff
文件:ASTVisitorAtomicChange.java
public boolean visit(TryStatement node) {
if (mtbStack.isEmpty()) // not part of a method
return true;
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 = mtbStack.peek();
String methodStr = getQualifiedName(mtb);
facts.add(Fact.makeTryCatchFact(bodyStr, catchClauses.toString(),
finallyStr, methodStr));
return true;
}
项目:o3smeasures-tool
文件:CyclomaticComplexityVisitor.java
/**
* @see ASTVisitor#visit(TryStatement)
*/
@Override
public boolean visit(TryStatement node) {
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
return true;
}
项目:eclipse.jdt.ls
文件:FlowContext.java
void pushExcptions(TryStatement node) {
List<CatchClause> catchClauses= node.catchClauses();
if (catchClauses == null) {
catchClauses= EMPTY_CATCH_CLAUSE;
}
fExceptionStack.add(catchClauses);
}
项目: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);
}
项目: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;
}
项目: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);
}
项目:Ref-Finder
文件:ASTVisitorAtomicChange.java
public boolean visit(TryStatement node)
{
if (this.mtbStack.isEmpty()) {
return true;
}
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;
}
项目:Ref-Finder
文件:ASTVisitorAtomicChange.java
public boolean visit(TryStatement node) {
if (mtbStack.isEmpty()) // not part of a method
return true;
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 = mtbStack.peek();
String methodStr = getQualifiedName(mtb);
facts.add(Fact.makeTryCatchFact(bodyStr, catchClauses.toString(),
finallyStr, methodStr));
return true;
}
项目:Ref-Finder
文件:ASTVisitorAtomicChange.java
public boolean visit(TryStatement node)
{
if (this.mtbStack.isEmpty()) {
return true;
}
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;
}
项目:che
文件:InlineTempRefactoring.java
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);
}
项目:che
文件: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;
}
项目:che
文件: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);
}
项目:che
文件:AbstractExceptionAnalyzer.java
@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;
}
项目:evosuite
文件:JUnitCodeGenerator.java
@SuppressWarnings("unchecked")
private TryStatement createTryStmtWithEmptyCatch(final AST ast, Statement stmt)
{
final TryStatement tryStmt = ast.newTryStatement();
tryStmt.getBody().statements().add(stmt);
final CatchClause cc = ast.newCatchClause();
SingleVariableDeclaration excDecl = ast.newSingleVariableDeclaration();
excDecl.setType(ast.newSimpleType(ast.newSimpleName("Throwable")));
excDecl.setName(ast.newSimpleName("t"));
cc.setException(excDecl);
tryStmt.catchClauses().add(cc);
return tryStmt;
}
项目:evosuite
文件:JUnitCodeGenerator.java
@SuppressWarnings("unchecked")
private TryStatement createTryStatementForPostProcessing(final AST ast, final Statement stmt, final int logRecNo)
{
final TryStatement tryStmt = this.createTryStmtWithEmptyCatch(ast, stmt);
final CatchClause cc = (CatchClause) tryStmt.catchClauses().get(0);
final MethodInvocation m = ast.newMethodInvocation();
m.setExpression(ast.newName(new String[]{"org", "evosuite","testcarver","codegen", "PostProcessor"}));
m.setName(ast.newSimpleName("captureException"));
m.arguments().add(ast.newNumberLiteral(String.valueOf(logRecNo)));
cc.getBody().statements().add(ast.newExpressionStatement(m));
MethodInvocation m2 = ast.newMethodInvocation();
m2.setExpression(ast.newSimpleName("t"));
m2.setName(ast.newSimpleName("printStackTrace"));
cc.getBody().statements().add(ast.newExpressionStatement(m2));
return tryStmt;
}
项目:evosuite
文件:CodeGenerator.java
@SuppressWarnings("unchecked")
private TryStatement createTryStmtWithEmptyCatch(final AST ast, Statement stmt)
{
final TryStatement tryStmt = ast.newTryStatement();
tryStmt.getBody().statements().add(stmt);
final CatchClause cc = ast.newCatchClause();
SingleVariableDeclaration excDecl = ast.newSingleVariableDeclaration();
excDecl.setType(ast.newSimpleType(ast.newSimpleName("Throwable")));
excDecl.setName(ast.newSimpleName("t"));
cc.setException(excDecl);
tryStmt.catchClauses().add(cc);
return tryStmt;
}
项目:Convert-For-Each-Loop-to-Lambda-Expression-Eclipse-Plugin
文件:EnhancedForStatementVisitor.java
/**
* @param nodeContaingException
* The node that throws an exception.
* @param exceptionTypes
* The list of exceptions being thrown.
* @throws JavaModelException
*/
private void handleException(ASTNode nodeContaingException, ITypeBinding... exceptionTypes)
throws JavaModelException {
Set<ITypeBinding> thrownExceptionTypeSet = new HashSet<ITypeBinding>(Arrays.asList(exceptionTypes));
// getting the parent node to check if it's an instance of tryStatement
ASTNode tryStatementParent = (nodeContaingException.getParent()).getParent();
ASTNode throwStatementParent = tryStatementParent.getParent();
if (throwStatementParent instanceof TryStatement) {
System.out.println("this is throwStatementParent");
this.encounteredThrownCheckedException = false;
}
// findTryStatmaent if there is any catch block
else if (tryStatementParent instanceof TryStatement) {
TryStatement tryStatement = (TryStatement) tryStatementParent;
@SuppressWarnings("unchecked")
List<CatchClause> catchClauses = tryStatement.catchClauses();
Stream<SingleVariableDeclaration> catchVarDeclStream = catchClauses.stream().map(CatchClause::getException);
Stream<Type> caughtExceptionTypeNodeStream = catchVarDeclStream.map(SingleVariableDeclaration::getType);
Stream<ITypeBinding> caughtExceptionTypeBindingStream = caughtExceptionTypeNodeStream
.map(Type::resolveBinding);
Stream<IJavaElement> caughtExceptionTypeJavaStream = caughtExceptionTypeBindingStream
.map(ITypeBinding::getJavaElement);
// for each thrown exception type, check if it is a sub-type of the
// caught exceptions types.
for (ITypeBinding te : thrownExceptionTypeSet) {
IType javaType = (IType) te.getJavaElement();
ITypeHierarchy supertypeHierarchy = javaType.newSupertypeHierarchy(monitor);
this.encounteredThrownCheckedException = !caughtExceptionTypeJavaStream
.anyMatch(t -> supertypeHierarchy.contains((IType) t));
}
} else {
this.encounteredThrownCheckedException = true;
}
}
项目:Beagle
文件:AstStatementInserter.java
/**
* Inserts the provided {@code statement} after the the node this inserter was created
* for. Tries to put the {@code statement} as close to the {@code marker} as possible.
*
* @param statement The statement to insert after the insertion node.
*/
void insertAfter(final Statement statement) {
Validate.notNull(statement);
Validate.validState(this.insertionList != null,
"Insertion is only possible after the inserter has been set up.");
Validate.validState(this.insertionList.isPresent(),
"Insertion is only possible if setting up the inserter succeeded.");
if (this.differentAfterList) {
this.afterList.add(0, statement);
} else if (this.breakDetector.containsControlFlowBreakingStatement(this.markedStatement)) {
// we are trying to insert after a control flow breaking statement. That’s
// dangerous, better surround with try…finally
final AST factory = this.markedStatement.getAST();
final TryStatement tryStatement = factory.newTryStatement();
tryStatement.setFinally(factory.newBlock());
@SuppressWarnings("unchecked")
final List<Statement> tryBodyStatements = tryStatement.getBody().statements();
@SuppressWarnings("unchecked")
final List<Statement> finallyStatements = tryStatement.getFinally().statements();
final Statement copy = (Statement) ASTNode.copySubtree(factory, this.markedStatement);
tryBodyStatements.add(copy);
finallyStatements.add(statement);
this.insertionList.get().set(this.markerIndex, tryStatement);
this.markedStatement = tryStatement;
this.differentAfterList = true;
this.afterList = finallyStatements;
} else {
this.insertionList.get().add(this.markerIndex, statement);
}
}
项目:Eclipse-Postfix-Code-Completion
文件:FlowAnalyzer.java
@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);
}
项目:Eclipse-Postfix-Code-Completion
文件: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);
}
项目:Eclipse-Postfix-Code-Completion
文件:AbstractExceptionAnalyzer.java
@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;
}