/// <summary> /// Wraps a call to GetTypeContractFor inside of a try-catch statement. /// </summary> public static bool TryGetTypeContract(CodeContractAwareHostEnvironment host, ITypeReference type, out ITypeContract typeContract, DocTracker docTracker) { Contract.Requires(host != null); Contract.Requires(type != null); Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out typeContract) != null); try { var unit = TypeHelper.GetDefiningUnit(type.ResolvedType); if (unit == null) { typeContract = null; } else { IContractProvider lcp = host.GetContractExtractor(unit.UnitIdentity); if (lcp == null) { typeContract = null; } else { typeContract = lcp.GetTypeContractFor(type); } } } catch (NullReferenceException) { docTracker.WriteLine("ERROR: NullReferenceException was thrown in CCI!"); typeContract = null; } return(typeContract != null); }
/// <summary> /// Wraps a call to GetMethodContractFor inside of a try-catch statement. /// </summary> public static bool TryGetMethodContract(CodeContractAwareHostEnvironment host, IMethodReference method, out IMethodContract methodContract, DocTracker docTracker) { Contract.Requires(host != null); Contract.Requires(method != null); Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out methodContract) != null); try { methodContract = ContractHelper.GetMethodContractFor(host, method.ResolvedMethod); } catch (NullReferenceException) { docTracker.WriteLine("ERROR: NullReferenceException was thrown in CCI!"); methodContract = null; } //} catch (Exception e) { // docTracker.WriteLine("ERROR: Exception of type '{0}' was thrown in CCI!", e.GetType().Name); // docTracker.WriteLine("\t'{0}'", e.Message); // methodContract = null; //} return(methodContract != null); }
/// <summary> /// Wraps a call to GetTypeContractFor inside of a try-catch statement. /// </summary> public static bool TryGetTypeContract(CodeContractAwareHostEnvironment host, ITypeReference type, out ITypeContract typeContract, DocTracker docTracker) { Contract.Requires(host != null); Contract.Requires(type != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out typeContract) != null); try { var unit = TypeHelper.GetDefiningUnit(type.ResolvedType); if (unit == null) { typeContract = null; } else { IContractProvider lcp = host.GetContractExtractor(unit.UnitIdentity); if (lcp == null) { typeContract = null; } else { typeContract = lcp.GetTypeContractFor(type); } } } catch (NullReferenceException) { docTracker.WriteLine("ERROR: NullReferenceException was thrown in CCI!"); typeContract = null; } return typeContract != null; }
/// <summary> /// Wraps a call to GetMethodContractFor inside of a try-catch statement. /// </summary> public static bool TryGetMethodContract(CodeContractAwareHostEnvironment host, IMethodReference method, out IMethodContract methodContract, DocTracker docTracker) { Contract.Requires(host != null); Contract.Requires(method != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out methodContract) != null); try { methodContract = ContractHelper.GetMethodContractFor(host, method.ResolvedMethod); } catch (NullReferenceException) { docTracker.WriteLine("ERROR: NullReferenceException was thrown in CCI!"); methodContract = null; } //} catch (Exception e) { // docTracker.WriteLine("ERROR: Exception of type '{0}' was thrown in CCI!", e.GetType().Name); // docTracker.WriteLine("\t'{0}'", e.Message); // methodContract = null; //} return methodContract != null; }
protected override void WriteEndElement(XmlReader reader, XmlWriter writer) { if (inMember && memberDepth == 0) { XContract[] contractArray = null; if (contracts.TryGetValue(currentMemberName, out contractArray)) { Contract.Assume(contractArray != null); foreach (var contractElement in contractArray) { Contract.Assume(contractElement != null); //Write contracts to the member contractElement.WriteTo(writer); //Add exception information to the member if relevent if (this.options.generateExceptionTable) { var contractWithException = contractElement as IContractWithException; if (contractWithException != null) { contractWithException.WriteExceptionTo(writer); } } } } inMember = false; } else if (inMember) { memberDepth--; } #region Write out all the contracts that haven't been written yet. else if (String.Equals(reader.LocalName, "members", StringComparison.Ordinal)) { foreach (var kv in contracts) { Contract.Assume(kv.Value != null); Contract.Assume(kv.Value.Length > 0); Contract.Assume(kv.Value[0] != null); if (kv.Value[0].wasWritten == false) { docTracker.WriteLine("Member does not exist. Adding..."); docTracker.WriteLine("{0}", kv.Key); writer.WriteStartElement("member"); writer.WriteAttributeString("name", kv.Key); foreach (var contract in kv.Value) { Contract.Assume(contract != null); contract.WriteTo(writer); if (this.options.generateExceptionTable) { var contractWithException = contract as IContractWithException; if (contractWithException != null) { contractWithException.WriteExceptionTo(writer); } } } writer.WriteEndElement(); } } } #endregion base.WriteEndElement(reader, writer); }
/// <summary> /// Write contracts from dictionary to XML file. /// </summary> static void WriteContracts(IDictionary<string, XContract[]> contracts, Options options, DocTracker docTracker) { Contract.Requires(options != null); Contract.Requires(docTracker != null); Contract.Requires(contracts != null); string xmlDocName = options.xmlFile; #region If an XML file isn't provided, create a new one. if (string.IsNullOrEmpty(xmlDocName) || !File.Exists(xmlDocName)) { docTracker.WriteLine("XML file not provided. Creating a new one."); #region Trim assembly name var trimmedAssemblyName = options.assembly; trimmedAssemblyName = Path.GetFileNameWithoutExtension(trimmedAssemblyName); #endregion if (string.IsNullOrEmpty(xmlDocName)) { xmlDocName = trimmedAssemblyName + ".xml"; } Contract.Assert(xmlDocName != null); #region Create new document XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null)); var docNode = doc.AppendChild(doc.CreateElement("doc")); var assemblyNode = docNode.AppendChild(doc.CreateElement("assembly")); var nameNode = assemblyNode.AppendChild(doc.CreateElement("name")); nameNode.AppendChild(doc.CreateTextNode(trimmedAssemblyName)); docNode.AppendChild(doc.CreateElement("members")); #endregion doc.Save(xmlDocName); } #endregion #region Traverse the XML file and create a new XML file with contracts var xmlDocTempName = xmlDocName + ".temp"; // xmlDocName may have a path, so don't prepend! using (var reader = XmlReader.Create(xmlDocName)) { docTracker.WriteLine("Reading {0}...", xmlDocName); var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineHandling = NewLineHandling.None; using (var writer = XmlWriter.Create(xmlDocTempName, settings)) { docTracker.WriteLine("Writing to {0} ...", xmlDocTempName); XmlTraverser xmlTraverser = new XmlTraverser(contracts, docTracker, options); xmlTraverser.Transform(reader, writer); writer.Flush(); } } #endregion #region Rename the output XML file if (String.IsNullOrEmpty(options.outputFile)) { File.Replace(xmlDocTempName, xmlDocName, xmlDocName + ".old"); } else { var outFile = options.outputFile; var ext = Path.GetExtension(outFile); if (ext != "xml") outFile = Path.ChangeExtension(outFile, ".xml"); if (File.Exists(outFile)) { File.Replace(xmlDocTempName, outFile, xmlDocName + ".old"); } else { File.Copy(xmlDocTempName, outFile); } } #endregion }
/// <summary> /// Write contracts from dictionary to XML file. /// </summary> static void WriteContracts(IDictionary <string, XContract[]> contracts, Options options, DocTracker docTracker) { Contract.Requires(options != null); Contract.Requires(docTracker != null); Contract.Requires(contracts != null); string xmlDocName = options.xmlFile; #region If an XML file isn't provided, create a new one. if (string.IsNullOrEmpty(xmlDocName) || !File.Exists(xmlDocName)) { docTracker.WriteLine("XML file not provided. Creating a new one."); #region Trim assembly name var trimmedAssemblyName = options.assembly; trimmedAssemblyName = Path.GetFileNameWithoutExtension(trimmedAssemblyName); #endregion if (string.IsNullOrEmpty(xmlDocName)) { xmlDocName = trimmedAssemblyName + ".xml"; } Contract.Assert(xmlDocName != null); #region Create new document XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null)); var docNode = doc.AppendChild(doc.CreateElement("doc")); var assemblyNode = docNode.AppendChild(doc.CreateElement("assembly")); var nameNode = assemblyNode.AppendChild(doc.CreateElement("name")); nameNode.AppendChild(doc.CreateTextNode(trimmedAssemblyName)); docNode.AppendChild(doc.CreateElement("members")); #endregion doc.Save(xmlDocName); } #endregion #region Traverse the XML file and create a new XML file with contracts var xmlDocTempName = xmlDocName + ".temp"; // xmlDocName may have a path, so don't prepend! using (var reader = XmlReader.Create(xmlDocName)) { docTracker.WriteLine("Reading {0}...", xmlDocName); var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineHandling = NewLineHandling.None; using (var writer = XmlWriter.Create(xmlDocTempName, settings)) { docTracker.WriteLine("Writing to {0} ...", xmlDocTempName); XmlTraverser xmlTraverser = new XmlTraverser(contracts, docTracker, options); xmlTraverser.Transform(reader, writer); writer.Flush(); } } #endregion #region Rename the output XML file if (String.IsNullOrEmpty(options.outputFile)) { File.Replace(xmlDocTempName, xmlDocName, xmlDocName + ".old"); } else { var outFile = options.outputFile; var ext = Path.GetExtension(outFile); if (ext != "xml") { outFile = Path.ChangeExtension(outFile, ".xml"); } if (File.Exists(outFile)) { File.Replace(xmlDocTempName, outFile, xmlDocName + ".old"); } else { File.Copy(xmlDocTempName, outFile); } } #endregion }