avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [avro] 01/02: [AVRO-1952] Squashed commit of the following: This closes #134
Date Mon, 10 Dec 2018 18:31:52 GMT
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 <dkulp@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_low@hotmail.com>
    Date:   Wed Sep 28 20:10:17 2016 +0900
    
        removed unused GetConstructor methods
    
    commit aad24a7b1bbeb39105b8b3ee9578dbc792d8cfea
    Author: dolow <do_low@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
+}


Mime
View raw message