Skip to content

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

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

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
return validIdentifier.Replace (normalizedIdentifier, "_");
4242
}
4343

44+
public static bool IsValidIdentifier (string identifier)
45+
{
46+
return !validIdentifier.IsMatch (identifier);
47+
}
48+
4449
// Makes uglier but unique identifiers by encoding each invalid character with its character value
4550
static string EncodeReplacement (Match match) => $"_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
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using Microsoft.CSharp;
6+
7+
using Java.Interop.Tools.JavaCallableWrappers;
8+
79
using Xamarin.Android.Binder;
810
using Xamarin.AndroidTools.AnnotationSupport;
911

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

100-
CSharpCodeProvider code_provider = new CSharpCodeProvider ();
101-
102102
public string GetSafeIdentifier (string name)
103103
{
104+
if (string.IsNullOrEmpty (name))
105+
return name;
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-
return name;
109+
switch (name) {
110+
case "partial": return name;
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
return name.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-
static string [] reserved_keywords = new [] {
14+
internal static string [] 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
<Project Sdk="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)