You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dk...@apache.org on 2018/12/10 18:31:51 UTC

[avro] branch master updated (6eda0b4 -> b9409b3)

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git.


    from 6eda0b4  fix avrogen.exe output dir path format using Path API
     new eaaf263  [AVRO-1952] Squashed commit of the following: This closes #134
     new b9409b3  Cleanup csharp tests a bit

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/apache/main/Specific/ObjectCreator.cs      | 43 +---------------------
 .../apache/main/Specific/SpecificDatumReader.cs    | 34 +++++++----------
 .../src/apache/main/Specific/SpecificReader.cs     |  8 +---
 .../src/apache/test/Specific/SpecificTests.cs      | 26 ++++++-------
 4 files changed, 30 insertions(+), 81 deletions(-)


[avro] 01/02: [AVRO-1952] Squashed commit of the following: This closes #134

Posted by dk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git

commit eaaf263372229f9aad9970f54767e043b1e104dc
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Mon Dec 10 13:05:59 2018 -0500

    [AVRO-1952] Squashed commit of the following:
    This closes #134
    
    commit 0cf2081569f3244ae535710d46ea41e15e57bbcc
    Author: dolow <do...@hotmail.com>
    Date:   Wed Sep 28 20:10:17 2016 +0900
    
        removed unused GetConstructor methods
    
    commit aad24a7b1bbeb39105b8b3ee9578dbc792d8cfea
    Author: dolow <do...@hotmail.com>
    Date:   Wed Sep 28 17:45:43 2016 +0900
    
        replaced Reflection.Emit to simple instantiation for supporting AOT.
---
 .../src/apache/main/Specific/ObjectCreator.cs      | 43 +---------------------
 .../apache/main/Specific/SpecificDatumReader.cs    | 34 +++++++----------
 2 files changed, 16 insertions(+), 61 deletions(-)

