protected override string InnerTranslate() { string finallyStr = ""; if (Finally != null) { finallyStr = Finally.Translate(); } if (Catches.GetEnumerable().Count() > 1) { return($@"try {Block.Translate()} catch(__ex__){{ {BuildCatchBlock()} }} {finallyStr}"); } return($@"try {Block.Translate()} {Catches.Translate()} {finallyStr}"); }
private void SaveData() { string json = JsonConvert.SerializeObject(Lures.ToList()); File.WriteAllText(lureFileName, json); json = JsonConvert.SerializeObject(Catches.ToList()); File.WriteAllText(catchFileName, json); }
internal int RunBlock(BlockSyntax node, VarFrame vf, int pc = 0) { var prevVars = Vars; Vars = vf; var children = node.ChildNodes().ToArray(); if (children.Length == pc) { return(-1); } for (; pc < children.Length; pc++) { var child = children[pc]; try { Run(child); } catch (Exception e) when(Catches.Count > 0) { Console.WriteLine(e); foreach (var c in Catches.Reverse()) { if (c.RunCatch(e)) { break; } } } if (Ctx.IsExpird()) { throw new TimeoutException(); } if (Halt != HaltType.None) { pc++; break; } } Vars = prevVars; return(pc); }
private string BuildCatchBlock() { StringBuilder bd = new StringBuilder(); foreach (var item in Catches.GetEnumerable()) { string str = ""; string name = "__ex__"; if (item.Syntax.Declaration.Identifier.ToString() != string.Empty) { str = $"var {item.Syntax.Declaration.Identifier } = __ex__;"; name = item.Syntax.Declaration.Identifier.ToString(); } str += $"if( {name } instanceof {item.Syntax.Declaration.Type})" + $"\n {item.Block} \n"; bd.Append(str); } return(bd.ToString()); }
private void RunTry(TryStatementSyntax node) { bool hasCatches = node.Catches.Count > 0; if (hasCatches) { Catches.Push(new CatchFrame(this, node)); } try { RunBlock(node.Block); } finally { if (node.Finally != null) { Run(node.Finally.Block); } } if (hasCatches) { Catches.Pop(); } }
/// <summary> /// Finds the <see cref="RetryCatch"/> object that will handle the given <see cref="Exception"/>. /// </summary> /// <param name="exception"></param> /// <returns></returns> RetryCatch FindCatch(Exception exception) { return(Catches.FirstOrDefault(i => i.ExceptionType == exception.GetType() || i.ExceptionType.IsAssignableFrom(exception.GetType()))); }
public void AnalyzeACatchBlock(CatchClauseSyntax catchblock) { CatchBlock catchBlockInfo = new CatchBlock(); var tree = catchblock.SyntaxTree; var model = TreeAndModelDic[tree]; TypeSyntax exceptionTypeSyntax = null; INamedTypeSymbol exceptionNamedTypeSymbol = null; if (catchblock.Declaration != null) { exceptionTypeSyntax = catchblock.Declaration.Type; exceptionNamedTypeSymbol = model.GetTypeInfo(exceptionTypeSyntax).ConvertedType as INamedTypeSymbol; if (exceptionNamedTypeSymbol != null) { catchBlockInfo.ExceptionType = exceptionNamedTypeSymbol.ToString(); //Binding info: if (exceptionNamedTypeSymbol.BaseType != null) { catchBlockInfo.OperationFeatures["Binded"] = 1; catchBlockInfo.OperationFeatures["RecoveredBinding"] = model.IsSpeculativeSemanticModel ? 1 : 0; catchBlockInfo.OperationFeatures["Kind"] = ASTUtilities.FindKind(exceptionNamedTypeSymbol, Compilation); } else { catchBlockInfo.OperationFeatures["Binded"] = 0; } } else { catchBlockInfo.ExceptionType = "!NO_NAMED_TYPE!"; } } else { catchBlockInfo.ExceptionType = "!NO_EXCEPTION_DECLARED!"; } //Basic info: catchBlockInfo.MetaInfo["ExceptionType"] = catchBlockInfo.ExceptionType; //Try info: var tryBlock = catchblock.Parent as TryStatementSyntax; catchBlockInfo.MetaInfo["TryBlock"] = tryBlock.ToString(); catchBlockInfo.OperationFeatures["ParentNodeType"] = ASTUtilities.FindParent(tryBlock).RawKind; catchBlockInfo.MetaInfo["ParentNodeType"] = ASTUtilities.FindParent(tryBlock).Kind().ToString(); //Common Features - try/catch block var tryNoTriviaCount = ASTUtilities.countLines(tryBlock.Block) - 2; var tryFileLinePositionSpan = tree.GetLineSpan(tryBlock.Block.Span); var tryStartLine = tryFileLinePositionSpan.StartLinePosition.Line + 1; var tryEndLine = tryFileLinePositionSpan.EndLinePosition.Line + 1; catchBlockInfo.OperationFeatures["TryStartLine"] = tryStartLine; catchBlockInfo.OperationFeatures["TryEndLine"] = tryEndLine; catchBlockInfo.MetaInfo["TryLine"] = tryStartLine.ToString(); catchBlockInfo.OperationFeatures["TryLOC"] = tryNoTriviaCount; var catchNoTriviaCount = ASTUtilities.countLines(catchblock.Block) - 2; var catchFileLinePositionSpan = tree.GetLineSpan(catchblock.Block.Span); var catchStartLine = catchFileLinePositionSpan.StartLinePosition.Line + 1; var catchEndLine = catchFileLinePositionSpan.EndLinePosition.Line + 1; catchBlockInfo.OperationFeatures["CatchStartLine"] = catchStartLine; catchBlockInfo.OperationFeatures["CatchEndLine"] = catchEndLine; catchBlockInfo.OperationFeatures["CatchLOC"] = catchNoTriviaCount; catchBlockInfo.OperationFeatures["CatchStart"] = catchFileLinePositionSpan.StartLinePosition.Line; catchBlockInfo.OperationFeatures["CatchLength"] = catchFileLinePositionSpan.EndLinePosition.Line - catchFileLinePositionSpan.StartLinePosition.Line; catchBlockInfo.MetaInfo["CatchBlock"] = catchblock.ToString(); catchBlockInfo.FilePath = tree.FilePath; catchBlockInfo.StartLine = catchStartLine; catchBlockInfo.MetaInfo["FilePath"] = tree.FilePath; catchBlockInfo.MetaInfo["StartLine"] = catchStartLine.ToString(); //Common Features - parent type catchBlockInfo.ParentType = ASTUtilities.FindParentType(tryBlock, model); catchBlockInfo.MetaInfo["ParentType"] = catchBlockInfo.ParentType; //Common Features - parent method name SyntaxNode parentNode = ASTUtilities.FindParentMethod(tryBlock); catchBlockInfo.ParentMethod = ASTUtilities.GetMethodName(parentNode, TreeAndModelDic, Compilation);; catchBlockInfo.MetaInfo["ParentMethod"] = catchBlockInfo.ParentMethod; //Common Features if (parentNode.IsKind(SyntaxKind.MethodDeclaration)) { parentNode = (parentNode as MethodDeclarationSyntax).Body; } if (parentNode.IsKind(SyntaxKind.ConstructorDeclaration)) { parentNode = (parentNode as ConstructorDeclarationSyntax).Body; } var parentMethodNoTriviaCount = ASTUtilities.countLines(parentNode) - 2; var parentMethodFileLinePositionSpan = tree.GetLineSpan(parentNode.Span); var parentMethodStartLine = parentMethodFileLinePositionSpan.StartLinePosition.Line + 1; var parentMethodEndLine = parentMethodFileLinePositionSpan.EndLinePosition.Line + 1; catchBlockInfo.OperationFeatures["MethodStartLine"] = parentMethodStartLine; catchBlockInfo.OperationFeatures["MethodEndLine"] = parentMethodStartLine; catchBlockInfo.OperationFeatures["MethodLOC"] = parentMethodNoTriviaCount; //Treatment for TryStatement bool hasTryStatement = catchblock.DescendantNodesAndSelf() .OfType <TryStatementSyntax>().Any(); SyntaxNode updatedCatchBlock = catchblock; if (hasTryStatement == true) { try { // remove try-catch-finally block inside updatedCatchBlock = tryblockremover.Visit(catchblock); } catch (System.ArgumentNullException e) { // ignore the ArgumentNullException } } //Treatment for TryStatement //RecoverFlag - (based on inner try blocks) var recoverStatement = FindRecoverStatement(catchblock, model); if (recoverStatement != null) { catchBlockInfo.MetaInfo["RecoverFlag"] = recoverStatement.ToString(); catchBlockInfo.OperationFeatures["RecoverFlag"] = 1; } /* * Flagging inner catch * CatchClause is a child of a TryStatement, which is a child of a Block, which we wanna know the parent. * If CatchClause it's the parent, then it's an inner catch. Get the line of the parent try of that. */ if (IsInnerCatch(catchblock.Parent)) { catchBlockInfo.OperationFeatures["InnerCatch"] = 1; catchBlockInfo.OperationFeatures["ParentTryStartLine"] = tree.GetLineSpan((FindParentCatch(catchblock.Parent).Parent as TryStatementSyntax).Block.Span).StartLinePosition.Line + 1;// tree.getLineNumber(node.getParent().getParent().getParent().getParent().getStartPosition() + 1)); } //Treatment for MethodInvocation //Collection of data for statements of: logging, abort, //Logging var loggingStatement = FindLoggingIn(updatedCatchBlock); if (loggingStatement != null) { catchBlockInfo.MetaInfo["Logged"] = loggingStatement.ToString(); catchBlockInfo.OperationFeatures["Logged"] = 1; if (CountLoggingIn(updatedCatchBlock) > 1) { catchBlockInfo.OperationFeatures["MultiLog"] = 1; } } //Abort var abortStatement = FindAbortIn(updatedCatchBlock); if (abortStatement != null) { catchBlockInfo.MetaInfo["Abort"] = abortStatement.ToString(); catchBlockInfo.OperationFeatures["Abort"] = 1; } //GetCause - C# is inner exception var getCauseStatement = FindGetCauseIn(updatedCatchBlock); if (getCauseStatement != null) { catchBlockInfo.MetaInfo["GetCause"] = getCauseStatement.ToString(); catchBlockInfo.OperationFeatures["GetCause"] = 1; } //Other - Other INVOCATION var otherStatement = FindOtherIn(updatedCatchBlock); if (otherStatement != null) { catchBlockInfo.MetaInfo["OtherInvocation"] = otherStatement.ToString(); catchBlockInfo.OperationFeatures["OtherInvocation"] = 1; } //Treatment for ThrowStatement //Collection of data for statements of: throw var throwStatement = FindThrowIn(updatedCatchBlock); if (throwStatement != null) { catchBlockInfo.MetaInfo["Thrown"] = throwStatement.ToString(); catchBlockInfo.OperationFeatures["NumThrown"] = CountThrowIn(updatedCatchBlock); catchBlockInfo.OperationFeatures["NumThrowNew"] = CountThrowNewIn(updatedCatchBlock); catchBlockInfo.OperationFeatures["NumThrowWrapCurrentException"] = CountThrowWrapIn(updatedCatchBlock, catchblock.Declaration?.Identifier.ToString()); } //Treatment for ReturnStatement var returnStatement = FindReturnIn(updatedCatchBlock); if (returnStatement != null) { catchBlockInfo.MetaInfo["Return"] = returnStatement.ToString(); catchBlockInfo.OperationFeatures["Return"] = 1; } //Treatment for ContinueStatement var continueStatement = FindContinueIn(updatedCatchBlock); if (continueStatement != null) { catchBlockInfo.MetaInfo["Continue"] = continueStatement.ToString(); catchBlockInfo.OperationFeatures["Continue"] = 1; } //var setLogicFlag = FindSetLogicFlagIn(updatedCatchBlock); //if (setLogicFlag != null) //{ // catchBlockInfo.MetaInfo["SetLogicFlag"] = setLogicFlag.ToString(); // catchBlockInfo.OperationFeatures["SetLogicFlag"] = 1; //} //var otherOperation = HasOtherOperation(updatedCatchBlock, model); //if (otherOperation != null) //{ // catchBlockInfo.MetaInfo["OtherOperation"] = otherOperation.ToString(); // catchBlockInfo.OperationFeatures["OtherOperation"] = 1; //} //EmptyBlock if (IsEmptyBlock(updatedCatchBlock)) { catchBlockInfo.OperationFeatures["EmptyBlock"] = 1; } //CatchException if (exceptionNamedTypeSymbol != null) { if (exceptionNamedTypeSymbol.Equals(Compilation.GetTypeByMetadataName("System.Exception"))) { catchBlockInfo.OperationFeatures["CatchException"] = 1; } else { catchBlockInfo.OperationFeatures["CatchException"] = 0; } } //ToDo if (IsToDo(updatedCatchBlock)) { catchBlockInfo.OperationFeatures["ToDo"] = 1; } //var variableAndComments = GetVariablesAndComments(tryBlock.Block); //var containingMethod = GetContainingMethodName(tryBlock, model); //var methodNameList = GetAllInvokedMethodNamesByBFS(tryBlock.Block, treeAndModelDic, compilation); var tryPossibleExceptionsCustomVisitor = new PossibleExceptionsCustomVisitor(Compilation, TreeAndModelDic, 0, true, tree.FilePath, catchStartLine, exceptionNamedTypeSymbol); tryPossibleExceptionsCustomVisitor.Visit(tryBlock.Block); /* * Process for possible exceptions */ getExceptionFlows(this.PossibleExceptionsList, tryPossibleExceptionsCustomVisitor.getClosedExceptionFlows(), Compilation); //catchBlockInfo.MetaInfo["TryMethods"] = possibleExceptionsCustomVisitor.PrintInvokedMethodsHandlerType(); catchBlockInfo.MetaInfo["TryMethodsAndExceptions"] = tryPossibleExceptionsCustomVisitor.PrintInvokedMethodsPossibleExceptions(); catchBlockInfo.OperationFeatures["NumDistinctMethods"] = tryPossibleExceptionsCustomVisitor.countInvokedMethodsHandlerType(); catchBlockInfo.MetaInfo["TryMethodsBinded"] = tryPossibleExceptionsCustomVisitor.PrintInvokedMethodsBinded(); catchBlockInfo.OperationFeatures["NumDistinctMethodsNotBinded"] = tryPossibleExceptionsCustomVisitor.getNumMethodsNotBinded(); catchBlockInfo.MetaInfo["DistinctExceptions"] = tryPossibleExceptionsCustomVisitor.PrintDistinctPossibleExceptions(); catchBlockInfo.OperationFeatures["NumDistinctExceptions"] = tryPossibleExceptionsCustomVisitor.getDistinctPossibleExceptions().Count; catchBlockInfo.OperationFeatures["NumSpecificHandler"] = tryPossibleExceptionsCustomVisitor.getNumSpecificHandler(); catchBlockInfo.OperationFeatures["NumSubsumptionHandler"] = tryPossibleExceptionsCustomVisitor.getNumSubsumptionHandler(); catchBlockInfo.OperationFeatures["NumSupersumptionHandler"] = tryPossibleExceptionsCustomVisitor.getNumSupersumptionHandler(); catchBlockInfo.OperationFeatures["NumOtherHandler"] = tryPossibleExceptionsCustomVisitor.getNumOtherHandler(); catchBlockInfo.OperationFeatures["MaxLevel"] = tryPossibleExceptionsCustomVisitor.getChildrenMaxLevel(); catchBlockInfo.OperationFeatures["NumIsDocSemantic"] = tryPossibleExceptionsCustomVisitor.getNumIsDocSemantic(); catchBlockInfo.OperationFeatures["NumIsDocSyntax"] = tryPossibleExceptionsCustomVisitor.getNumIsDocSyntax(); catchBlockInfo.OperationFeatures["NumIsThrow"] = tryPossibleExceptionsCustomVisitor.getNumIsThrow(); //FinallyThrowing FinallyClauseSyntax finallyBlock = tryBlock.Finally; if (finallyBlock != null) { catchBlockInfo.MetaInfo["FinallyBlock"] = finallyBlock.ToString(); var finallyPossibleExceptionsCustomVisitor = new PossibleExceptionsCustomVisitor(Compilation, TreeAndModelDic, 0, true, tree.FilePath, catchStartLine, exceptionNamedTypeSymbol); finallyPossibleExceptionsCustomVisitor.Visit(finallyBlock.Block); if (finallyBlock.DescendantNodes().OfType <ThrowStatementSyntax>().Any() || finallyPossibleExceptionsCustomVisitor.getDistinctPossibleExceptions().Count > 0) { catchBlockInfo.OperationFeatures["FinallyThrowing"] = 1; } } //var methodAndExceptionList = GetAllInvokedMethodNamesAndExceptionsByBFS(tryBlock.Block, treeAndModelDic, compilation); //catchBlockInfo.OperationFeatures["NumMethod"] = methodAndExceptionList[0].Count; //catchBlockInfo.OperationFeatures["NumExceptions"] = methodAndExceptionList[1].Count; //catchBlockInfo.TextFeatures = methodAndExceptionList[0]; //if (containingMethod != null) //{ // MergeDic<string>(ref catchBlockInfo.TextFeatures, // new Dictionary<string, int>() { { containingMethod, 1 } }); //} //MergeDic<string>(ref catchBlockInfo.TextFeatures, // new Dictionary<string, int>() { { "##spliter##", 0 } }); // to seperate methods and variables //MergeDic<string>(ref catchBlockInfo.TextFeatures, variableAndComments); Catches.Add(catchBlockInfo); }