Skip to content

Commit

Permalink
Implement ThrowIfDisposed()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rekkonnect committed Feb 27, 2024
1 parent 472e00a commit a9eca33
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/IDisposableGenerator/ClassItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal class ClassItems
public string? Name { get; set; }
public Accessibility Accessibility { get; set; }
public bool Stream { get; set; }
public bool ThrowIfDisposed { get; set; }
public List<string> Owns { get; } = [];
public List<string> Fields { get; } = [];
public List<string> SetNull { get; } = [];
Expand Down Expand Up @@ -46,6 +47,7 @@ public override string ToString()
_ = result.Append($"Class: Name {this.Name}")
.Append($", Accessibility: {this.Accessibility}")
.Append($", Stream: {this.Stream}")
.Append($", ThrowIfDisposed: {this.ThrowIfDisposed}")
.Append($", Owns Count: {this.Owns.Count}")
.Append($", Fields Count: {this.Fields.Count}")
.Append($", SetNull Count: {this.SetNull.Count}")
Expand Down
85 changes: 74 additions & 11 deletions src/IDisposableGenerator/DisposableCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,28 @@ End If
");
}

_ = sourceBuilder.Append(@" End Sub
End Class
");
_ = sourceBuilder.Append("""
End Sub
""");

if (classItem.ThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
Friend Sub ThrowIfDisposed()
If Me.isDisposed Then
Throw New ObjectDisposedException(NameOf({{classItem.Name}}))
End If
End Sub
""");
}

_ = sourceBuilder.Append("""
End Class
""");
}

_ = sourceBuilder.Append(@"End Namespace
Expand Down Expand Up @@ -209,9 +228,30 @@ namespace {workItem.Namespace};
");
}

_ = sourceBuilder.Append(@" }
}
");
_ = sourceBuilder.Append("""
}
""");

if (classItem.ThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof({{classItem.Name}}));
}
}
""");
}

_ = sourceBuilder.Append("""
}
""");
}

// inject the created sources into the users compilation.
Expand Down Expand Up @@ -319,13 +359,36 @@ namespace {workItem.Namespace}
");
}

_ = sourceBuilder.Append(@" }
}
");
_ = sourceBuilder.Append("""
}
""");

if (classItem.ThrowIfDisposed)
{
_ = sourceBuilder.Append($$"""
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException(nameof({{classItem.Name}}));
}
}
""");
}

_ = sourceBuilder.Append("""
}
""");
}

_ = sourceBuilder.Append(@"}
");
_ = sourceBuilder.Append("""
}
""");
}

// inject the created source into the users compilation.
Expand Down
165 changes: 137 additions & 28 deletions src/IDisposableGenerator/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -1,25 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AttributeCodeCSharp" xml:space="preserve">
<value>// &lt;autogenerated/&gt;
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AttributeCodeCSharp" xml:space="preserve">
<value>// &lt;autogenerated/&gt;
#pragma warning disable SA1636, 8618
namespace IDisposableGenerator
{
Expand Down Expand Up @@ -60,12 +159,17 @@ namespace IDisposableGenerator
{
}
}

// used only by a source generator to generate Dispose() and Dispose(bool).
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal class GenerateThrowIfDisposedAttribute : Attribute
{
}
}
#pragma warning restore SA1636, 8618
</value>
</data>
<data name="AttributeCodeVisualBasic" xml:space="preserve">
<value>' &lt;autogenerated/&gt;
#pragma warning restore SA1636, 8618</value>
</data>
<data name="AttributeCodeVisualBasic" xml:space="preserve">
<value>' &lt;autogenerated/&gt;
#Disable Warning SA1636
Imports System

Expand Down Expand Up @@ -101,8 +205,13 @@ Namespace IDisposableGenerator
Inherits Attribute
End Class

' used only by a source generator to generate Dispose() and Dispose(bool).
&lt;AttributeUsage(AttributeTargets.Class, Inherited:=False, AllowMultiple:=False)&gt;
Friend Class GenerateThrowIfDisposedAttribute
Inherits Attribute
End Class

End Namespace
#Enable Warning SA1636
</value>
</data>
#Enable Warning SA1636</value>
</data>
</root>
48 changes: 31 additions & 17 deletions src/IDisposableGenerator/WorkItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public void Process(INamedTypeSymbol testClass, CancellationToken ct)
}

ct.ThrowIfCancellationRequested();
var classItemsQuery =
from att in testClass.GetAttributes()
where att.AttributeClass!.Name.Equals(
"GenerateDisposeAttribute", StringComparison.Ordinal)
select GetClassItem(att, testClass);
var classItem = GetClassItem(testClass);

if (classItem is null)
{
return;
}

ct.ThrowIfCancellationRequested();
workItem!.Classes.Add(classItem);

var memberQuery =
from member in testClass.GetMembers()
select member;
foreach (var classItem in classItemsQuery)
{
ct.ThrowIfCancellationRequested();
workItem!.Classes.Add(classItem);
}

foreach (var member in memberQuery)
{
Expand All @@ -49,16 +49,30 @@ public List<WorkItem> GetWorkItems()
public int IndexOf(WorkItem item)
=> this.WorkItems.IndexOf(item);

private static ClassItems GetClassItem(AttributeData attr, INamedTypeSymbol testClass)
private static ClassItems? GetClassItem(INamedTypeSymbol testClass)
{
var result = new ClassItems
var result = new ClassItems();
var hasDisposalGeneration = false;

foreach (var attr in testClass.GetAttributes())
{
Name = testClass.Name,
Accessibility = testClass.DeclaredAccessibility,
Stream = (bool)attr.ConstructorArguments[0].Value!,
};
switch (attr.AttributeClass!.Name)
{
case "GenerateDisposeAttribute":
hasDisposalGeneration = true;
result.Name = testClass.Name;
result.Accessibility = testClass.DeclaredAccessibility;
result.Stream = (bool)attr.ConstructorArguments[0].Value!;
break;
case "GenerateThrowIfDisposedAttribute":
result.ThrowIfDisposed = true;
break;
default:

Check warning

Code scanning / Sonarscharp (reported by Codacy)

Remove this empty 'default' clause. Warning

Remove this empty 'default' clause.

Check notice on line 70 in src/IDisposableGenerator/WorkItemCollection.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/IDisposableGenerator/WorkItemCollection.cs#L70

Remove this empty 'default' clause.
break;
}
}

return result;
return !hasDisposalGeneration ? null : result;
}

private static void CheckAttributesOnMember(ISymbol member,
Expand Down
Loading

0 comments on commit a9eca33

Please sign in to comment.