diff --git a/tools/generator/CodeGenerationOptions.cs b/tools/generator/CodeGenerationOptions.cs index 95319a95d..83e7d0ade 100644 --- a/tools/generator/CodeGenerationOptions.cs +++ b/tools/generator/CodeGenerationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -10,29 +11,6 @@ namespace MonoDroid.Generation { public class CodeGenerationOptions { - Stack context_types = new Stack (); - public Stack ContextTypes { - get { return context_types; } - } - public List ContextGeneratedMethods { get; set; } = new List (); - public GenBase ContextType { - get { return context_types.Any () ? context_types.Peek () : null; } - } - public Field ContextField { get; set; } - string ContextFieldString { - get { return ContextField != null ? "in field " + ContextField.Name + " " : null; } - } - public MethodBase ContextMethod { get; set; } - string ContextMethodString { - get { return ContextMethod != null ? "in method " + ContextMethod.Name + " " : null; } - } - string ContextTypeString { - get { return ContextType != null ? "in managed type " + ContextType.FullName : null; } - } - public string ContextString { - get { return ContextFieldString + ContextMethodString + ContextTypeString; } - } - CodeGenerationTarget codeGenerationTarget; public CodeGenerationTarget CodeGenerationTarget { get { return codeGenerationTarget; } @@ -134,20 +112,23 @@ public string GetSafeIdentifier (string name) return name.Replace ('$', '_'); } - Dictionary short_file_names = new Dictionary (); + readonly Dictionary short_file_names = new Dictionary (); public string GetFileName (string fullName) { if (!UseShortFileNames) return fullName; - string s; - if (short_file_names.TryGetValue (fullName, out s)) + + lock (short_file_names) { + if (short_file_names.TryGetValue (fullName, out var s)) + return s; + + s = short_file_names.Count.ToString (); + short_file_names [fullName] = s; + return s; - s = short_file_names.Count.ToString (); - short_file_names [fullName] = s; - return s; + } } } - } diff --git a/tools/generator/CodeGenerator.cs b/tools/generator/CodeGenerator.cs index d7d96b619..a5c47bd5a 100644 --- a/tools/generator/CodeGenerator.cs +++ b/tools/generator/CodeGenerator.cs @@ -140,7 +140,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve AddTypeToTable (opt, gen); } - Validate (gens, opt); + Validate (gens, opt, new CodeGeneratorContext ()); if (api_versions_xml != null) ApiVersionsSupport.AssignApiLevels (gens, api_versions_xml); @@ -175,6 +175,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve if (gen.IsGeneratable) gen.Generate (opt, gen_info); + ClassGen.GenerateTypeRegistrations (opt, gen_info); ClassGen.GenerateEnumList (gen_info); @@ -211,7 +212,7 @@ static IEnumerable FlattenNestedTypes (IEnumerable gens) } } - static void Validate (List gens, CodeGenerationOptions opt) + static void Validate (List gens, CodeGenerationOptions opt, CodeGeneratorContext context) { //int cycle = 1; List removed = new List (); @@ -229,7 +230,7 @@ static void Validate (List gens, CodeGenerationOptions opt) foreach (GenBase gen in gens) if ((opt.IgnoreNonPublicType && (gen.RawVisibility != "public" && gen.RawVisibility != "internal")) - || !gen.Validate (opt, null)) { + || !gen.Validate (opt, null, context)) { foreach (GenBase nest in gen.NestedTypes) { foreach (var nt in nest.Invalidate ()) removed.Add (nt); diff --git a/tools/generator/CodeGeneratorContext.cs b/tools/generator/CodeGeneratorContext.cs new file mode 100644 index 000000000..c91e58769 --- /dev/null +++ b/tools/generator/CodeGeneratorContext.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MonoDroid.Generation +{ + public class CodeGeneratorContext + { + public Stack ContextTypes { get; } = new Stack (); + public List ContextGeneratedMethods { get; set; } = new List (); + public Field ContextField { get; set; } + public MethodBase ContextMethod { get; set; } + + public GenBase ContextType => ContextTypes.Any () ? ContextTypes.Peek () : null; + string ContextFieldString => ContextField != null ? "in field " + ContextField.Name + " " : null; + string ContextMethodString => ContextMethod != null ? "in method " + ContextMethod.Name + " " : null; + string ContextTypeString => ContextType != null ? "in managed type " + ContextType.FullName : null; + public string ContextString => ContextFieldString + ContextMethodString + ContextTypeString; + } +} diff --git a/tools/generator/GenerationInfo.cs b/tools/generator/GenerationInfo.cs index 4e8fa0e78..75fcf64e3 100644 --- a/tools/generator/GenerationInfo.cs +++ b/tools/generator/GenerationInfo.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -13,77 +14,36 @@ public class GenerationInfo { public GenerationInfo (string csdir, string javadir, string assembly) { - this.csdir = csdir; - this.javadir = javadir; - this.assembly = assembly; + CSharpDir = csdir; + JavaDir = javadir; + Assembly = assembly; } - string assembly; - public string Assembly { - get { return assembly; } - } - - string csdir; - public string CSharpDir { - get { return csdir; } - } - - string javadir; - public string JavaDir { - get { return javadir; } - } - - string member; - public string CurrentMember { - get { return typename + "." + member; } - set { member = value; } - } - - string typename; - public string CurrentType { - get { return typename; } - set { typename = value; } - } - - StreamWriter sw; - public StreamWriter Writer { - get { return sw; } - set { sw = value; } - } - - List generated_files = new List (); - public IEnumerable GeneratedFiles { - get { return generated_files; } - } + public string Assembly { get; } + public string CSharpDir { get; } + public string JavaDir { get; } + public ConcurrentBag GeneratedFiles { get; } = new ConcurrentBag (); + public ConcurrentBag Enums { get; } = new ConcurrentBag (); + public ConcurrentBag> TypeRegistrations { get; } = new ConcurrentBag> (); public StreamWriter OpenStream (string name) { - if (!Directory.Exists(csdir)) - Directory.CreateDirectory (csdir); - string filename = Path.Combine (csdir, name + ".cs"); + if (!Directory.Exists (CSharpDir)) + Directory.CreateDirectory (CSharpDir); + string filename = Path.Combine (CSharpDir, name + ".cs"); - sw = new StreamWriter (File.Create (filename)); - generated_files.Add (filename); + var sw = new StreamWriter (File.Create (filename)); + GeneratedFiles.Add (filename); return sw; } - List enums = new List (); - public ICollection Enums { - get { return enums; } - } - - List> type_registrations = new List> (); - public ICollection> TypeRegistrations { - get { return type_registrations; } - } - internal void GenerateLibraryProjectFile (CodeGeneratorOptions options, IEnumerable enumFiles, string path = null) { if (path == null) { var name = Assembly ?? "GeneratedFiles"; int idx = name.IndexOf (','); name = idx < 0 ? name : name.Substring (0, idx); - path = Path.Combine (csdir, name + ".projitems"); + path = Path.Combine (CSharpDir, name + ".projitems"); } var msbuild = XNamespace.Get ("http://schemas.microsoft.com/developer/msbuild/2003"); diff --git a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs index 7c6e4d834..6f6e3cfc4 100644 --- a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs +++ b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; @@ -13,6 +14,8 @@ abstract class CodeGenerator protected TextWriter writer; protected CodeGenerationOptions opt; + public CodeGeneratorContext Context { get; } = new CodeGeneratorContext (); + protected CodeGenerator (TextWriter writer, CodeGenerationOptions options) { this.writer = writer; @@ -38,8 +41,8 @@ protected CodeGenerator (TextWriter writer, CodeGenerationOptions options) public void WriteClass (ClassGen @class, string indent, GenerationInfo gen_info) { - opt.ContextTypes.Push (@class); - opt.ContextGeneratedMethods = new List (); + Context.ContextTypes.Push (@class); + Context.ContextGeneratedMethods = new List (); gen_info.TypeRegistrations.Add (new KeyValuePair (@class.RawJniName, @class.AssemblyQualifiedName)); bool is_enum = @class.base_symbol != null && @class.base_symbol.FullName == "Java.Lang.Enum"; @@ -186,9 +189,9 @@ public void WriteClass (ClassGen @class, string indent, GenerationInfo gen_info) WriteClassInvoker (@class, indent); } - opt.ContextGeneratedMethods.Clear (); + Context.ContextGeneratedMethods.Clear (); - opt.ContextTypes.Pop (); + Context.ContextTypes.Pop (); } public void WriteClassAbstractMembers (ClassGen @class, string indent) @@ -289,7 +292,7 @@ public void WriteClassMethods (ClassGen @class, string indent) WriteMethodAbstractDeclaration (m, indent, null, @class); else WriteMethod (m, indent, @class, true); - opt.ContextGeneratedMethods.Add (m); + Context.ContextGeneratedMethods.Add (m); m.IsVirtual = virt; } @@ -393,7 +396,7 @@ public bool WriteFields (List fields, string indent, GenBase gen, HashSet Report.Warning (0, Report.WarningDuplicateField, "Skipping {0}.{1}, due to a duplicate field. (Field) (Java type: {2})", gen.FullName, f.Name, gen.JavaName); continue; } - if (f.Validate (opt, gen.TypeParameters)) { + if (f.Validate (opt, gen.TypeParameters, Context)) { if (seen != null) seen.Add (f.Name); needsProperty = needsProperty || f.NeedsProperty; @@ -440,7 +443,7 @@ internal virtual void WriteField (Field field, string indent, GenBase type) public void WriteInterface (InterfaceGen @interface, string indent, GenerationInfo gen_info) { - opt.ContextTypes.Push (@interface); + Context.ContextTypes.Push (@interface); // interfaces don't nest, so generate as siblings foreach (GenBase nest in @interface.NestedTypes) { @@ -465,7 +468,7 @@ public void WriteInterface (InterfaceGen @interface, string indent, GenerationIn WriteInterfaceExtensionsDeclaration (@interface, indent, null); WriteInterfaceInvoker (@interface, indent); WriteInterfaceEventHandler (@interface, indent); - opt.ContextTypes.Pop (); + Context.ContextTypes.Pop (); } // For each interface, generate either an abstract method or an explicit implementation method. @@ -474,7 +477,7 @@ public void WriteInterfaceAbstractMembers (InterfaceGen @interface, ClassGen gen foreach (Method m in @interface.Methods.Where (m => !m.IsInterfaceDefaultMethod && !m.IsStatic)) { bool mapped = false; string sig = m.GetSignature (); - if (opt.ContextGeneratedMethods.Any (_ => _.Name == m.Name && _.JniSignature == m.JniSignature)) + if (Context.ContextGeneratedMethods.Any (_ => _.Name == m.Name && _.JniSignature == m.JniSignature)) continue; for (var cls = gen; cls != null; cls = cls.BaseGen) if (cls.ContainsMethod (m, false) || cls != gen && gen.ExplicitlyImplementedInterfaceMethods.Contains (sig)) { @@ -487,7 +490,7 @@ public void WriteInterfaceAbstractMembers (InterfaceGen @interface, ClassGen gen WriteMethodExplicitInterfaceImplementation (m, indent, @interface); else WriteMethodAbstractDeclaration (m, indent, @interface, gen); - opt.ContextGeneratedMethods.Add (m); + Context.ContextGeneratedMethods.Add (m); } foreach (Property prop in @interface.Properties.Where (p => !p.Getter.IsStatic)) { if (gen.ContainsProperty (prop.Name, false)) @@ -777,7 +780,7 @@ public void WriteInterfaceInvoker (InterfaceGen @interface, string indent) writer.WriteLine (); writer.WriteLine ("{0}\tpublic {1}Invoker (IntPtr handle, JniHandleOwnership transfer) : base (Validate (handle), transfer)", indent, @interface.Name); writer.WriteLine ("{0}\t{{", indent); - writer.WriteLine ("{0}\t\tIntPtr local_ref = JNIEnv.GetObjectClass ({1});", indent, opt.ContextType.GetObjectHandleProperty ("this")); + writer.WriteLine ("{0}\t\tIntPtr local_ref = JNIEnv.GetObjectClass ({1});", indent, Context.ContextType.GetObjectHandleProperty ("this")); writer.WriteLine ("{0}\t\tthis.class_ref = JNIEnv.NewGlobalRef (local_ref);", indent); writer.WriteLine ("{0}\t\tJNIEnv.DeleteLocalRef (local_ref);", indent); writer.WriteLine ("{0}\t}}", indent); @@ -1176,7 +1179,7 @@ public void WriteMethodInvokerBody (Method method, string indent) WriteParameterListCallArgs (method.Parameters, indent, invoker: true); string env_method = "Call" + method.RetVal.CallMethodPrefix + "Method"; string call = "JNIEnv." + env_method + " (" + - opt.ContextType.GetObjectHandleProperty ("this") + ", " + method.EscapedIdName + method.Parameters.GetCallArgs (opt, invoker: true) + ")"; + Context.ContextType.GetObjectHandleProperty ("this") + ", " + method.EscapedIdName + method.Parameters.GetCallArgs (opt, invoker: true) + ")"; if (method.IsVoid) writer.WriteLine ("{0}{1};", indent, call); else diff --git a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/JavaInteropCodeGenerator.cs b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/JavaInteropCodeGenerator.cs index 3eccd634a..b9ad3ec38 100644 --- a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/JavaInteropCodeGenerator.cs +++ b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/JavaInteropCodeGenerator.cs @@ -120,7 +120,7 @@ internal override void WriteConstructorBody (Ctor ctor, string indent, System.Co ? "(" + ctor.Parameters.JniNestedDerivedSignature + ")V" : ctor.JniSignature); writer.WriteLine (); - writer.WriteLine ("{0}if ({1} != IntPtr.Zero)", indent, opt.ContextType.GetObjectHandleProperty ("this")); + writer.WriteLine ("{0}if ({1} != IntPtr.Zero)", indent, Context.ContextType.GetObjectHandleProperty ("this")); writer.WriteLine ("{0}\treturn;", indent); writer.WriteLine (); foreach (string prep in ctor.Parameters.GetCallPrep (opt)) diff --git a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/XamarinAndroidCodeGenerator.cs b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/XamarinAndroidCodeGenerator.cs index 6e0640672..6a750edda 100644 --- a/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/XamarinAndroidCodeGenerator.cs +++ b/tools/generator/Java.Interop.Tools.Generator.CodeGeneration/XamarinAndroidCodeGenerator.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.Design; using System.IO; namespace MonoDroid.Generation { @@ -65,7 +66,7 @@ internal override void WriteConstructorIdField (Ctor ctor, string indent) internal override void WriteConstructorBody (Ctor ctor, string indent, System.Collections.Specialized.StringCollection call_cleanup) { - writer.WriteLine ("{0}if ({1} != IntPtr.Zero)", indent, opt.ContextType.GetObjectHandleProperty ("this")); + writer.WriteLine ("{0}if ({1} != IntPtr.Zero)", indent, Context.ContextType.GetObjectHandleProperty ("this")); writer.WriteLine ("{0}\treturn;", indent); writer.WriteLine (); foreach (string prep in ctor.Parameters.GetCallPrep (opt)) @@ -83,7 +84,7 @@ internal override void WriteConstructorBody (Ctor ctor, string indent, System.Co writer.WriteLine ("{0}\t\t\tJniHandleOwnership.TransferLocalRef);", indent); writer.WriteLine ("{0}\tglobal::Android.Runtime.JNIEnv.FinishCreateInstance ({1}, \"{2}\"{3});", indent, - opt.ContextType.GetObjectHandleProperty ("this"), + Context.ContextType.GetObjectHandleProperty ("this"), ctor.IsNonStaticNestedType ? "(" + ctor.Parameters.JniNestedDerivedSignature + ")V" : ctor.JniSignature, ctor.Parameters.GetCallArgs (opt, invoker:false)); writer.WriteLine ("{0}\treturn;", indent); @@ -97,7 +98,7 @@ internal override void WriteConstructorBody (Ctor ctor, string indent, System.Co writer.WriteLine ("{0}\t\tJniHandleOwnership.TransferLocalRef);", indent); writer.WriteLine ("{0}JNIEnv.FinishCreateInstance ({1}, class_ref, {2}{3});", indent, - opt.ContextType.GetObjectHandleProperty ("this"), + Context.ContextType.GetObjectHandleProperty ("this"), ctor.ID, ctor.Parameters.GetCallArgs (opt, invoker:false)); indent = oldindent; diff --git a/tools/generator/Java.Interop.Tools.Generator.Importers/Xml/XmlCtor.cs b/tools/generator/Java.Interop.Tools.Generator.Importers/Xml/XmlCtor.cs index 94c0234ea..eb948c2cc 100644 --- a/tools/generator/Java.Interop.Tools.Generator.Importers/Xml/XmlCtor.cs +++ b/tools/generator/Java.Interop.Tools.Generator.Importers/Xml/XmlCtor.cs @@ -68,11 +68,11 @@ public override string Name { set { name = value; } } - protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList tps) + protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList tps, CodeGeneratorContext context) { if (missing_enclosing_class) return false; - return base.OnValidate (opt, tps); + return base.OnValidate (opt, tps, context); } public override string CustomAttributes { diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs index 98576600a..ef17d8655 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs @@ -2,15 +2,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -using System.Xml; - using Java.Interop.Tools.TypeNameMappings; - using Xamarin.Android.Binder; -using MonoDroid.Utils; - namespace MonoDroid.Generation { @@ -93,7 +87,7 @@ public override void ResetValidation () base.ResetValidation (); } - protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { if (validated) return is_valid; @@ -106,7 +100,7 @@ protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterD } // We're validating this in prior to BaseType. - if (TypeParameters != null && !TypeParameters.Validate (opt, type_params)) { + if (TypeParameters != null && !TypeParameters.Validate (opt, type_params, context)) { is_valid = false; return false; } @@ -124,7 +118,7 @@ protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterD return false; } - if ((base_symbol != null && !base_symbol.Validate (opt, TypeParameters)) || !base.OnValidate (opt, type_params)) { + if ((base_symbol != null && !base_symbol.Validate (opt, TypeParameters, context)) || !base.OnValidate (opt, type_params, context)) { Report.Warning (0, Report.WarningClassGen + 3, "Class {0} has invalid base type {1}.", FullName, BaseType); is_valid = false; return false; @@ -132,7 +126,7 @@ protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterD List valid_ctors = new List (); foreach (Ctor c in ctors) - if (c.Validate (opt, TypeParameters)) + if (c.Validate (opt, TypeParameters, context)) valid_ctors.Add (c); ctors = valid_ctors; @@ -244,113 +238,108 @@ public bool IsExplicitlyImplementedMethod (string sig) public override void Generate (CodeGenerationOptions opt, GenerationInfo gen_info) { - gen_info.CurrentType = FullName; + using (var sw = gen_info.OpenStream (opt.GetFileName (FullName))) { + sw.WriteLine ("using System;"); + sw.WriteLine ("using System.Collections.Generic;"); + sw.WriteLine ("using Android.Runtime;"); + if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid) { + sw.WriteLine ("using Java.Interop;"); + } + sw.WriteLine (); + sw.WriteLine ("namespace {0} {{", Namespace); + sw.WriteLine (); - StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName (FullName)); + var generator = opt.CreateCodeGenerator (sw); + generator.WriteClass (this, "\t", gen_info); - sw.WriteLine ("using System;"); - sw.WriteLine ("using System.Collections.Generic;"); - sw.WriteLine ("using Android.Runtime;"); - if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid) { - sw.WriteLine ("using Java.Interop;"); + sw.WriteLine ("}"); } - sw.WriteLine (); - sw.WriteLine ("namespace {0} {{", Namespace); - sw.WriteLine (); - - var generator = opt.CreateCodeGenerator (sw); - generator.WriteClass (this, "\t", gen_info); - - sw.WriteLine ("}"); - sw.Close (); - gen_info.Writer = null; } public static void GenerateTypeRegistrations (CodeGenerationOptions opt, GenerationInfo gen_info) { - StreamWriter sw = gen_info.Writer = gen_info.OpenStream (opt.GetFileName ("Java.Interop.__TypeRegistrations")); - - Dictionary>> mapping = new Dictionary>>(); - foreach (KeyValuePair reg in gen_info.TypeRegistrations) { - int ls = reg.Key.LastIndexOf ('/'); - string package = ls >= 0 ? reg.Key.Substring (0, ls) : ""; - - if (JavaNativeTypeManager.ToCliType (reg.Key) == reg.Value) - continue; - List> v; - if (!mapping.TryGetValue (package, out v)) - mapping.Add (package, v = new List>()); - v.Add (new KeyValuePair(reg.Key, reg.Value)); - } + using (var sw = gen_info.OpenStream (opt.GetFileName ("Java.Interop.__TypeRegistrations"))) { + + Dictionary>> mapping = new Dictionary>> (); + foreach (KeyValuePair reg in gen_info.TypeRegistrations.OrderBy (p => p.Key, StringComparer.OrdinalIgnoreCase)) { + int ls = reg.Key.LastIndexOf ('/'); + string package = ls >= 0 ? reg.Key.Substring (0, ls) : ""; + + if (JavaNativeTypeManager.ToCliType (reg.Key) == reg.Value) + continue; + List> v; + if (!mapping.TryGetValue (package, out v)) + mapping.Add (package, v = new List> ()); + v.Add (new KeyValuePair (reg.Key, reg.Value)); + } - sw.WriteLine ("using System;"); - sw.WriteLine ("using System.Collections.Generic;"); - sw.WriteLine ("using Android.Runtime;"); - sw.WriteLine (); - sw.WriteLine ("namespace Java.Interop {"); - sw.WriteLine (); - sw.WriteLine ("\tpartial class __TypeRegistrations {"); - sw.WriteLine (); - sw.WriteLine ("\t\tpublic static void RegisterPackages ()"); - sw.WriteLine ("\t\t{"); - sw.WriteLine ("#if MONODROID_TIMING"); - sw.WriteLine ("\t\t\tvar start = DateTime.Now;"); - sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages start: \" + (start - new DateTime (1970, 1, 1)).TotalMilliseconds);"); - sw.WriteLine ("#endif // def MONODROID_TIMING"); - sw.WriteLine ("\t\t\tJava.Interop.TypeManager.RegisterPackages ("); - sw.WriteLine ("\t\t\t\t\tnew string[]{"); - foreach (KeyValuePair>> e in mapping) { - sw.WriteLine ("\t\t\t\t\t\t\"{0}\",", e.Key); - } - sw.WriteLine ("\t\t\t\t\t},"); - sw.WriteLine ("\t\t\t\t\tnew Converter[]{"); - foreach (KeyValuePair>> e in mapping) { - sw.WriteLine ("\t\t\t\t\t\tlookup_{0}_package,", e.Key.Replace ('/', '_')); - } - sw.WriteLine ("\t\t\t\t\t});"); - sw.WriteLine ("#if MONODROID_TIMING"); - sw.WriteLine ("\t\t\tvar end = DateTime.Now;"); - sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages time: \" + (end - new DateTime (1970, 1, 1)).TotalMilliseconds + \" [elapsed: \" + (end - start).TotalMilliseconds + \" ms]\");"); - sw.WriteLine ("#endif // def MONODROID_TIMING"); - sw.WriteLine ("\t\t}"); - sw.WriteLine (); - sw.WriteLine ("\t\tstatic Type Lookup (string[] mappings, string javaType)"); - sw.WriteLine ("\t\t{"); - sw.WriteLine ("\t\t\tstring managedType = Java.Interop.TypeManager.LookupTypeMapping (mappings, javaType);"); - sw.WriteLine ("\t\t\tif (managedType == null)"); - sw.WriteLine ("\t\t\t\treturn null;"); - sw.WriteLine ("\t\t\treturn Type.GetType (managedType);"); - sw.WriteLine ("\t\t}"); - foreach (KeyValuePair>> map in mapping) { + sw.WriteLine ("using System;"); + sw.WriteLine ("using System.Collections.Generic;"); + sw.WriteLine ("using Android.Runtime;"); + sw.WriteLine (); + sw.WriteLine ("namespace Java.Interop {"); + sw.WriteLine (); + sw.WriteLine ("\tpartial class __TypeRegistrations {"); sw.WriteLine (); - string package = map.Key.Replace ('/', '_'); - sw.WriteLine ("\t\tstatic string[] package_{0}_mappings;", package); - sw.WriteLine ("\t\tstatic Type lookup_{0}_package (string klass)", package); + sw.WriteLine ("\t\tpublic static void RegisterPackages ()"); sw.WriteLine ("\t\t{"); - sw.WriteLine ("\t\t\tif (package_{0}_mappings == null) {{", package); - sw.WriteLine ("\t\t\t\tpackage_{0}_mappings = new string[]{{", package); - map.Value.Sort ((a, b) => a.Key.CompareTo (b.Key)); - foreach (KeyValuePair t in map.Value) { - sw.WriteLine ("\t\t\t\t\t\"{0}:{1}\",", t.Key, t.Value); + sw.WriteLine ("#if MONODROID_TIMING"); + sw.WriteLine ("\t\t\tvar start = DateTime.Now;"); + sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages start: \" + (start - new DateTime (1970, 1, 1)).TotalMilliseconds);"); + sw.WriteLine ("#endif // def MONODROID_TIMING"); + sw.WriteLine ("\t\t\tJava.Interop.TypeManager.RegisterPackages ("); + sw.WriteLine ("\t\t\t\t\tnew string[]{"); + foreach (KeyValuePair>> e in mapping) { + sw.WriteLine ("\t\t\t\t\t\t\"{0}\",", e.Key); } - sw.WriteLine ("\t\t\t\t};"); - sw.WriteLine ("\t\t\t}"); - sw.WriteLine (""); - sw.WriteLine ("\t\t\treturn Lookup (package_{0}_mappings, klass);", package); + sw.WriteLine ("\t\t\t\t\t},"); + sw.WriteLine ("\t\t\t\t\tnew Converter[]{"); + foreach (KeyValuePair>> e in mapping) { + sw.WriteLine ("\t\t\t\t\t\tlookup_{0}_package,", e.Key.Replace ('/', '_')); + } + sw.WriteLine ("\t\t\t\t\t});"); + sw.WriteLine ("#if MONODROID_TIMING"); + sw.WriteLine ("\t\t\tvar end = DateTime.Now;"); + sw.WriteLine ("\t\t\tAndroid.Util.Log.Info (\"MonoDroid-Timing\", \"RegisterPackages time: \" + (end - new DateTime (1970, 1, 1)).TotalMilliseconds + \" [elapsed: \" + (end - start).TotalMilliseconds + \" ms]\");"); + sw.WriteLine ("#endif // def MONODROID_TIMING"); + sw.WriteLine ("\t\t}"); + sw.WriteLine (); + sw.WriteLine ("\t\tstatic Type Lookup (string[] mappings, string javaType)"); + sw.WriteLine ("\t\t{"); + sw.WriteLine ("\t\t\tstring managedType = Java.Interop.TypeManager.LookupTypeMapping (mappings, javaType);"); + sw.WriteLine ("\t\t\tif (managedType == null)"); + sw.WriteLine ("\t\t\t\treturn null;"); + sw.WriteLine ("\t\t\treturn Type.GetType (managedType);"); sw.WriteLine ("\t\t}"); + foreach (KeyValuePair>> map in mapping) { + sw.WriteLine (); + string package = map.Key.Replace ('/', '_'); + sw.WriteLine ("\t\tstatic string[] package_{0}_mappings;", package); + sw.WriteLine ("\t\tstatic Type lookup_{0}_package (string klass)", package); + sw.WriteLine ("\t\t{"); + sw.WriteLine ("\t\t\tif (package_{0}_mappings == null) {{", package); + sw.WriteLine ("\t\t\t\tpackage_{0}_mappings = new string[]{{", package); + map.Value.Sort ((a, b) => a.Key.CompareTo (b.Key)); + foreach (KeyValuePair t in map.Value) { + sw.WriteLine ("\t\t\t\t\t\"{0}:{1}\",", t.Key, t.Value); + } + sw.WriteLine ("\t\t\t\t};"); + sw.WriteLine ("\t\t\t}"); + sw.WriteLine (""); + sw.WriteLine ("\t\t\treturn Lookup (package_{0}_mappings, klass);", package); + sw.WriteLine ("\t\t}"); + } + sw.WriteLine ("\t}"); + sw.WriteLine ("}"); } - sw.WriteLine ("\t}"); - sw.WriteLine ("}"); - sw.Close (); - gen_info.Writer = null; } public static void GenerateEnumList (GenerationInfo gen_info) { - StreamWriter sw = new StreamWriter (File.Create (Path.Combine (gen_info.CSharpDir, "enumlist"))); - foreach (string e in gen_info.Enums) - sw.WriteLine (e); - sw.Close (); + using (var sw = new StreamWriter (File.Create (Path.Combine (gen_info.CSharpDir, "enumlist")))) { + foreach (string e in gen_info.Enums.OrderBy (p => p, StringComparer.OrdinalIgnoreCase)) + sw.WriteLine (e); + } } protected override bool GetEnumMappedMemberInfo () diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Ctor.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Ctor.cs index 191c05ce4..acd36a433 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Ctor.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Ctor.cs @@ -24,9 +24,9 @@ public string ID { get { return "id_ctor" + IDSignature; } } - protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList tps) + protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList tps, CodeGeneratorContext context) { - if (!base.OnValidate (opt, tps)) + if (!base.OnValidate (opt, tps, context)) return false; jni_sig = "(" + Parameters.JniSignature + ")V"; return true; diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Field.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Field.cs index 07187908b..ad8a6cc56 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Field.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Field.cs @@ -83,21 +83,21 @@ internal ParameterList SetParameters { public string Annotation { get; internal set; } - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { symbol = opt.SymbolTable.Lookup (TypeName, type_params); - if (symbol == null || !symbol.Validate (opt, type_params)) { - Report.Warning (0, Report.WarningField + 0, "unexpected field type {0} {1}.", TypeName, opt.ContextString); + if (symbol == null || !symbol.Validate (opt, type_params, context)) { + Report.Warning (0, Report.WarningField + 0, "unexpected field type {0} {1}.", TypeName, context.ContextString); return false; } setParameters = new ParameterList () { SetterParameter, }; - if (!setParameters.Validate (opt, type_params)) + if (!setParameters.Validate (opt, type_params, context)) throw new NotSupportedException ( - string.Format ("Unable to generate setter parameter list {0}", opt.ContextString)); + string.Format ("Unable to generate setter parameter list {0}", context.ContextString)); return true; } diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs index 5eb4ff9ad..611622baa 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs @@ -413,9 +413,9 @@ public bool ContainsName (string name) return false; } - bool ValidateMethod (CodeGenerationOptions opt, Method m) + bool ValidateMethod (CodeGenerationOptions opt, Method m, CodeGeneratorContext context) { - if (!m.Validate (opt, TypeParameters)) { + if (!m.Validate (opt, TypeParameters, context)) { return false; } return true; @@ -623,17 +623,17 @@ void AdjustNestedTypeFullName (GenBase parent) nested.FullName = parent.FullName + "." + nested.Name; } - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { - opt.ContextTypes.Push (this); + context.ContextTypes.Push (this); try { - return is_valid = OnValidate (opt, type_params); + return is_valid = OnValidate (opt, type_params, context); } finally { - opt.ContextTypes.Pop (); + context.ContextTypes.Pop (); } } - protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { if (Name.Length > TypeNamePrefix.Length && (Name [TypeNamePrefix.Length] == '.' || Char.IsDigit (Name [TypeNamePrefix.Length]))) // see bug #5111 @@ -644,7 +644,7 @@ protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDe List valid_nests = new List (); foreach (GenBase gen in nested_types) { - if (gen.Validate (opt, TypeParameters)) + if (gen.Validate (opt, TypeParameters, context)) valid_nests.Add (gen); } nested_types = valid_nests; @@ -653,7 +653,7 @@ protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDe foreach (string iface_name in iface_names) { ISymbol isym = opt.SymbolTable.Lookup (iface_name); - if (isym != null && isym.Validate (opt, TypeParameters)) + if (isym != null && isym.Validate (opt, TypeParameters, context)) ifaces.Add (isym); else { if (isym == null) @@ -666,14 +666,14 @@ protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDe List valid_fields = new List (); foreach (Field f in fields) { - if (!f.Validate (opt, TypeParameters)) + if (!f.Validate (opt, TypeParameters, context)) continue; valid_fields.Add (f); } fields = valid_fields; int method_cnt = methods.Count; - methods = methods.Where (m => ValidateMethod (opt, m)).ToList (); + methods = methods.Where (m => ValidateMethod (opt, m, context)).ToList (); method_validation_failed = method_cnt != methods.Count; foreach (Method m in methods) { if (m.IsVirtual) @@ -869,28 +869,27 @@ protected void GenerateAnnotationAttribute (CodeGenerationOptions opt, Generatio var baseName = Namespace.Length > 0 ? FullName.Substring (Namespace.Length + 1) : FullName; var attrClassNameBase = baseName.Substring (TypeNamePrefix.Length) + "Attribute"; var localFullName = Namespace + (Namespace.Length > 0 ? "." : string.Empty) + attrClassNameBase; - gen_info.CurrentType = localFullName; - StreamWriter sw = gen_info.Writer = gen_info.OpenStream (opt.GetFileName (localFullName)); - sw.WriteLine ("using System;"); - sw.WriteLine (); - sw.WriteLine ("namespace {0} {{", Namespace); - sw.WriteLine (); - sw.WriteLine ("\t[global::Android.Runtime.Annotation (\"{0}\")]", JavaName); - sw.WriteLine ("\t{0} partial class {1} : Attribute", this.Visibility, attrClassNameBase); - sw.WriteLine ("\t{"); - - // An Annotation attribute property is generated for each applicable annotation method, - // where *applicable* means java annotation compatible types. See IsTypeCommensurate(). - foreach (var method in Methods.Where (m => m.Parameters.Count == 0 && - IsTypeCommensurate (opt, opt.SymbolTable.Lookup (m.RetVal.JavaName)))) { - sw.WriteLine ("\t\t[global::Android.Runtime.Register (\"{0}\"{1})]", method.JavaName, method.AdditionalAttributeString ()); - sw.WriteLine ("\t\tpublic {0} {1} {{ get; set; }}", opt.GetOutputName (method.RetVal.FullName), method.Name); + + using (var sw = gen_info.OpenStream (opt.GetFileName (localFullName))) { + sw.WriteLine ("using System;"); + sw.WriteLine (); + sw.WriteLine ("namespace {0} {{", Namespace); sw.WriteLine (); + sw.WriteLine ("\t[global::Android.Runtime.Annotation (\"{0}\")]", JavaName); + sw.WriteLine ("\t{0} partial class {1} : Attribute", this.Visibility, attrClassNameBase); + sw.WriteLine ("\t{"); + + // An Annotation attribute property is generated for each applicable annotation method, + // where *applicable* means java annotation compatible types. See IsTypeCommensurate(). + foreach (var method in Methods.Where (m => m.Parameters.Count == 0 && + IsTypeCommensurate (opt, opt.SymbolTable.Lookup (m.RetVal.JavaName)))) { + sw.WriteLine ("\t\t[global::Android.Runtime.Register (\"{0}\"{1})]", method.JavaName, method.AdditionalAttributeString ()); + sw.WriteLine ("\t\tpublic {0} {1} {{ get; set; }}", opt.GetOutputName (method.RetVal.FullName), method.Name); + sw.WriteLine (); + } + sw.WriteLine ("\t}"); + sw.WriteLine ("}"); } - sw.WriteLine ("\t}"); - sw.WriteLine ("}"); - sw.Close (); - gen_info.Writer = null; } } diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterDefinition.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterDefinition.cs index 9e40d39e7..f4912b563 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterDefinition.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterDefinition.cs @@ -28,7 +28,7 @@ public GenericParameterDefinition (string name, string [] constraints) bool validated, is_valid; - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { if (ConstraintExpressions == null || ConstraintExpressions.Length == 0) return true; @@ -38,7 +38,7 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList foreach (var c in ConstraintExpressions) { var sym = opt.SymbolTable.Lookup (c, type_params); if (sym == null) { - Report.Warning (0, Report.WarningGenericParameterDefinition + 0, "Unknown generic argument constraint type {0} {1}.", c, opt.ContextString); + Report.Warning (0, Report.WarningGenericParameterDefinition + 0, "Unknown generic argument constraint type {0} {1}.", c, context.ContextString); validated = true; return false; } @@ -114,10 +114,10 @@ public string GetSignature () return Count == 0 ? null : '<' + String.Join (",", (from p in this select p.Name).ToArray ()) + '>'; } - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { foreach (var pd in this) - if (!pd.Validate (opt, type_params)) + if (!pd.Validate (opt, type_params, context)) return false; return true; } diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterList.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterList.cs index 94eaa83ac..8d472ba81 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterList.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenericParameterList.cs @@ -64,7 +64,7 @@ public override string ToString () return managed; } - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList in_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList in_params, CodeGeneratorContext context) { if (validated) return is_valid; @@ -89,7 +89,7 @@ public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList } ISymbol psym = opt.SymbolTable.Lookup (tp, in_params); - if (psym == null || !psym.Validate (opt, in_params)) + if (psym == null || !psym.Validate (opt, in_params, context)) return false; diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/InterfaceGen.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/InterfaceGen.cs index 3e6dd4f27..e42193d9b 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/InterfaceGen.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/InterfaceGen.cs @@ -118,7 +118,7 @@ public override void ResetValidation () base.ResetValidation (); } - protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { if (validated) return is_valid; @@ -127,10 +127,10 @@ protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterD // Due to demand to validate in prior to validate ClassGen's BaseType, it is *not* done at // GenBase. - if (TypeParameters != null && !TypeParameters.Validate (opt, type_params)) + if (TypeParameters != null && !TypeParameters.Validate (opt, type_params, context)) return false; - if (!base.OnValidate (opt, type_params) || iface_validation_failed || MethodValidationFailed) { + if (!base.OnValidate (opt, type_params, context) || iface_validation_failed || MethodValidationFailed) { if (iface_validation_failed) Report.Warning (0, Report.WarningInterfaceGen + 2, "Invalidating {0} and all nested types because some of its interfaces were invalid.", FullName); else if (MethodValidationFailed) @@ -188,26 +188,22 @@ internal string GetArgsName (Method m) public override void Generate (CodeGenerationOptions opt, GenerationInfo gen_info) { - gen_info.CurrentType = FullName; + using (var sw = gen_info.OpenStream (opt.GetFileName (FullName))) { + sw.WriteLine ("using System;"); + sw.WriteLine ("using System.Collections.Generic;"); + sw.WriteLine ("using Android.Runtime;"); + if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid) { + sw.WriteLine ("using Java.Interop;"); + } + sw.WriteLine (); + sw.WriteLine ("namespace {0} {{", Namespace); + sw.WriteLine (); - StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName (FullName)); + var generator = opt.CreateCodeGenerator (sw); + generator.WriteInterface (this, "\t", gen_info); - sw.WriteLine ("using System;"); - sw.WriteLine ("using System.Collections.Generic;"); - sw.WriteLine ("using Android.Runtime;"); - if (opt.CodeGenerationTarget != CodeGenerationTarget.XamarinAndroid) { - sw.WriteLine ("using Java.Interop;"); + sw.WriteLine ("}"); } - sw.WriteLine (); - sw.WriteLine ("namespace {0} {{", Namespace); - sw.WriteLine (); - - var generator = opt.CreateCodeGenerator (sw); - generator.WriteInterface (this, "\t", gen_info); - - sw.WriteLine ("}"); - sw.Close (); - gen_info.Writer = null; GenerateAnnotationAttribute (opt, gen_info); } diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs index c11e3d2b3..6de5e867a 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs @@ -172,15 +172,15 @@ internal string CalculateEventName (Func checkNameDuplicate) return event_name; } - protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + protected override bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { if (GenericArguments != null) - GenericArguments.Validate (opt, type_params); + GenericArguments.Validate (opt, type_params, context); var tpl = GenericParameterDefinitionList.Merge (type_params, GenericArguments); - if (!retval.Validate (opt, tpl)) + if (!retval.Validate (opt, tpl, context)) return false; - return base.OnValidate (opt, tpl); + return base.OnValidate (opt, tpl, context); } string connector_name; diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/MethodBase.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/MethodBase.cs index 5e64862bc..40f456423 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/MethodBase.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/MethodBase.cs @@ -79,23 +79,23 @@ public virtual bool Matches (MethodBase other) return true; } - public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { - opt.ContextMethod = this; + context.ContextMethod = this; try { - return IsValid = OnValidate (opt, type_params); + return IsValid = OnValidate (opt, type_params, context); } finally { - opt.ContextMethod = null; + context.ContextMethod = null; } } - protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) + protected virtual bool OnValidate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context) { var tpl = GenericParameterDefinitionList.Merge (type_params, GenericArguments); - if (!parms.Validate (opt, tpl)) + if (!parms.Validate (opt, tpl, context)) return false; if (Parameters.Count > 14) { - Report.Warning (0, Report.WarningMethodBase + 0, "More than 16 parameters were found, which goes beyond the maximum number of parameters. ({0})", opt.ContextString); + Report.Warning (0, Report.WarningMethodBase + 0, "More than 16 parameters were found, which goes beyond the maximum number of parameters. ({0})", context.ContextString); return false; } return true; diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Parameter.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Parameter.cs index 6123523ad..f6c98e57c 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Parameter.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Parameter.cs @@ -252,15 +252,15 @@ public string GetGenericCall (CodeGenerationOptions opt, Dictionary mappings = null); - bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params); + bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params, CodeGeneratorContext context); string[] PreCallback (CodeGenerationOptions opt, string var_name, bool owned); string[] PostCallback (CodeGenerationOptions opt, string var_name); diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/StreamSymbol.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/StreamSymbol.cs index 474c1922b..655e45e52 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/StreamSymbol.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/StreamSymbol.cs @@ -74,7 +74,7 @@ public string ToNative (CodeGenerationOptions opt, string var_name, Dictionary (); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassInvokerMembers (@class, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassInvokerMembers)), writer.ToString ().NormalizeLineEndings ()); } @@ -119,9 +119,9 @@ public void WriteClassMethodInvokers () var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); var members = new HashSet (); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassMethodInvokers (@class, @class.Methods, string.Empty, members, null); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassMethodInvokers)), writer.ToString ().NormalizeLineEndings ()); } @@ -133,9 +133,9 @@ public void WriteClassMethodInvokersWithSkips () var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); var members = new HashSet (new [] { @class.Methods [0].Name }); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassMethodInvokers (@class, @class.Methods, string.Empty, members, null); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassMethodInvokersWithSkips)), writer.ToString ().NormalizeLineEndings ()); } @@ -145,9 +145,9 @@ public void WriteClassMethods () { var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassMethods (@class, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassMethods)), writer.ToString ().NormalizeLineEndings ()); } @@ -157,9 +157,9 @@ public void WriteClassProperties () { var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassProperties (@class, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassProperties)), writer.ToString ().NormalizeLineEndings ()); } @@ -171,9 +171,9 @@ public void WriteClassPropertyInvokers () var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); var members = new HashSet (); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassPropertyInvokers (@class, @class.Properties, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassPropertyInvokers)), writer.ToString ().NormalizeLineEndings ()); } @@ -185,9 +185,9 @@ public void WriteClassPropertyInvokersWithSkips () var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options); var members = new HashSet (new [] { @class.Properties [0].Name }); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteClassPropertyInvokers (@class, @class.Properties, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteClassPropertyInvokersWithSkips)), writer.ToString ().NormalizeLineEndings ()); } @@ -198,9 +198,9 @@ public void WriteCtor () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var ctor = new TestCtor (@class, "Object"); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteConstructor (ctor, string.Empty, true, @class); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteCtor)), writer.ToString ().NormalizeLineEndings ()); } @@ -214,9 +214,9 @@ public void WriteCtorDeprecated () .SetCustomAttributes ("[MyAttribute]") .SetAnnotation ("[global::Android.Runtime.IntDefinition (null, JniField = \"xamarin/test/SomeObject.SOME_VALUE\")]"); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteConstructor (ctor, string.Empty, true, @class); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteCtorDeprecated)), writer.ToString ().NormalizeLineEndings ()); } @@ -228,11 +228,11 @@ public void WriteCtorWithStringOverload () var ctor = new TestCtor (@class, "Object"); ctor.Parameters.Add (new Parameter ("mystring", "java.lang.CharSequence", "Java.Lang.ICharSequence", false)); - ctor.Validate (options, null); + ctor.Validate (options, null, new CodeGeneratorContext ()); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WriteConstructor (ctor, string.Empty, true, @class); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteCtorWithStringOverload)), writer.ToString ().NormalizeLineEndings ()); } @@ -242,7 +242,7 @@ public void WriteEnumifiedField () { var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("int", "bar").SetEnumified (); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); StringAssert.Contains ("[global::Android.Runtime.GeneratedEnum]", builder.ToString (), "Should contain GeneratedEnumAttribute!"); @@ -254,7 +254,7 @@ public void WriteDeprecatedField () var comment = "Don't use this!"; var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("int", "bar").SetConstant ("1234").SetDeprecated (comment); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); StringAssert.Contains ($"[Obsolete (\"{comment}\")]", builder.ToString (), "Should contain ObsoleteAttribute!"); @@ -265,7 +265,7 @@ public void WriteProtectedField () { var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("int", "bar").SetVisibility ("protected"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); StringAssert.Contains ("protected int bar {", builder.ToString (), "Property should be protected!"); @@ -277,7 +277,7 @@ public void WriteFieldConstant () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("java.lang.String", "bar").SetConstant (); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldConstant)), writer.ToString ().NormalizeLineEndings ()); @@ -289,7 +289,7 @@ public void WriteFieldConstantWithIntValue () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("int", "bar").SetConstant ("1234"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldConstantWithIntValue)), writer.ToString ().NormalizeLineEndings ()); @@ -301,7 +301,7 @@ public void WriteFieldConstantWithStringValue () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("java.lang.String", "bar").SetConstant ("\"hello\""); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldConstantWithStringValue)), writer.ToString ().NormalizeLineEndings ()); @@ -313,7 +313,7 @@ public void WriteFieldGetBody () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("java.lang.String", "bar"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteFieldGetBody (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldGetBody)), writer.ToString ().NormalizeLineEndings ()); @@ -335,7 +335,7 @@ public void WriteFieldInt () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("int", "bar"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldInt)), writer.ToString ().NormalizeLineEndings ()); @@ -347,7 +347,7 @@ public void WriteFieldSetBody () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("java.lang.String", "bar"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteFieldSetBody (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldSetBody)), writer.ToString ().NormalizeLineEndings ()); @@ -359,7 +359,7 @@ public void WriteFieldString () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var field = new TestField ("java.lang.String", "bar"); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); generator.WriteField (field, string.Empty, @class); Assert.AreEqual (GetTargetedExpected (nameof (WriteFieldString)), writer.ToString ().NormalizeLineEndings ()); @@ -371,7 +371,7 @@ public void WriteDeprecatedMethod () var comment = "Don't use this!"; var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetDeprecated (comment); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); StringAssert.Contains ($"[Obsolete (@\"{comment}\")]", builder.ToString (), "Should contain ObsoleteAttribute!"); @@ -382,7 +382,7 @@ public void WritedMethodWithManagedReturn () { var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar", @return: "int").SetManagedReturn ("long"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); StringAssert.Contains ("public virtual unsafe long bar ()", builder.ToString (), "Should contain return long!"); @@ -393,7 +393,7 @@ public void WritedMethodWithEnumifiedReturn () { var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar", @return: "int").SetManagedReturn ("int").SetReturnEnumified (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); StringAssert.Contains ("[return:global::Android.Runtime.GeneratedEnum]", builder.ToString (), "Should contain GeneratedEnumAttribute!"); @@ -405,9 +405,9 @@ public void WriteInterface () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var gen_info = new GenerationInfo (null, null, null); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterface (iface, string.Empty, gen_info); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteInterface)), writer.ToString ().NormalizeLineEndings ()); } @@ -417,9 +417,9 @@ public void WriteInterfaceDeclaration () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceDeclaration (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceDeclaration)), writer.ToString ().NormalizeLineEndings ()); } @@ -429,9 +429,9 @@ public void WriteInterfaceExtensionMethods () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceExtensionMethods (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceExtensionMethods)), writer.ToString ().NormalizeLineEndings ()); } @@ -441,9 +441,9 @@ public void WriteInterfaceEventArgs () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceEventArgs (iface, iface.Methods[0], string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceEventArgs)), writer.ToString ().NormalizeLineEndings ()); } @@ -453,9 +453,9 @@ public void WriteInterfaceEventHandler () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceEventHandler (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceEventHandler)), writer.ToString ().NormalizeLineEndings ()); } @@ -465,9 +465,9 @@ public void WriteInterfaceEventHandlerImpl () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceEventHandlerImpl (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceEventHandlerImpl)), writer.ToString ().NormalizeLineEndings ()); } @@ -478,9 +478,9 @@ public void WriteInterfaceEventHandlerImplContent () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var handlers = new List (); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceEventHandlerImplContent (iface, iface.Methods[0], string.Empty, true, string.Empty, handlers); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (1, handlers.Count); Assert.AreEqual ("GetCountForKey", handlers [0]); @@ -492,9 +492,9 @@ public void WriteInterfaceExtensionsDeclaration () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceExtensionsDeclaration (iface, string.Empty, "java.code.DeclaringType"); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceExtensionsDeclaration)), writer.ToString ().NormalizeLineEndings ()); } @@ -504,9 +504,9 @@ public void WriteInterfaceInvoker () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceInvoker (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteInterfaceInvoker)), writer.ToString ().NormalizeLineEndings ()); } @@ -516,9 +516,9 @@ public void WriteInterfaceListenerEvent () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceListenerEvent (iface, string.Empty, "MyName", "MyNameSpec", "MyMethodName", "MyFullDelegateName", true, "MyWrefSuffix", "Add", "Remove"); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceListenerEvent)), writer.ToString ().NormalizeLineEndings ()); } @@ -528,9 +528,9 @@ public void WriteInterfaceListenerProperty () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceListenerProperty (iface, string.Empty, "MyName", "MyNameSpec", "MyMethodName", "MyConnectorFmt", "MyFullDelegateName"); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceListenerProperty)), writer.ToString ().NormalizeLineEndings ()); } @@ -542,9 +542,9 @@ public void WriteInterfaceMethodInvokers () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var members = new HashSet (); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceMethodInvokers (iface, iface.Methods, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceMethodInvokers)), writer.ToString ().NormalizeLineEndings ()); } @@ -556,9 +556,9 @@ public void WriteInterfaceMethodInvokersWithSkips () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var members = new HashSet (new [] { iface.Methods [0].Name }); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceMethodInvokers (iface, iface.Methods, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceMethodInvokersWithSkips)), writer.ToString ().NormalizeLineEndings ()); } @@ -568,9 +568,9 @@ public void WriteInterfaceMethods () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceMethods (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceMethods)), writer.ToString ().NormalizeLineEndings ()); } @@ -580,9 +580,9 @@ public void WriteInterfaceProperties () { var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceProperties (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfaceProperties)), writer.ToString ().NormalizeLineEndings ()); } @@ -594,9 +594,9 @@ public void WriteInterfacePropertyInvokers () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var members = new HashSet (); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfacePropertyInvokers (iface, iface.Properties, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfacePropertyInvokers)), writer.ToString ().NormalizeLineEndings ()); } @@ -608,9 +608,9 @@ public void WriteInterfacePropertyInvokersWithSkips () var iface = SupportTypeBuilder.CreateInterface ("java.code.IMyInterface", options); var members = new HashSet (new [] { iface.Properties [0].Name }); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfacePropertyInvokers (iface, iface.Properties, string.Empty, members); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WriteInterfacePropertyInvokersWithSkips)), writer.ToString ().NormalizeLineEndings ()); } @@ -621,7 +621,7 @@ public void WriteMethodAbstractWithVoidReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetAbstract (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodAbstractWithVoidReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -633,7 +633,7 @@ public void WriteMethodAsyncifiedWithIntReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar", @return: "int").SetAsyncify (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodAsyncifiedWithIntReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -645,7 +645,7 @@ public void WriteMethodAsyncifiedWithVoidReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetAsyncify (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodAsyncifiedWithVoidReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -657,7 +657,7 @@ public void WriteMethodBody () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethodBody (method, string.Empty); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodBody)), writer.ToString ().NormalizeLineEndings ()); @@ -669,7 +669,7 @@ public void WriteMethodFinalWithVoidReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetFinal (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodFinalWithVoidReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -692,7 +692,7 @@ public void WriteMethodProtected () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetVisibility ("protected"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodProtected)), writer.ToString ().NormalizeLineEndings ()); @@ -704,7 +704,7 @@ public void WriteMethodStaticWithVoidReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar").SetStatic (); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodStaticWithVoidReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -716,7 +716,7 @@ public void WriteMethodWithIntReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar", @return: "int"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodWithIntReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -728,7 +728,7 @@ public void WriteMethodWithStringReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar", @return: "java.lang.String"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodWithStringReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -740,7 +740,7 @@ public void WriteMethodWithVoidReturn () var @class = new TestClass ("java.lang.Object", "com.mypackage.foo"); var method = new TestMethod (@class, "bar"); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); generator.WriteMethod (method, string.Empty, @class, true); Assert.AreEqual (GetTargetedExpected (nameof (WriteMethodWithVoidReturn)), writer.ToString ().NormalizeLineEndings ()); @@ -821,9 +821,9 @@ public void WritePropertyInvoker () { var @class = SupportTypeBuilder.CreateClassWithProperty ("java.lang.Object", "com.mypackage.foo", "MyProperty", "int", options); - options.ContextTypes.Push (@class); + generator.Context.ContextTypes.Push (@class); generator.WritePropertyInvoker (@class.Properties.First (), string.Empty, @class); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetExpected (nameof (WritePropertyInvoker)), writer.ToString ().NormalizeLineEndings ()); } diff --git a/tools/generator/Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs b/tools/generator/Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs index 6fd050802..5321110a2 100644 --- a/tools/generator/Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs +++ b/tools/generator/Tests/Unit-Tests/DefaultInterfaceMethodsTests.cs @@ -37,11 +37,11 @@ public void WriteInterfaceFields () iface.Fields.Add (new TestField ("int", "MyConstantField").SetConstant ().SetValue ("7")); iface.Methods.Add (new TestMethod (iface, "DoSomething").SetAbstract ()); - iface.Validate (options, new GenericParameterDefinitionList ()); + iface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceDeclaration (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteInterfaceFields)), writer.ToString ().NormalizeLineEndings ()); } @@ -62,11 +62,11 @@ public void WriteConstSugarInterfaceFields () iface.Fields.Add (new TestField ("int", "MyDeprecatedEnumField").SetConstant ().SetValue ("MyEnumValue").SetDeprecated ("This constant will be removed in the future version.")); iface.Fields.Add (new TestField ("int", "MyStaticField").SetStatic ().SetValue ("7")); - iface.Validate (options, new GenericParameterDefinitionList ()); + iface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()); - options.ContextTypes.Push (iface); + generator.Context.ContextTypes.Push (iface); generator.WriteInterfaceDeclaration (iface, string.Empty); - options.ContextTypes.Pop (); + generator.Context.ContextTypes.Pop (); Assert.AreEqual (GetTargetedExpected (nameof (WriteConstSugarInterfaceFields)), writer.ToString ().NormalizeLineEndings ()); } diff --git a/tools/generator/Tests/Unit-Tests/ManagedTests.cs b/tools/generator/Tests/Unit-Tests/ManagedTests.cs index 490d09225..9902cddde 100644 --- a/tools/generator/Tests/Unit-Tests/ManagedTests.cs +++ b/tools/generator/Tests/Unit-Tests/ManagedTests.cs @@ -61,7 +61,7 @@ public void SetUp () foreach (var type in module.Types.Where(t => t.IsClass && t.Namespace == "Java.Lang")) { var @class = new ManagedClassGen (type, options); - Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList ()), "@class.Validate failed!"); + Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext()), "@class.Validate failed!"); options.SymbolTable.AddType (@class); } } @@ -77,7 +77,7 @@ public void TearDown () public void Class () { var @class = new ManagedClassGen (module.GetType ("Com.Mypackage.Foo"), options); - Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList ()), "@class.Validate failed!"); + Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "@class.Validate failed!"); Assert.AreEqual ("public", @class.Visibility); Assert.AreEqual ("Foo", @class.Name); @@ -95,7 +95,7 @@ public void Method () var type = module.GetType ("Com.Mypackage.Foo"); var @class = new ManagedClassGen (type, options); var method = new ManagedMethod (@class, type.Methods.First (m => m.Name == "Bar")); - Assert.IsTrue (method.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); Assert.AreEqual ("public", method.Visibility); Assert.AreEqual ("void", method.Return); @@ -140,7 +140,7 @@ public void MethodWithParameters () var type = module.GetType ("Com.Mypackage.Foo"); var @class = new ManagedClassGen (type, options); var method = new ManagedMethod (@class, type.Methods.First (m => m.Name == "BarWithParams")); - Assert.IsTrue (method.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); Assert.AreEqual ("(ZID)Ljava/lang/String;", method.JniSignature); Assert.AreEqual ("java.lang.String", method.Return); Assert.AreEqual ("System.String", method.ManagedReturn); @@ -170,7 +170,7 @@ public void Ctor () var type = module.GetType ("Com.Mypackage.Foo"); var @class = new ManagedClassGen (type, options); var ctor = new ManagedCtor (@class, type.Methods.First (m => m.IsConstructor && !m.IsStatic)); - Assert.IsTrue (ctor.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList ()), "ctor.Validate failed!"); + Assert.IsTrue (ctor.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "ctor.Validate failed!"); Assert.AreEqual ("public", ctor.Visibility); Assert.AreEqual (".ctor", ctor.Name); @@ -184,7 +184,7 @@ public void Field () var type = module.GetType ("Com.Mypackage.Foo"); var @class = new ManagedClassGen (type, options); var field = new ManagedField (type.Fields.First (f => f.Name == "Value")); - Assert.IsTrue (field.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); Assert.AreEqual ("Value", field.Name); Assert.AreEqual ("value", field.JavaName); @@ -199,7 +199,7 @@ public void Interface () { var type = module.GetType ("Com.Mypackage.IService"); var @interface = new ManagedInterfaceGen (type, options); - Assert.IsTrue (@interface.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList ()), "interface.Validate failed!"); + Assert.IsTrue (@interface.Validate (new CodeGenerationOptions (), new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "interface.Validate failed!"); Assert.AreEqual ("public", @interface.Visibility); Assert.AreEqual ("IService", @interface.Name); diff --git a/tools/generator/Tests/Unit-Tests/SupportTypes.cs b/tools/generator/Tests/Unit-Tests/SupportTypes.cs index 16dd2198d..73d3a992b 100644 --- a/tools/generator/Tests/Unit-Tests/SupportTypes.cs +++ b/tools/generator/Tests/Unit-Tests/SupportTypes.cs @@ -353,7 +353,7 @@ public static TestCtor CreateConstructor (GenBase parent, string methodName, Cod foreach (var p in parameters) ctor.Parameters.Add (p); - ctor.Validate (options, null); + ctor.Validate (options, null, new CodeGeneratorContext ()); return ctor; } @@ -394,8 +394,8 @@ public static TestMethod CreateMethod (GenBase parent, string methodName, CodeGe foreach (var p in parameters) method.Parameters.Add (p); - method.Validate (options, null); - method.RetVal.Validate (options, null); + method.Validate (options, null, new CodeGeneratorContext ()); + method.RetVal.Validate (options, null, new CodeGeneratorContext ()); return method; } @@ -419,7 +419,7 @@ public static ParameterList CreateParameterList (CodeGenerationOptions options) }; foreach (var p in list) - p.Validate (options, null); + p.Validate (options, null, new CodeGeneratorContext ()); return list; } diff --git a/tools/generator/Tests/Unit-Tests/XmlTests.cs b/tools/generator/Tests/Unit-Tests/XmlTests.cs index 88b49356c..84465fb85 100644 --- a/tools/generator/Tests/Unit-Tests/XmlTests.cs +++ b/tools/generator/Tests/Unit-Tests/XmlTests.cs @@ -53,7 +53,7 @@ public void SetUp () var javaLang = xml.Element ("api").Element ("package"); foreach (var type in javaLang.Elements("class")) { var @class = new XmlClassGen (javaLang, type); - Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList ()), "@class.Validate failed!"); + Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "@class.Validate failed!"); options.SymbolTable.AddType (@class); } @@ -65,7 +65,7 @@ public void Class () { var element = package.Element ("class"); var @class = new XmlClassGen (package, element); - Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList ()), "@class.Validate failed!"); + Assert.IsTrue (@class.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "@class.Validate failed!"); Assert.AreEqual ("public", @class.Visibility); Assert.AreEqual ("Foo", @class.Name); @@ -83,7 +83,7 @@ public void Method () var element = package.Element ("class"); var @class = new XmlClassGen (package, element); var method = new XmlMethod (@class, element.Element ("method")); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); Assert.AreEqual ("public", method.Visibility); Assert.AreEqual ("void", method.Return); @@ -128,7 +128,7 @@ public void MethodWithParameters () var element = package.Element ("class"); var @class = new XmlClassGen (package, element); var method = new XmlMethod (@class, element.Elements ("method").Where (e => e.Attribute ("name").Value == "barWithParams").First ()); - Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList ()), "method.Validate failed!"); + Assert.IsTrue (method.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "method.Validate failed!"); Assert.AreEqual ("(ZID)Ljava/lang/String;", method.JniSignature); Assert.AreEqual ("java.lang.String", method.Return); Assert.AreEqual ("System.String", method.ManagedReturn); @@ -158,7 +158,7 @@ public void Ctor () var element = package.Element ("class"); var @class = new XmlClassGen (package, element); var ctor = new XmlCtor (@class, element.Element ("constructor")); - Assert.IsTrue (ctor.Validate (options, new GenericParameterDefinitionList ()), "ctor.Validate failed!"); + Assert.IsTrue (ctor.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "ctor.Validate failed!"); Assert.AreEqual ("public", ctor.Visibility); Assert.AreEqual ("foo", ctor.Name); @@ -172,7 +172,7 @@ public void Field () var element = package.Element ("class"); var @class = new XmlClassGen (package, element); var field = new XmlField (element.Element ("field")); - Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList ()), "field.Validate failed!"); + Assert.IsTrue (field.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "field.Validate failed!"); Assert.AreEqual ("Value", field.Name); Assert.AreEqual ("value", field.JavaName); @@ -187,7 +187,7 @@ public void Interface () { var element = package.Element ("interface"); var @interface = new XmlInterfaceGen (package, element); - Assert.IsTrue (@interface.Validate (options, new GenericParameterDefinitionList ()), "interface.Validate failed!"); + Assert.IsTrue (@interface.Validate (options, new GenericParameterDefinitionList (), new CodeGeneratorContext ()), "interface.Validate failed!"); Assert.AreEqual ("public", @interface.Visibility); Assert.AreEqual ("IService", @interface.Name); diff --git a/tools/generator/generator.csproj b/tools/generator/generator.csproj index f86b97dbc..d1e4e3791 100644 --- a/tools/generator/generator.csproj +++ b/tools/generator/generator.csproj @@ -58,6 +58,7 @@ utils\XmlExtensions.cs +