Skip to content

Commit 60c7870

Browse files
committed
Each hierarchy element is passed a GeneratorContext instance
This makes it possible to put java-to-managed name translation in the name translation provider, as previously intended, thus making the code programming language-independent
1 parent 534dade commit 60c7870

26 files changed

+192
-82
lines changed

src/Java.Interop.Bindings/Compiler/GeneratorContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ public GeneratorContext (FormattingCodeGenerator codeGenerator, Encoding fileEnc
7373
CodeGenerator = codeGenerator ?? throw new ArgumentNullException (nameof (codeGenerator));
7474
OutputPathProvider = new DefaultOutputPathProvider (this);
7575
FileEncoding = fileEncoding ?? Encoding.UTF8;
76-
HierarchyBuilder = new XamarinAndroidHierarchy ();
76+
HierarchyBuilder = new XamarinAndroidHierarchy (this);
77+
NameTranslationProvider = new XamarinNameTranslationProvider ();
7778
}
7879

7980
internal void AssertSaneEnvironment ()
8081
{
81-
/*
8282
if (NameTranslationProvider == null)
8383
throw new InvalidOperationException ("Name translation provider not defined");
84-
*/
8584

8685
if (OutputPathProvider == null)
8786
throw new InvalidOperationException ("Output path provider not defined");

src/Java.Interop.Bindings/Compiler/Hierarchy.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class Hierarchy : HierarchyBase
5151
public IList<HierarchyNamespace> Namespaces => namespaces;
5252
protected HierarchyIndex TypeIndex { get; } = new HierarchyIndex ();
5353

54+
public Hierarchy (GeneratorContext context) : base (context)
55+
{}
56+
5457
public void Build (IList<ApiElement> rawElements)
5558
{
5659
if (rawElements == null || rawElements.Count == 0)
@@ -550,29 +553,29 @@ protected virtual void Process (ApiEnum apiEnum)
550553
HierarchyBase ret = null;
551554

552555
if (type == typeof (HierarchyNamespace))
553-
ret = new HierarchyNamespace (parent as Hierarchy);
556+
ret = new HierarchyNamespace (Context, parent as Hierarchy);
554557
else if (type == typeof (HierarchyClass))
555-
ret = new HierarchyClass (parent as HierarchyNamespace);
558+
ret = new HierarchyClass (Context, parent as HierarchyNamespace);
556559
else if (type == typeof (HierarchyImplements))
557-
ret = new HierarchyImplements (parent as HierarchyObject);
560+
ret = new HierarchyImplements (Context, parent as HierarchyObject);
558561
else if (type == typeof (HierarchyMethod))
559-
ret = new HierarchyMethod (parent as HierarchyObject);
562+
ret = new HierarchyMethod (Context, parent as HierarchyObject);
560563
else if (type == typeof (HierarchyConstructor))
561-
ret = new HierarchyConstructor (parent as HierarchyObject);
564+
ret = new HierarchyConstructor (Context, parent as HierarchyObject);
562565
else if (type == typeof (HierarchyException))
563-
ret = new HierarchyException (parent as HierarchyMethod);
566+
ret = new HierarchyException (Context, parent as HierarchyMethod);
564567
else if (type == typeof (HierarchyTypeParameter))
565-
ret = new HierarchyTypeParameter (parent as HierarchyElement);
568+
ret = new HierarchyTypeParameter (Context, parent as HierarchyElement);
566569
else if (type == typeof (HierarchyTypeParameterGenericConstraint))
567-
ret = new HierarchyTypeParameterGenericConstraint (parent as HierarchyTypeParameter);
570+
ret = new HierarchyTypeParameterGenericConstraint (Context, parent as HierarchyTypeParameter);
568571
else if (type == typeof (HierarchyMethodParameter))
569-
ret = new HierarchyMethodParameter (parent as HierarchyMethod);
572+
ret = new HierarchyMethodParameter (Context, parent as HierarchyMethod);
570573
else if (type == typeof (HierarchyField))
571-
ret = new HierarchyField (parent as HierarchyObject);
574+
ret = new HierarchyField (Context, parent as HierarchyObject);
572575
else if (type == typeof (HierarchyInterface))
573-
ret = new HierarchyInterface (parent as HierarchyNamespace);
576+
ret = new HierarchyInterface (Context, parent as HierarchyNamespace);
574577
else if (type == typeof (HierarchyEnum))
575-
ret = new HierarchyEnum (parent as Hierarchy);
578+
ret = new HierarchyEnum (Context, parent as Hierarchy);
576579
else
577580
throw new InvalidOperationException ($"Unsupported hierarchy element type {type}");
578581

src/Java.Interop.Bindings/Compiler/HierarchyBase.cs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,63 +32,23 @@ namespace Java.Interop.Bindings.Compiler
3232
{
3333
public abstract class HierarchyBase
3434
{
35+
public GeneratorContext Context { get; }
36+
3537
// Intended for types that aren't in API.xml and yet are needed for other types to work, for instance
3638
// the IJavaObject interface (which is defined in Xamarin.Android's runtime)
3739
public bool IgnoreForCodeGeneration { get; set; }
3840

3941
// Whether the instance was created based on API description
4042
public bool IsBoundAPI { get; protected set; }
4143

42-
// If a Java name has a dot in it, preserve the dots when converting to managed name
43-
protected bool PreserveDotsInJavaNameTranslation { get; set; }
44-
45-
// This is a hack used by the old generator - it uppercases a symbol segment if it consists only of two
46-
// characters
47-
protected bool UpperCaseTwoLetterSegments { get; set; } = true;
48-
49-
public HierarchyBase ()
44+
protected HierarchyBase (GeneratorContext context)
5045
{
46+
Context = context ?? throw new ArgumentNullException (nameof (context));
5147
}
5248

5349
protected string JavaNameToManagedName (string javaName)
5450
{
55-
if (String.IsNullOrEmpty (javaName))
56-
throw new ArgumentException ("must not be null or empty", nameof (javaName));
57-
58-
if (javaName.Length == 1)
59-
return UpperFirst (javaName);
60-
61-
int dot = javaName.IndexOf ('.');
62-
if (dot >= 0) {
63-
var segments = new List <string> ();
64-
foreach (string s in javaName.Split ('.')) {
65-
if (UpperCaseTwoLetterSegments && s.Length == 2)
66-
segments.Add (s.ToUpper ());
67-
else
68-
segments.Add (UpperFirst (s));
69-
}
70-
javaName = String.Join (PreserveDotsInJavaNameTranslation ? "." : String.Empty, segments);
71-
} else if (javaName.Length == 2)
72-
javaName = javaName.ToUpper ();
73-
74-
return EnsureValidIdentifier (UpperFirst (javaName));
75-
76-
string UpperFirst (string s)
77-
{
78-
char first = Char.ToUpper (s [0]);
79-
if (s.Length > 1)
80-
return $"{first}{s.Substring (1)}";
81-
return first.ToString ();
82-
}
83-
}
84-
85-
protected virtual string EnsureValidIdentifier (string identifier)
86-
{
87-
if (String.IsNullOrEmpty (identifier))
88-
throw new InvalidOperationException ($".NET identifier must not be null or empty");
89-
90-
// TODO: implement full validation (check for VALID characters)
91-
return identifier;
51+
return Context.NameTranslationProvider.Translate (javaName);
9252
}
9353

9454
protected TApiElement EnsureApiElementType <TApiElement> (ApiElement apiElement) where TApiElement: ApiElement

src/Java.Interop.Bindings/Compiler/HierarchyClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class HierarchyClass : HierarchyObject
4040
public string ExtendsGenericAware { get; set; }
4141
public bool Obfuscated { get; set; }
4242

43-
public HierarchyClass (HierarchyElement parent) : base (parent)
43+
public HierarchyClass (GeneratorContext context, HierarchyElement parent) : base (context, parent)
4444
{
4545
}
4646

src/Java.Interop.Bindings/Compiler/HierarchyConstructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Java.Interop.Bindings.Compiler
2929
{
3030
public class HierarchyConstructor : HierarchyMethod
3131
{
32-
public HierarchyConstructor (HierarchyObject parent) : base (parent)
32+
public HierarchyConstructor (GeneratorContext context, HierarchyObject parent) : base (context, parent)
3333
{
3434
}
3535

src/Java.Interop.Bindings/Compiler/HierarchyElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class HierarchyElement : HierarchyBase//, IEquatable <HierarchyE
6464

6565
public bool UseGlobal { get; set; } = false;
6666

67-
protected HierarchyElement (HierarchyBase parent)
67+
protected HierarchyElement (GeneratorContext context, HierarchyBase parent) : base (context)
6868
{
6969
Parent = parent ?? throw new ArgumentNullException (nameof (parent));
7070
IsBoundAPI = false;
@@ -109,7 +109,7 @@ protected string GetManagedName ()
109109
{
110110
if (String.IsNullOrEmpty (ManagedName))
111111
return JavaNameToManagedName (Name);
112-
return EnsureValidIdentifier (ManagedName);
112+
return Context.NameTranslationProvider.EnsureValidIdentifier (ManagedName);
113113
}
114114

115115
protected void EnsureName ()

src/Java.Interop.Bindings/Compiler/HierarchyEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class HierarchyEnum : HierarchyObject
3535

3636
protected ApiEnum ApiEnum => apiEnum;
3737

38-
public HierarchyEnum (HierarchyBase parent) : base (parent)
38+
public HierarchyEnum (GeneratorContext context, HierarchyBase parent) : base (context, parent)
3939
{
4040
DoNotAddBaseTypes = true;
4141
}

src/Java.Interop.Bindings/Compiler/HierarchyException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class HierarchyException : HierarchyElement
3737
public string Type { get; set; }
3838
public string TypeGenericAware { get; set; }
3939

40-
public HierarchyException (HierarchyBase parent) : base (parent)
40+
public HierarchyException (GeneratorContext context, HierarchyBase parent) : base (context, parent)
4141
{
4242
}
4343

src/Java.Interop.Bindings/Compiler/HierarchyExternalTypeReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class HierarchyExternalTypeReference : HierarchyElement
3131
{
3232
public bool IsManaged { get; }
3333

34-
public HierarchyExternalTypeReference (HierarchyBase parent, string typeName, bool isManaged = false) : base (parent)
34+
public HierarchyExternalTypeReference (GeneratorContext context, HierarchyBase parent, string typeName, bool isManaged = false) : base (context, parent)
3535
{
3636
if (String.IsNullOrEmpty (nameof (typeName)))
3737
throw new ArgumentException ("must not be null or empty", nameof (typeName));

src/Java.Interop.Bindings/Compiler/HierarchyField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class HierarchyField : HierarchyTypeMember
4545
public string Value { get; set; }
4646
public bool Volatile { get; set; }
4747

48-
public HierarchyField (HierarchyObject parent) : base (parent)
48+
public HierarchyField (GeneratorContext context, HierarchyObject parent) : base (context, parent)
4949
{ }
5050

5151
public override void Init (ApiElement apiElement)

src/Java.Interop.Bindings/Compiler/HierarchyImplements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HierarchyImplements : HierarchyElement
3636
protected ApiImplements ApiImplements => apiImplements;
3737
public string JniType { get; set; }
3838

39-
public HierarchyImplements (HierarchyBase parent) : base (parent)
39+
public HierarchyImplements (GeneratorContext context, HierarchyBase parent) : base (context, parent)
4040
{
4141
}
4242

src/Java.Interop.Bindings/Compiler/HierarchyInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class HierarchyInterface : HierarchyObject
3232
public HierarchyInterfaceInvoker Invoker { get; set; }
3333
public bool InvokerNotNeeded { get; set; }
3434

35-
public HierarchyInterface (HierarchyElement parent) : base (parent)
35+
public HierarchyInterface (GeneratorContext context, HierarchyElement parent) : base (context, parent)
3636
{}
3737
}
3838
}

src/Java.Interop.Bindings/Compiler/HierarchyInterfaceInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class HierarchyInterfaceInvoker : HierarchyClass
3434
{
3535
HierarchyInterface InvokedInterface { get; }
3636

37-
public HierarchyInterfaceInvoker (HierarchyElement parent, HierarchyInterface invokedInterface) : base (parent)
37+
public HierarchyInterfaceInvoker (GeneratorContext context, HierarchyElement parent, HierarchyInterface invokedInterface) : base (context, parent)
3838
{
3939
InvokedInterface = invokedInterface ?? throw new ArgumentNullException (nameof (invokedInterface));
4040
}

src/Java.Interop.Bindings/Compiler/HierarchyMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class HierarchyMethod : HierarchyTypeMember
5050
public bool IsBridge { get; set; }
5151
public bool IsSynthetic { get; set; }
5252

53-
public HierarchyMethod (HierarchyObject parent) : base (parent)
53+
public HierarchyMethod (GeneratorContext context, HierarchyObject parent) : base (context, parent)
5454
{ }
5555

5656
public override void Init (ApiElement apiElement)

src/Java.Interop.Bindings/Compiler/HierarchyMethodParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HierarchyMethodParameter : HierarchyElement
3636
protected ApiMethodParameter ApiMethodParameter => apiMethodParameter;
3737
public string JniType { get; set; }
3838

39-
public HierarchyMethodParameter (HierarchyBase parent) : base (parent)
39+
public HierarchyMethodParameter (GeneratorContext context, HierarchyBase parent) : base (context, parent)
4040
{
4141
}
4242

src/Java.Interop.Bindings/Compiler/HierarchyNamespace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public class HierarchyNamespace : HierarchyElement
3636
protected ApiNameSpace ApiNameSpace => apiNameSpace;
3737
public string JniName { get; set; }
3838

39-
public HierarchyNamespace (Hierarchy parent) : base (parent)
39+
public HierarchyNamespace (GeneratorContext context, Hierarchy parent) : base (context, parent)
4040
{
41-
PreserveDotsInJavaNameTranslation = true;
41+
Context.NameTranslationProvider.PreserveDotsInJavaNameTranslation = true;
4242
}
4343

4444
public override void Init (ApiElement apiElement)

src/Java.Interop.Bindings/Compiler/HierarchyObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract class HierarchyObject : HierarchyElement
4747

4848
public IList<HierarchyObject> BaseTypes => baseTypes;
4949

50-
protected HierarchyObject (HierarchyBase parent) : base (parent)
50+
protected HierarchyObject (GeneratorContext context, HierarchyBase parent) : base (context, parent)
5151
{
5252
}
5353

src/Java.Interop.Bindings/Compiler/HierarchyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class HierarchyType : HierarchyBase
3333
public string ManagedName { get; internal set; }
3434
public HierarchyTypeKind Kind { get; }
3535

36-
public HierarchyType (string name, HierarchyTypeKind kind)
36+
public HierarchyType (GeneratorContext context, string name, HierarchyTypeKind kind) : base (context)
3737
{
3838
if (String.IsNullOrEmpty (name))
3939
throw new ArgumentException ("must not be null or empty", nameof (name));

src/Java.Interop.Bindings/Compiler/HierarchyTypeMember.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class HierarchyTypeMember : HierarchyElement
3838
public bool Static { get; set; }
3939
public ApiVisibility Visibility { get; set; }
4040

41-
public HierarchyTypeMember (HierarchyObject parent) : base (parent)
41+
public HierarchyTypeMember (GeneratorContext context, HierarchyObject parent) : base (context, parent)
4242
{
4343
}
4444

src/Java.Interop.Bindings/Compiler/HierarchyTypeParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class HierarchyTypeParameter : HierarchyElement
4242
// public string InterfaceBounds { get; set; }
4343
// public string JniInterfaceBounds { get; set; }
4444

45-
public HierarchyTypeParameter (HierarchyBase parent) : base (parent)
45+
public HierarchyTypeParameter (GeneratorContext context, HierarchyBase parent) : base (context, parent)
4646
{
4747
}
4848

src/Java.Interop.Bindings/Compiler/HierarchyTypeParameterGenericConstraint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class HierarchyTypeParameterGenericConstraint : HierarchyElement
3737

3838
public string Type { get; set; }
3939

40-
public HierarchyTypeParameterGenericConstraint (HierarchyBase parent) : base (parent)
40+
public HierarchyTypeParameterGenericConstraint (GeneratorContext context, HierarchyBase parent) : base (context, parent)
4141
{}
4242

4343
public override void Init (ApiElement apiElement)

src/Java.Interop.Bindings/Compiler/NameTranslationProvider.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// NameTranslator.cs
2+
// NameTranslationProvider.cs
33
//
44
// Author:
55
// Marek Habersack <[email protected]>
@@ -32,6 +32,9 @@ public abstract class NameTranslationProvider
3232
public string Name { get; }
3333
public Guid ID { get; }
3434

35+
// If a Java name has a dot in it, preserve the dots when converting to managed name
36+
public bool PreserveDotsInJavaNameTranslation { get; set; }
37+
3538
protected NameTranslationProvider (string name, Guid id)
3639
{
3740
if (String.IsNullOrEmpty (name))
@@ -43,6 +46,24 @@ protected NameTranslationProvider (string name, Guid id)
4346
ID = id;
4447
}
4548

49+
public virtual string EnsureValidIdentifier (string identifier)
50+
{
51+
if (String.IsNullOrEmpty (identifier))
52+
throw new InvalidOperationException ($".NET identifier must not be null or empty");
53+
54+
// TODO: implement full validation (check for VALID characters)
55+
return identifier;
56+
}
57+
58+
protected string UpperFirst (string s)
59+
{
60+
char first = Char.ToUpper (s [0]);
61+
if (s.Length > 1)
62+
return $"{first}{s.Substring (1)}";
63+
return first.ToString ();
64+
}
65+
66+
protected abstract string TranslateSegment (string segment);
4667
public abstract string Translate (string javaName);
4768
}
4869
}

0 commit comments

Comments
 (0)