Skip to content

Commit 440c05e

Browse files
authored
[generator] Refactor logic for applying [Obsolete] attributes (#1024)
Context: dotnet/android#7234 Refactor logic for applying `[Obsolete]` attributes into a single common method. This method will later be extended to add support for [`[ObsoletedOSPlatformAttribute]`][0]. Doing this piece first and separately allows us to verify that the refactor does not break anything, as the existing logic is tricky. A future PR will also remove the temporary hacks used to preserve stylistic compatibility with a `generator` refactor in 6bbb00a. [0]: dotnet/runtime#72970
1 parent 9b1d3ab commit 440c05e

13 files changed

+34
-26
lines changed

tools/generator/SourceWriters/BoundClass.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public BoundClass (ClassGen klass, CodeGenerationOptions opt, CodeGeneratorConte
4444
klass.JavadocInfo?.AddJavadocs (Comments);
4545
Comments.Add ($"// Metadata.xml XPath class reference: path=\"{klass.MetadataXPathReference}\"");
4646

47-
if (klass.IsDeprecated)
48-
Attributes.Add (new ObsoleteAttr (klass.DeprecatedComment) { WriteAttributeSuffix = true });
47+
SourceWriterExtensions.AddObsolete (Attributes, klass.DeprecatedComment, klass.IsDeprecated, writeAttributeSuffix: true);
4948

5049
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, klass, opt);
5150

tools/generator/SourceWriters/BoundConstructor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public BoundConstructor (ClassGen klass, Ctor constructor, bool useBase, CodeGen
3333
Attributes.Add (new RegisterAttr (".ctor", constructor.JniSignature, string.Empty, additionalProperties: constructor.AdditionalAttributeString ()));
3434
}
3535

36-
if (constructor.Deprecated != null)
37-
Attributes.Add (new ObsoleteAttr (constructor.Deprecated.Replace ("\"", "\"\"")));
36+
SourceWriterExtensions.AddObsolete (Attributes, constructor.Deprecated);
3837

3938
if (constructor.CustomAttributes != null)
4039
Attributes.Add (new CustomAttr (constructor.CustomAttributes));

tools/generator/SourceWriters/BoundField.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public BoundField (GenBase type, Field field, CodeGenerationOptions opt)
3030

3131
if (field.IsEnumified)
3232
Attributes.Add (new GeneratedEnumAttr ());
33-
if (field.IsDeprecated)
34-
Attributes.Add (new ObsoleteAttr (field.DeprecatedComment, field.IsDeprecatedError) { NoAtSign = true, WriteEmptyString = true });
33+
34+
SourceWriterExtensions.AddObsolete (Attributes, field.DeprecatedComment, field.IsDeprecated, noAtSign: true, writeEmptyString: true, isError: field.IsDeprecatedError);
35+
3536
if (field.Annotation.HasValue ())
3637
Attributes.Add (new CustomAttr (field.Annotation));
3738

tools/generator/SourceWriters/BoundFieldAsProperty.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public BoundFieldAsProperty (GenBase type, Field field, CodeGenerationOptions op
4747
Attributes.Add (new RegisterAttr (field.JavaName, additionalProperties: field.AdditionalAttributeString ()));
4848
}
4949

50-
if (field.IsDeprecated)
51-
Attributes.Add (new ObsoleteAttr (field.DeprecatedComment, field.IsDeprecatedError) { NoAtSign = true });
50+
SourceWriterExtensions.AddObsolete (Attributes, field.DeprecatedComment, field.IsDeprecated, noAtSign: true, isError: field.IsDeprecatedError);
5251

5352
SetVisibility (field.Visibility);
5453
UseExplicitPrivateKeyword = true;

tools/generator/SourceWriters/BoundInterface.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public BoundInterface (InterfaceGen iface, CodeGenerationOptions opt, CodeGenera
4343
iface.JavadocInfo?.AddJavadocs (Comments);
4444
Comments.Add ($"// Metadata.xml XPath interface reference: path=\"{iface.MetadataXPathReference}\"");
4545

46-
if (iface.IsDeprecated)
47-
Attributes.Add (new ObsoleteAttr (iface.DeprecatedComment) { WriteAttributeSuffix = true, WriteEmptyString = true });
46+
SourceWriterExtensions.AddObsolete (Attributes, iface.DeprecatedComment, iface.IsDeprecated, writeAttributeSuffix: true, writeEmptyString: true);
4847

4948
if (!iface.IsConstSugar (opt)) {
5049
var signature = string.IsNullOrWhiteSpace (iface.Namespace)

tools/generator/SourceWriters/BoundInterfaceMethodDeclaration.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public BoundInterfaceMethodDeclaration (Method method, string adapter, CodeGener
3333

3434
if (method.DeclaringType.IsGeneratable)
3535
Comments.Add ($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
36-
if (method.Deprecated != null)
37-
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"")));
36+
37+
SourceWriterExtensions.AddObsolete (Attributes, method.Deprecated);
38+
3839
if (method.IsReturnEnumified)
3940
Attributes.Add (new GeneratedEnumAttr (true));
4041
if (method.IsInterfaceDefaultMethod)

tools/generator/SourceWriters/BoundMethod.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public BoundMethod (GenBase type, Method method, CodeGenerationOptions opt, bool
7474
if (method.DeclaringType.IsGeneratable)
7575
Comments.Add ($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
7676

77-
if (method.Deprecated.HasValue ())
78-
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"")));
77+
SourceWriterExtensions.AddObsolete (Attributes, method.Deprecated);
7978

8079
if (method.IsReturnEnumified)
8180
Attributes.Add (new GeneratedEnumAttr (true));

tools/generator/SourceWriters/BoundMethodAbstractDeclaration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public BoundMethodAbstractDeclaration (GenBase gen, Method method, CodeGeneratio
5353
if (method.DeclaringType.IsGeneratable)
5454
Comments.Add ($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
5555

56-
if (method.Deprecated.HasValue ())
57-
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"")));
56+
SourceWriterExtensions.AddObsolete (Attributes, method.Deprecated);
5857

5958
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt);
6059

tools/generator/SourceWriters/BoundMethodExtensionStringOverload.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public BoundMethodExtensionStringOverload (Method method, CodeGenerationOptions
2626
SetVisibility (method.Visibility);
2727
ReturnType = new TypeReferenceWriter (opt.GetTypeReferenceName (method.RetVal).Replace ("Java.Lang.ICharSequence", "string").Replace ("global::string", "string"));
2828

29-
if (method.Deprecated != null)
30-
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"").Trim ()));
29+
SourceWriterExtensions.AddObsolete (Attributes, method.Deprecated);
3130

3231
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt);
3332

tools/generator/SourceWriters/BoundMethodStringOverload.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public BoundMethodStringOverload (Method method, CodeGenerationOptions opt)
2424
SetVisibility (method.Visibility);
2525
ReturnType = new TypeReferenceWriter (opt.GetTypeReferenceName (method.RetVal).Replace ("Java.Lang.ICharSequence", "string").Replace ("global::string", "string"));
2626

27-
if (method.Deprecated != null)
28-
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"").Trim ()));
27+
SourceWriterExtensions.AddObsolete (Attributes, method.Deprecated);
2928

3029
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt);
3130

0 commit comments

Comments
 (0)