/** * 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); } }
boolean isExceptionCaught(ITypeBinding excpetionType) { for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) { for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) { SingleVariableDeclaration caughtException= catchClauses.next().getException(); IVariableBinding binding= caughtException.resolveBinding(); if (binding == null) { continue; } ITypeBinding caughtype= binding.getType(); while (caughtype != null) { if (caughtype == excpetionType) { return true; } caughtype= caughtype.getSuperclass(); } } } return false; }
@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; }
@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); }
@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); }
boolean isExceptionCaught(ITypeBinding excpetionType) { for (Iterator<List<CatchClause>> exceptions = fExceptionStack.iterator(); exceptions.hasNext(); ) { for (Iterator<CatchClause> catchClauses = exceptions.next().iterator(); catchClauses.hasNext(); ) { SingleVariableDeclaration caughtException = catchClauses.next().getException(); IVariableBinding binding = caughtException.resolveBinding(); if (binding == null) continue; ITypeBinding caughtype = binding.getType(); while (caughtype != null) { if (caughtype == excpetionType) return true; caughtype = caughtype.getSuperclass(); } } } return false; }
@Override public ITypeConstraint[] create(CatchClause node) { SingleVariableDeclaration exception = node.getException(); ConstraintVariable nameVariable = fConstraintVariableFactory.makeExpressionOrTypeVariable(exception.getName(), getContext()); ITypeConstraint[] defines = fTypeConstraintFactory.createDefinesConstraint( nameVariable, fConstraintVariableFactory.makeTypeVariable(exception.getType())); ITypeBinding throwable = node.getAST().resolveWellKnownType("java.lang.Throwable"); // $NON-NLS-1$ ITypeConstraint[] catchBound = fTypeConstraintFactory.createSubtypeConstraint( nameVariable, fConstraintVariableFactory.makeRawBindingVariable(throwable)); ArrayList<ITypeConstraint> result = new ArrayList<ITypeConstraint>(); result.addAll(Arrays.asList(defines)); result.addAll(Arrays.asList(catchBound)); return result.toArray(new ITypeConstraint[result.size()]); }
@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 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); }
boolean isExceptionCaught(ITypeBinding excpetionType) { for (Iterator<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) { for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) { SingleVariableDeclaration caughtException= catchClauses.next().getException(); IVariableBinding binding= caughtException.resolveBinding(); if (binding == null) continue; ITypeBinding caughtype= binding.getType(); while (caughtype != null) { if (caughtype == excpetionType) return true; caughtype= caughtype.getSuperclass(); } } } return false; }
@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; }
@Override public ITypeConstraint[] create(CatchClause node) { SingleVariableDeclaration exception= node.getException(); ConstraintVariable nameVariable= fConstraintVariableFactory.makeExpressionOrTypeVariable(exception.getName(), getContext()); ITypeConstraint[] defines= fTypeConstraintFactory.createDefinesConstraint( nameVariable, fConstraintVariableFactory.makeTypeVariable(exception.getType())); ITypeBinding throwable= node.getAST().resolveWellKnownType("java.lang.Throwable"); //$NON-NLS-1$ ITypeConstraint[] catchBound= fTypeConstraintFactory.createSubtypeConstraint( nameVariable, fConstraintVariableFactory.makeRawBindingVariable(throwable)); ArrayList<ITypeConstraint> result= new ArrayList<ITypeConstraint>(); result.addAll(Arrays.asList(defines)); result.addAll(Arrays.asList(catchBound)); return result.toArray(new ITypeConstraint[result.size()]); }
@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); }
/** * 'catch(' exception ')' '{' body '}' */ @Override public boolean visit(final CatchClause node) { final int lineNo = lineStart(node); // new dependence final IResolvedLine rd = createDependence(LK_CATCH, lineNo, mdg.parent()); // control node parent final ASTNode controlNode = ASTTools.enclosingControlScope(node); // last line of the control scope final int lineEnd = lineEnd(controlNode); // the catch dependence must be propagated to all lines in the method's body jumpDependences.put(lineEnd, rd); // new variable use final IResolvedData rv = factory.resolveExpression(node.getException().getName(), node, null, false, true); // map this definition rd.definitions().add(rv); // append the control dependence rd.uses().add(rv); // recursively append dependences appendRecursive(lineNo, rd, node.getBody()); // do not visit children return false; }
@Override public DSubTree handle() { DSubTree tree = new DSubTree(); // restriction: considering only the first catch clause DSubTree Ttry = new DOMBlock(statement.getBody()).handle(); DSubTree Tcatch; if (! statement.catchClauses().isEmpty()) Tcatch = new DOMCatchClause((CatchClause) statement.catchClauses().get(0)).handle(); else Tcatch = new DSubTree(); DSubTree Tfinally = new DOMBlock(statement.getFinally()).handle(); boolean except = Ttry.isValid() && Tcatch.isValid(); if (except) tree.addNode(new DExcept(Ttry.getNodes(), Tcatch.getNodes())); else { // only one of these will add nodes tree.addNodes(Ttry.getNodes()); tree.addNodes(Tcatch.getNodes()); } tree.addNodes(Tfinally.getNodes()); return tree; }
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); }
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; }
/** * @see ASTVisitor#visit(CatchClause) */ @Override public boolean visit(CatchClause node) { cyclomaticComplexityIndex++; sumCyclomaticComplexity++; return true; }
@Override public boolean visit(CatchClause node) { // open a new scope for the exception declaration. fScopes.add(fScope); fScope = new Scope(fScope, node.getStartPosition(), node.getLength()); return true; }
void pushExcptions(TryStatement node) { List<CatchClause> catchClauses= node.catchClauses(); if (catchClauses == null) { catchClauses= EMPTY_CATCH_CLAUSE; } fExceptionStack.add(catchClauses); }
@Override public void endVisit(CatchClause node) { if (skipNode(node)) { return; } processSequential(node, node.getException(), node.getBody()); }
@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; }
private void handleCatchArguments(List<CatchClause> 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();) { removeCaughtExceptions(iterator.next().resolveBinding()); } } else { removeCaughtExceptions(type.resolveBinding()); } } }
@Override public boolean visit(CatchClause node) { SingleVariableDeclaration param = node.getException(); ITypeBinding type = param.resolveBinding().getType(); if (!hasAnnotation(param.modifiers())) { if (!type.isPrimitive()) { if (type.getName().compareTo("String") != 0) { setParameterAnnotation(rewrite, param, "lent"); } } } return super.visit(node); }
@Override public boolean visit(CatchClause node) { SingleVariableDeclaration param = node.getException(); ITypeBinding type = param.resolveBinding().getType(); SingleMemberAnnotation annot = hasAnnotation(param.modifiers()); if (annot != null) { ListRewrite paramRewrite = rewrite.getListRewrite(param, SingleVariableDeclaration.MODIFIERS2_PROPERTY); paramRewrite.remove(annot, null); } return super.visit(node); }
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; }
@Override public boolean visit(CatchClause node) { if (isInside(node)) { node.getBody().accept(this); node.getException().accept(this); } return false; }
@Override public boolean visit(CatchClause node) { SingleVariableDeclaration exception = node.getException(); IVariableBinding variableBinding = exception.resolveBinding(); VariableVariable2 cv = fTCModel.makeDeclaredVariableVariable(variableBinding, fCU); setConstraintVariable(exception, cv); return true; }
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) { 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; }