forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generator] Use an intermediate C# model (dotnet#674)
`generator` is an amazingly powerful tool that has proven to be very versatile over the past decade. However, many of the remaining bugs fall into a category that is hard to fix given `generator`'s current design, which is: 1. Read in Java model 2. Make tweaks to Java model 3. Open `<TYPE>.cs` 4. Loop through Java model determining what C# to write The issue with this is that we are deciding **what** to generate on-the-fly to a write-only stream. This means that once we've written to the stream we cannot change it if future information suggests that we should. A good example is issue dotnet#461. The Java model is: dotnet#461 Given: // Java: interface AnimatorListener { onAnimationEnd (int p0); onAnimationEnd (int p0, p1); } Looping through this, we see we need to generate an `EventArgs` class for `AnimatorListener.onAnimationEnd(int p0)`, so we do: public partial class OnAnimationEndEventArgs : global::System.EventArgs { int p0; public OnAnimationEndEventArgs (int p0) { this.p0= p0; } public int P0 { get { return p0; } } } Then we get to the second method `AnimatorListener.onAnimationEnd(int p0, int p1)` and see that we need to add additional parameters to `OnAnimationEndEventArgs`. However we cannot modify the already written class, so we generate a second `OnAnimationEndEventArgs` with different parameters, which causes CS0101 compilation errors in C#. Another example is we can generate an empty `InterfaceConsts` class. This is because we write the class opening because we think we have members to place in it (`public static class InterfaceConsts {`), but as we loop through the members we thought we were going to add they all get eliminated. At that point all we can do is close the class with no members. The solution to both examples above is "simple": we need to first loop through the rest of the Java model and determine what we **actually** need to generate before we start writing anything. We can then merge the two `OnAnimationEndEventArgs` classes, or forgo writing an empty `InterfaceConsts` type. However, rather than adding this additional logic in each instance that needs it, there is enough benefit to convert the entire `generator` architecture to work by first building a C# model of what needs to be generated that can be tweaked before writing to a file, resulting in a design of: 1. Read in Java model 2. Make tweaks to Java model 3. ***Build C# model from Java model*** 4. ***Make tweaks to C# model*** 5. Open `<TYPE>.cs` 6. Loop through C# model, writing code Having a C# model to tweak should also help in a few other tricky cases we fail at today, like determining `override`s (dotnet#367, dotnet#586) and implementing `abstract` methods for `abstract` classes inheriting an `interface` (dotnet#470). Additionally, having distinct logic and source writing steps will make unit testing better. Currently, the only way to test the *logic* of if we are going to generate something is to write out the source code and compare it to "expected" source code. This means tiny fixes can have many "expected" files that need to change (dotnet#625). With separate steps, we can have a set of unit tests that test what code we are writing, and a set that tests the logic of determining what to generate. To test the above "2 `EventArgs` classes" example, we can test a fix by looking at the created C# model to ensure that only a single combined `OnAnimationEndEventArgs` class is created. We would not need to write out the generated source code and compare it to expected source code. To assist in this new design, add a new `Xamarin.SourceWriter.dll` assembly which is responsible for generating the C# source code. This eliminates a lot of hard to read and error prone code such as: writer.WriteLine ("{0}{1}{2}{3}{4} unsafe {5} {6}{7} ({8})", indent, visibility, static_arg, virt_ov, seal, ret, is_explicit ? GetDeclaringTypeOfExplicitInterfaceMethod (method.OverriddenInterfaceMethod) + '.' : string.Empty, method.AdjustedName, method.GetSignature (opt)); and replaces it with: var m = new MethodWriter { IsPublic = true, IsUnsafe = true, IsOverride = true, Name = method.AdjustedName }; m.Write (…); which will emit: public unsafe override void DoSomething () { } `Xamarin.SourceWriter.CodeWriter` is a wrapper around a `System.IO.Stream` that tracks the current indent level, rather than needing to pass `indent` around to every method. cw.WriteLine ("{"); cw.Indent (); // ... write block body ... cw.Unindent (); cw.WriteLine ("}"); ~~ Testing ~~ Many existing unit tests call directly into methods like `CodeGenerator.WriteProperties()`. These methods are no longer available when using `JavaInterop`/`XAJavaInterop`. These tests were either changed to use `CodeGenerator.WriteType()` or were moved to only run for `XamarinAndroid` codegen target. Tests were updated to ignore whitespace and line endings differences, as the refactor did not preserve 100% identical whitespace.
- Loading branch information
Showing
113 changed files
with
6,715 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class CodeWriter : IDisposable | ||
{ | ||
TextWriter stream; | ||
bool owns_stream; | ||
int indent; | ||
bool need_indent = true; | ||
string base_indent; | ||
|
||
public CodeWriter (string filename) | ||
{ | ||
stream = File.CreateText (filename); | ||
owns_stream = true; | ||
} | ||
|
||
public CodeWriter (TextWriter streamWriter, string baseIndent = "") | ||
{ | ||
stream = streamWriter; | ||
base_indent = baseIndent; | ||
} | ||
|
||
public void Write (string value) | ||
{ | ||
WriteIndent (); | ||
stream.Write (value); | ||
} | ||
|
||
public void WriteLine () | ||
{ | ||
WriteIndent (); | ||
stream.WriteLine (); | ||
need_indent = true; | ||
} | ||
|
||
public void WriteLine (string value) | ||
{ | ||
WriteIndent (); | ||
stream.WriteLine (value); | ||
need_indent = true; | ||
} | ||
|
||
public void WriteLine (string format, params object[] args) | ||
{ | ||
WriteIndent (); | ||
stream.WriteLine (format, args); | ||
need_indent = true; | ||
} | ||
|
||
public void Indent (int count = 1) => indent += count; | ||
public void Unindent (int count = 1) => indent -= count; | ||
|
||
private void WriteIndent () | ||
{ | ||
if (!need_indent) | ||
return; | ||
|
||
stream.Write (base_indent + new string ('\t', indent)); | ||
|
||
need_indent = false; | ||
} | ||
|
||
public void Dispose () | ||
{ | ||
if (owns_stream) | ||
stream?.Dispose (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public enum Visibility | ||
{ | ||
Default = 0, | ||
Private = 1, | ||
Public = 2, | ||
Protected = 4, | ||
Internal = 8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static bool HasValue (this string str) => !string.IsNullOrWhiteSpace (str); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public abstract class AttributeWriter | ||
{ | ||
public virtual void WriteAttribute (CodeWriter writer) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class ClassWriter : TypeWriter | ||
{ | ||
public ObservableCollection<ConstructorWriter> Constructors { get; } = new ObservableCollection<ConstructorWriter> (); | ||
|
||
public ClassWriter () | ||
{ | ||
Constructors.CollectionChanged += MemberAdded; | ||
} | ||
|
||
public override void WriteConstructors (CodeWriter writer) | ||
{ | ||
foreach (var ctor in Constructors) { | ||
ctor.Write (writer); | ||
writer.WriteLine (); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class CommentWriter : ISourceWriter | ||
{ | ||
public string Value { get; set; } | ||
public int Priority { get; set; } | ||
|
||
public CommentWriter (string value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
public virtual void Write (CodeWriter writer) | ||
{ | ||
writer.WriteLine (Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class ConstructorWriter : MethodWriter | ||
{ | ||
public string BaseCall { get; set; } | ||
|
||
protected override void WriteReturnType (CodeWriter writer) | ||
{ | ||
} | ||
|
||
protected override void WriteConstructorBaseCall (CodeWriter writer) | ||
{ | ||
if (BaseCall.HasValue ()) | ||
writer.Write ($" : {BaseCall}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Xamarin.SourceWriter | ||
{ | ||
public class DelegateWriter : ISourceWriter, ITakeParameters | ||
{ | ||
Visibility visibility; | ||
|
||
public string Name { get; set; } | ||
public List<MethodParameterWriter> Parameters { get; } = new List<MethodParameterWriter> (); | ||
public TypeReferenceWriter Type { get; set; } | ||
public List<string> Comments { get; } = new List<string> (); | ||
public List<AttributeWriter> Attributes { get; } = new List<AttributeWriter> (); | ||
public bool IsPublic { get => visibility.HasFlag (Visibility.Public); set => visibility = value ? Visibility.Public : Visibility.Default; } | ||
public bool UseExplicitPrivateKeyword { get; set; } | ||
public bool IsInternal { get => visibility.HasFlag (Visibility.Internal); set => visibility = value ? Visibility.Internal : Visibility.Default; } | ||
public bool IsConst { get; set; } | ||
public string Value { get; set; } | ||
public bool IsStatic { get; set; } | ||
public bool IsReadonly { get; set; } | ||
public bool IsPrivate { get => visibility.HasFlag (Visibility.Private); set => visibility = value ? Visibility.Private : Visibility.Default; } | ||
public bool IsProtected { get => visibility.HasFlag (Visibility.Protected); set => visibility = value ? Visibility.Protected : Visibility.Default; } | ||
public int Priority { get; set; } | ||
public bool IsShadow { get; set; } | ||
|
||
public void SetVisibility (string visibility) | ||
{ | ||
switch (visibility?.ToLowerInvariant ()) { | ||
case "public": | ||
IsPublic = true; | ||
break; | ||
case "internal": | ||
IsInternal = true; | ||
break; | ||
case "protected": | ||
IsProtected = true; | ||
break; | ||
case "private": | ||
IsPrivate = true; | ||
break; | ||
} | ||
} | ||
|
||
public virtual void Write (CodeWriter writer) | ||
{ | ||
WriteComments (writer); | ||
WriteAttributes (writer); | ||
WriteSignature (writer); | ||
} | ||
|
||
public virtual void WriteComments (CodeWriter writer) | ||
{ | ||
foreach (var c in Comments) | ||
writer.WriteLine (c); | ||
} | ||
|
||
public virtual void WriteAttributes (CodeWriter writer) | ||
{ | ||
foreach (var att in Attributes) | ||
att.WriteAttribute (writer); | ||
} | ||
|
||
public virtual void WriteSignature (CodeWriter writer) | ||
{ | ||
if (IsPublic) | ||
writer.Write ("public "); | ||
else if (IsInternal) | ||
writer.Write ("internal "); | ||
else if (IsPrivate) | ||
writer.Write ("private "); | ||
|
||
if (IsStatic) | ||
writer.Write ("static "); | ||
if (IsReadonly) | ||
writer.Write ("readonly "); | ||
if (IsConst) | ||
writer.Write ("const "); | ||
|
||
if (IsShadow) | ||
writer.Write ("new "); | ||
|
||
writer.Write ("delegate "); | ||
|
||
WriteType (writer); | ||
|
||
writer.Write (Name + " "); | ||
writer.Write ("("); | ||
|
||
WriteParameters (writer); | ||
|
||
writer.Write (")"); | ||
|
||
writer.Write (";"); | ||
} | ||
|
||
protected virtual void WriteParameters (CodeWriter writer) | ||
{ | ||
for (var i = 0; i < Parameters.Count; i++) { | ||
var p = Parameters [i]; | ||
p.WriteParameter (writer); | ||
|
||
if (i < Parameters.Count - 1) | ||
writer.Write (", "); | ||
} | ||
} | ||
|
||
protected virtual void WriteType (CodeWriter writer) | ||
{ | ||
Type.WriteTypeReference (writer); | ||
} | ||
} | ||
} |
Oops, something went wrong.