Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit 8e63cc8

Browse files
authored
[generator] generator --lang-features=do-not-fix-obsolete-overrides (#1143)
Fixes: #1142 Context: d0231c5 In d0231c5, we added a feature to automatically mark a method as "deprecated" if it overrides a "deprecated" method. However, there can be instances where this is undesirable for a user, and there is currently no way to opt out of this change on a global or `metadata` level. Add a global opt-out for this feature, usable via `generator --lang-features=do-not-fix-obsolete-overrides …`. This will eventually be made available to users via an MSBuild property in a separate xamarin/xamarin-android PR.
1 parent d7f41c4 commit 8e63cc8

5 files changed

Lines changed: 21 additions & 8 deletions

File tree

‎tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,12 @@ public void FixupDeprecatedBaseMethods ()
11381138

11391139
vargens=ParseApiDefinition(xml);
11401140

1141+
// Override method should not be marked deprecated because it's: deprecated='not deprecated'
1142+
Assert.IsNull(gens.Single(g =>g.Name=="MyClass").Methods.Single(m =>m.Name=="DoStuff").Deprecated);
1143+
1144+
options.FixObsoleteOverrides=true;
1145+
gens=ParseApiDefinition(xml);
1146+
11411147
// Override method should be marked deprecated because base method is
11421148
Assert.AreEqual("deprecated",gens.Single(g =>g.Name=="MyClass").Methods.Single(m =>m.Name=="DoStuff").Deprecated);
11431149
}
@@ -1158,6 +1164,7 @@ public void FixupDeprecatedSinceBaseMethods ()
11581164
</package>
11591165
</api>";
11601166

1167+
options.FixObsoleteOverrides=true;
11611168
vargens=ParseApiDefinition(xml);
11621169

11631170
// Override method should match base method's 'deprecated-since'

‎tools/generator/CodeGenerationOptions.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public SymbolTable SymbolTable {
6565
publicboolUseShallowReferencedTypes{get;set;}
6666
publicboolUseObsoletedOSPlatformAttributes{get;set;}
6767
publicboolUseRestrictToAttributes{get;set;}
68+
publicboolFixObsoleteOverrides{get;set;}
6869
publicboolRemoveConstSugar=>BuildingCoreAssembly;
6970

7071
bool?buildingCoreAssembly;

‎tools/generator/CodeGenerator.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
8585
SupportNullableReferenceTypes=options.SupportNullableReferenceTypes,
8686
UseObsoletedOSPlatformAttributes=options.UseObsoletedOSPlatformAttributes,
8787
UseRestrictToAttributes=options.UseRestrictToAttributes,
88+
FixObsoleteOverrides=options.FixObsoleteOverrides,
8889
};
8990
varresolverCache=newTypeDefinitionCache();
9091

‎tools/generator/CodeGeneratorOptions.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public CodeGeneratorOptions ()
5353
publicboolSupportNestedInterfaceTypes{get;set;}
5454
publicboolSupportNullableReferenceTypes{get;set;}
5555
publicboolUseRestrictToAttributes{get;set;}
56+
publicboolFixObsoleteOverrides{get;set;}=true;
5657
publicboolUseLegacyJavaResolver{get;set;}
5758
publicboolUseObsoletedOSPlatformAttributes{get;set;}
5859

@@ -104,14 +105,15 @@ public static CodeGeneratorOptions Parse (string[] args)
104105
"SDK Platform {VERSION}/API level.",
105106
v =>opts.ApiLevel=v},
106107
{"lang-features=",
107-
"For internal use. (Flags: interface-constants,default-interface-methods,nested-interface-types,nullable-reference-types,obsoleted-platform-attributes,restrict-to-attributes)",
108+
"For internal use. (Flags: interface-constants,default-interface-methods,nested-interface-types,nullable-reference-types,obsoleted-platform-attributes,restrict-to-attributes,do-not-fix-obsolete-overrides)",
108109
v =>{
109110
opts.SupportInterfaceConstants=v?.Contains("interface-constants")==true;
110111
opts.SupportDefaultInterfaceMethods=v?.Contains("default-interface-methods")==true;
111112
opts.SupportNestedInterfaceTypes=v?.Contains("nested-interface-types")==true;
112113
opts.SupportNullableReferenceTypes=v?.Contains("nullable-reference-types")==true;
113114
opts.UseObsoletedOSPlatformAttributes=v?.Contains("obsoleted-platform-attributes")==true;
114115
opts.UseRestrictToAttributes=v?.Contains("restrict-to-attributes")==true;
116+
opts.FixObsoleteOverrides=v?.Contains("do-not-fix-obsolete-overrides")==false;
115117
}},
116118
{"preserve-enums",
117119
"For internal use.",

‎tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,15 @@ public void FixupMethodOverrides (CodeGenerationOptions opt)
311311
if(bm!=null&&bm.RetVal.FullName==m.RetVal.FullName){// if return type is different, it could be still "new", not "override".
312312
m.IsOverride=true;
313313

314-
// If method overrides a deprecated method, it also needs to be marked as deprecated
315-
if(bm.Deprecated.HasValue()&&!m.Deprecated.HasValue())
316-
m.Deprecated=bm.Deprecated;
317-
318-
// Fix issue when base method was deprecated before the overriding method, set both both to base method value
319-
if(bm.DeprecatedSince.GetValueOrDefault(0)<m.DeprecatedSince.GetValueOrDefault(0))
320-
m.DeprecatedSince=bm.DeprecatedSince;
314+
if(opt.FixObsoleteOverrides){
315+
// If method overrides a deprecated method, it also needs to be marked as deprecated
316+
if(bm.Deprecated.HasValue()&&!m.Deprecated.HasValue())
317+
m.Deprecated=bm.Deprecated;
318+
319+
// Fix issue when base method was deprecated before the overriding method, set both both to base method value
320+
if(bm.DeprecatedSince.GetValueOrDefault(0)<m.DeprecatedSince.GetValueOrDefault(0))
321+
m.DeprecatedSince=bm.DeprecatedSince;
322+
}
321323

322324
break;
323325
}

0 commit comments

Comments
 (0)