diff --git a/lang/csharp/src/apache/main/Specific/ObjectCreator.cs b/lang/csharp/src/apache/main/Specific/ObjectCreator.cs
index ae43c84..d1daa95 100644
--- a/lang/csharp/src/apache/main/Specific/ObjectCreator.cs
+++ b/lang/csharp/src/apache/main/Specific/ObjectCreator.cs
@@ -1,4 +1,4 @@
-/**
+/**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -49,8 +49,6 @@ namespace Avro.Specific
         private readonly bool diffAssembly;
 
         public delegate object CtorDelegate();
-        private Type ctorType = typeof(CtorDelegate);
-        Dictionary<NameCtorKey, CtorDelegate> ctors;
 
         private ObjectCreator()
         {
@@ -61,8 +59,6 @@ namespace Avro.Specific
 
             GenericMapType = typeof(Dictionary<,>);
             GenericListType = typeof(List<>);
-
-            ctors = new Dictionary<NameCtorKey, CtorDelegate>();
         }
 
         public struct NameCtorKey : IEquatable<NameCtorKey>
@@ -273,28 +269,6 @@ namespace Avro.Specific
         }
 
         /// <summary>
-        /// Gets the default constructor for the specified type
-        /// </summary>
-        /// <param name="name">name of object for the type</param>
-        /// <param name="schemaType">schema type for the object</param>
-        /// <param name="type">type of the object</param>
-        /// <returns>Default constructor for the type</returns>
-        public CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type)
-        {
-            ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes);
-            if (ctorInfo == null)
-                throw new AvroException("Class " + name + " has no default constructor");
-
-            DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object), null, type, true);
-            ILGenerator ilGen = dynMethod.GetILGenerator();
-            ilGen.Emit(OpCodes.Nop);
-            ilGen.Emit(OpCodes.Newobj, ctorInfo);
-            ilGen.Emit(OpCodes.Ret);
-
-            return (CtorDelegate)dynMethod.CreateDelegate(ctorType);
-        }
-
-        /// <summary>
         /// Creates new instance of the given type
         /// </summary>
         /// <param name="name">fully qualified name of the type</param>
@@ -302,20 +276,7 @@ namespace Avro.Specific
         /// <returns>new object of the given type</returns>
         public object New(string name, Schema.Type schemaType)
         {
-            NameCtorKey key = new NameCtorKey(name, schemaType);
-
-            CtorDelegate ctor;
-            lock(ctors)
-            {
-                if (!ctors.TryGetValue(key, out ctor))
-                {
-                    Type type = GetType(name, schemaType);
-                    ctor = GetConstructor(name, schemaType, type);
-
-                    ctors.Add(key, ctor);
-                }
-            }
-            return ctor();
+            return Activator.CreateInstance(GetType(name, schemaType));
         }
     }
 }
diff --git a/lang/csharp/src/apache/main/Specific/SpecificDatumReader.cs b/lang/csharp/src/apache/main/Specific/SpecificDatumReader.cs
index d54f3ca..3ec8b3e 100644
--- a/lang/csharp/src/apache/main/Specific/SpecificDatumReader.cs
+++ b/lang/csharp/src/apache/main/Specific/SpecificDatumReader.cs
@@ -1,4 +1,4 @@
-/**
+/**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -77,12 +77,6 @@ namespace Avro.Specific
             return new SpecificFixedAccess(readerSchema);
         }
 
-        private static ObjectCreator.CtorDelegate GetConstructor(string name, Schema.Type schemaType)
-        {
-            var creator = ObjectCreator.Instance;
-            return creator.GetConstructor(name, schemaType, creator.GetType(name, schemaType));
-        }
-
         private class SpecificEnumAccess : EnumAccess
         {
             public object CreateEnum(object reuse, int ordinal)
@@ -93,16 +87,16 @@ namespace Avro.Specific
 
         private class SpecificRecordAccess : RecordAccess
         {
-            private ObjectCreator.CtorDelegate objCreator;
+            private string typeName;
 
             public SpecificRecordAccess(RecordSchema readerSchema)
             {
-                objCreator = GetConstructor(readerSchema.Fullname, Schema.Type.Record);
+                typeName = readerSchema.Fullname;
             }
 
             public object CreateRecord(object reuse)
             {
-                return reuse ?? objCreator();
+                return reuse ?? ObjectCreator.Instance.New(typeName, Schema.Type.Record);
             }
 
             public object GetField(object record, string fieldName, int fieldPos)
@@ -118,16 +112,16 @@ namespace Avro.Specific
 
         private class SpecificFixedAccess : FixedAccess
         {
-            private ObjectCreator.CtorDelegate objCreator;
+            private string typeName;
 
             public SpecificFixedAccess(FixedSchema readerSchema)
             {
-                objCreator = GetConstructor(readerSchema.Fullname, Schema.Type.Fixed);
+                typeName = readerSchema.Fullname;
             }
 
             public object CreateFixed(object reuse)
             {
-                return reuse ?? objCreator();
+                return reuse ?? ObjectCreator.Instance.New(typeName, Schema.Type.Fixed);
             }
 
             public byte[] GetFixedBuffer(object rec)
@@ -138,7 +132,7 @@ namespace Avro.Specific
 
         private class SpecificArrayAccess : ArrayAccess
         {
-            private ObjectCreator.CtorDelegate objCreator;
+            private string typeName;
 
             public SpecificArrayAccess(ArraySchema readerSchema)
             {
@@ -147,7 +141,7 @@ namespace Avro.Specific
                 type = type.Remove(0, 6);              // remove IList<
                 type = type.Remove(type.Length - 1);   // remove >
 
-                objCreator = GetConstructor(type, Schema.Type.Array);
+                typeName = type;
             }
 
             public object Create(object reuse)
@@ -164,7 +158,7 @@ namespace Avro.Specific
                     array.Clear();
                 }
                 else
-                    array = objCreator() as IList;
+                    array = ObjectCreator.Instance.New(typeName, Schema.Type.Array) as IList;
                 return array;
             }
 
@@ -190,7 +184,7 @@ namespace Avro.Specific
 
         private class SpecificMapAccess : MapAccess
         {
-            private ObjectCreator.CtorDelegate objCreator;
+            private string typeName;
 
             public SpecificMapAccess(MapSchema readerSchema)
             {
@@ -199,7 +193,7 @@ namespace Avro.Specific
                 type = type.Remove(0, 19);             // remove IDictionary<string,
                 type = type.Remove(type.Length - 1);   // remove >
 
-                objCreator = GetConstructor(type, Schema.Type.Map);
+                typeName = type;
             }
 
             public object Create(object reuse)
@@ -214,7 +208,7 @@ namespace Avro.Specific
                     map.Clear();
                 }
                 else
-                    map = objCreator() as System.Collections.IDictionary;
+                    map = ObjectCreator.Instance.New(typeName, Schema.Type.Map) as System.Collections.IDictionary;
                 return map;
             }
 
@@ -229,4 +223,4 @@ namespace Avro.Specific
             }
         }
     }
-}
\ No newline at end of file
+}


[avro] 02/02: Cleanup csharp tests a bit

Posted by dk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git

commit b9409b3bee5a45e8ffc67430e6a878be564569ce
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Mon Dec 10 13:31:23 2018 -0500

    Cleanup csharp tests a bit
---
 .../src/apache/main/Specific/SpecificReader.cs     |  8 +------
 .../src/apache/test/Specific/SpecificTests.cs      | 26 +++++++++++-----------
 2 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/lang/csharp/src/apache/main/Specific/SpecificReader.cs b/lang/csharp/src/apache/main/Specific/SpecificReader.cs
index 384465b..cc8c594 100644
--- a/lang/csharp/src/apache/main/Specific/SpecificReader.cs
+++ b/lang/csharp/src/apache/main/Specific/SpecificReader.cs
@@ -1,4 +1,4 @@
-/**
+/**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -81,12 +81,6 @@ namespace Avro.Specific
     public class SpecificDefaultReader : DefaultReader
     {
         /// <summary>
-        /// Static dictionary of type names and its corresponding assembly type.
-        /// This is used to prevent multiple reflection for the same type name.
-        /// </summary>
-        private static IDictionary<string, Type> TypeName = new Dictionary<string, Type>();
-
-        /// <summary>
         /// Constructor
         /// </summary>
         /// <param name="writerSchema">schema of the object that wrote the data</param>
diff --git a/lang/csharp/src/apache/test/Specific/SpecificTests.cs b/lang/csharp/src/apache/test/Specific/SpecificTests.cs
index a39168d..aedf8f2 100644
--- a/lang/csharp/src/apache/test/Specific/SpecificTests.cs
+++ b/lang/csharp/src/apache/test/Specific/SpecificTests.cs
@@ -1,4 +1,4 @@
-/**
+/**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -30,14 +30,14 @@ namespace Avro.Test
 {
     [TestFixture]
     class SpecificTests
-    {
-        // The dynamically created assembly used in the test below can only be created
-        // once otherwise repeated tests will fail as the same type name will exist in
-        // multiple assemblies and so the type in the test and the type found by ObjectCreator
-        // will differ.  This single CompilerResults only works so long as there is only one test.
-        // If additional tests are added then each test will need its own CompilerResults.
-        private static CompilerResults compres;
-
+    {
+        // The dynamically created assembly used in the test below can only be created
+        // once otherwise repeated tests will fail as the same type name will exist in
+        // multiple assemblies and so the type in the test and the type found by ObjectCreator
+        // will differ.  This single CompilerResults only works so long as there is only one test.
+        // If additional tests are added then each test will need its own CompilerResults.
+        private static CompilerResults compres;
+
         [TestCase(@"{
   ""protocol"" : ""MyProtocol"",
   ""namespace"" : ""com.foo"",
@@ -94,9 +94,9 @@ namespace Avro.Test
 }"
 , new object[] {3, // index of the schema to serialize
   "com.foo.Z",  // name of the schema to serialize
-@"Console.WriteLine(""Constructing com.foo.Z..."");", // Empty Constructor.
+@"// Console.WriteLine(""Constructing com.foo.Z..."");", // Empty Constructor.
 @"
-  Console.WriteLine(""Populating com.foo.Z..."");
+  // Console.WriteLine(""Populating com.foo.Z..."");
   string bytes = ""bytes sample text"";
   System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
 
@@ -195,7 +195,7 @@ namespace Avro.Test
             comparam.GenerateInMemory = true;
             var ccp = new Microsoft.CSharp.CSharpCodeProvider();
             var units = new CodeCompileUnit[] { compileUnit };
-            compres = ccp.CompileAssemblyFromDom(comparam, units);
+            compres = ccp.CompileAssemblyFromDom(comparam, units);
             Assert.IsNotNull(compres);
             if (compres.Errors.Count > 0)
             {
@@ -375,4 +375,4 @@ namespace Avro.Test
             enumType = (EnumType)fieldValue;
         }
     }
-}
\ No newline at end of file
+}