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

Commit e7c5f54

Browse files
authored
[generator] Add netcoreapp3.1 support (#606)
The `netcoreapp3.1` target framework doesn't include `System.CodeDom`. Remove use of CSharpCodeGenerator.
1 parent 95f698b commit e7c5f54

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/IdentifierValidator.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public static string CreateValidIdentifier (string identifier, bool useEncodedRe
4141
returnvalidIdentifier.Replace(normalizedIdentifier,"_");
4242
}
4343

44+
publicstaticboolIsValidIdentifier(stringidentifier)
45+
{
46+
return!validIdentifier.IsMatch(identifier);
47+
}
48+
4449
// Makes uglier but unique identifiers by encoding each invalid character with its character value
4550
staticstringEncodeReplacement(Matchmatch)=>$"_x{(ushort)match.Value[0]}_";
4651
}

‎tools/generator/CodeGenerationOptions.cs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
usingSystem.Collections.Generic;
44
usingSystem.IO;
55
usingSystem.Linq;
6-
usingMicrosoft.CSharp;
6+
7+
usingJava.Interop.Tools.JavaCallableWrappers;
8+
79
usingXamarin.Android.Binder;
810
usingXamarin.AndroidTools.AnnotationSupport;
911

@@ -97,21 +99,29 @@ public string GetOutputName (string s)
9799
returnGetOutputName(s.Substring(0,idx))+'<'+String.Join(", ",typeParams.ToArray())+'>';
98100
}
99101

100-
CSharpCodeProvidercode_provider=newCSharpCodeProvider();
101-
102102
publicstringGetSafeIdentifier(stringname)
103103
{
104+
if(string.IsNullOrEmpty(name))
105+
returnname;
106+
104107
// NOTE: "partial" differs in behavior on macOS vs. Windows, Windows reports "partial" as a valid identifier
105108
// This check ensures the same output on both platforms
106-
if(name=="partial")
107-
returnname;
109+
switch(name){
110+
case"partial":returnname;
111+
// `this` isn't in TypeNameUtilities.reserved_keywords; special-case.
112+
case"this":return"this_";
113+
}
108114

109115
// In the ideal world, it should not be applied twice.
110116
// Sadly that is not true in reality, so we need to exclude non-symbols
111117
// when replacing the argument name with a valid identifier.
112118
// (ReturnValue.ToNative() takes an argument which could be either an expression or mere symbol.)
113-
if(name.LastOrDefault()!=')'&&!name.Contains('.'))
114-
name=code_provider.IsValidIdentifier(name)?name:name+'_';
119+
if(name[name.Length-1]!=')'&&!name.Contains('.')&&!name.StartsWith("@")){
120+
if(!IdentifierValidator.IsValidIdentifier(name)||
121+
Array.BinarySearch(TypeNameUtilities.reserved_keywords,name)>=0){
122+
name=name+"_";
123+
}
124+
}
115125
returnname.Replace('$','_');
116126
}
117127

‎tools/generator/Utilities/TypeNameUtilities.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class TypeNameUtilities
1111
{
1212
// These must be sorted for BinarySearch to work
1313
// Missing "this" because it's handled elsewhere as "this_"
14-
staticstring[]reserved_keywords=new[]{
14+
internalstaticstring[]reserved_keywords=new[]{
1515
"abstract","as","base","bool","break","byte","callback","case","catch","char","checked","class","const",
1616
"continue","decimal","default","delegate","do","double","else","enum","event","explicit","extern","false",
1717
"finally","fixed","float","for","foreach","goto","if","implicit","in","int","interface","internal",

‎tools/generator/generator.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<ProjectSdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net472</TargetFramework>
4+
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
55
<OutputType>Exe</OutputType>
66
<DefineConstants>$(DefineConstants);GENERATOR;HAVE_CECIL;JCW_ONLY_TYPE_NAMES</DefineConstants>
77
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>

0 commit comments

Comments
 (0)