* FieldBuilder.cs: Added null check for type.
authorGert Driesen <drieseng@users.sourceforge.net>
Sun, 11 May 2008 20:03:52 +0000 (20:03 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sun, 11 May 2008 20:03:52 +0000 (20:03 -0000)
* TypeBuilder.cs: For enums, construct UnderlyingSystemType when
first instance field is defined instead of having to lookup it up on
demand. Avoid cast in IsCompilerContext.
* TypeBuilderTest.cs: Added test for type null check in DefineField.

svn path=/trunk/mcs/; revision=102973

mcs/class/corlib/System.Reflection.Emit/ChangeLog
mcs/class/corlib/System.Reflection.Emit/FieldBuilder.cs
mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs
mcs/class/corlib/Test/System.Reflection.Emit/ChangeLog
mcs/class/corlib/Test/System.Reflection.Emit/TypeBuilderTest.cs

index a03c25d7cf50936727fe12965aedd9bebf27786c..a2751fe6e26f559d0c3dcd0180053895cc7de625 100644 (file)
@@ -1,3 +1,10 @@
+2008-05-11  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * FieldBuilder.cs: Added null check for type.
+       * TypeBuilder.cs: For enums, construct UnderlyingSystemType when
+       first instance field is defined instead of having to lookup it up on
+       demand. Avoid cast in IsCompilerContext.
+
 2008-05-11  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * TypeBuilder.cs: Use Assembly.FullName instead of AssemblyName.
index 52f87df7ed6631202c56ae0bf3ef7337c9fffd29..991afd1ffcbabe847b9ba07b845e0a2e5bbf43be 100644 (file)
@@ -59,7 +59,11 @@ namespace System.Reflection.Emit {
                private Type[] modReq;
                private Type[] modOpt;
 
-               internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt) {
+               internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
                        attrs = attributes;
                        name = fieldName;
                        this.type = type;
index 1884c82432db59137b4f7b950d291297185a3326..2e2c69be704079720b2c42808a47a02af9fce593 100644 (file)
@@ -83,6 +83,7 @@ namespace System.Reflection.Emit
                #endregion
                string fullname;
                bool createTypeCalled;
+               private Type underlying_type;
 
        public const int UnspecifiedTypeSize = 0;
 
@@ -194,13 +195,8 @@ namespace System.Reflection.Emit
                                        return created.UnderlyingSystemType;
 
                                if (IsEnum && !IsCompilerContext) {
-                                       for (int i = 0; i < num_fields; i++) {
-                                               FieldBuilder field = fields [i];
-                                               if ((field.Attributes & FieldAttributes.Static) == 0) {
-                                                       return field.FieldType;
-                                               }
-                                       }
-
+                                       if (underlying_type != null)
+                                               return underlying_type;
                                        throw new InvalidOperationException (
                                                "Enumeration type is not defined.");
                                }
@@ -686,6 +682,12 @@ namespace System.Reflection.Emit
                                num_fields ++;
                                create_internal_class (this);
                        }
+
+                       if (IsEnum && !IsCompilerContext) {
+                               if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
+                                       underlying_type = type;
+                       }
+
                        return res;
                }
 
@@ -1618,7 +1620,7 @@ namespace System.Reflection.Emit
 
                internal bool IsCompilerContext {
                        get {
-                               return ((AssemblyBuilder) Assembly).IsCompilerContext;
+                               return pmodule.assemblyb.IsCompilerContext;
                        }
                }
 
index 783e723914d8c9e611a2e14d85fc61dc15516ef2..0062ff5a44e8dff95b0c7d15bf315391891b8a64 100644 (file)
@@ -1,3 +1,7 @@
+2008-05-11  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * TypeBuilderTest.cs: Added test for type null check in DefineField. 
+
 2008-05-11  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * TypeBuilderTest.cs: Renamed tests and updated bug number. Enabled
index 0d16cc70faaee0936d122d7099be113bb08a5416..ad71a2afa73a21ba6a6216630a85463c52700747 100644 (file)
@@ -1330,31 +1330,8 @@ namespace MonoTests.System.Reflection.Emit
                        }
                }
 
-               [Test]
-               public void DefineField_Name_NullChar ()
-               {
-                       TypeBuilder tb = module.DefineType (genTypeName ());
-
-                       try {
-                               tb.DefineField ("\0test", typeof (int),
-                                       FieldAttributes.Private);
-                               Assert.Fail ("#A1");
-                       } catch (ArgumentException ex) {
-                               // Illegal name
-                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
-                               Assert.IsNull (ex.InnerException, "#A3");
-                               Assert.IsNotNull (ex.Message, "#A4");
-                               Assert.AreEqual ("fieldName", ex.ParamName, "#A5");
-                       }
-
-                       FieldBuilder fb = tb.DefineField ("te\0st", typeof (int),
-                               FieldAttributes.Private);
-                       Assert.IsNotNull (fb, "#B1");
-                       Assert.AreEqual ("te\0st", fb.Name, "#B2");
-               }
-
-               [Test]
-               public void TestDefineField ()
+               [Test] // DefineField (String, Type, FieldAttributes)
+               public void DefineField1 ()
                {
                        TypeBuilder tb = module.DefineType (genTypeName ());
 
@@ -1418,6 +1395,65 @@ namespace MonoTests.System.Reflection.Emit
                        }
                }
 
+               [Test] // DefineField (String, Type, FieldAttributes)
+               public void DefineField1_Name_NullChar ()
+               {
+                       TypeBuilder tb = module.DefineType (genTypeName ());
+
+                       try {
+                               tb.DefineField ("\0test", typeof (int),
+                                       FieldAttributes.Private);
+                               Assert.Fail ("#A1");
+                       } catch (ArgumentException ex) {
+                               // Illegal name
+                               Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.AreEqual ("fieldName", ex.ParamName, "#A5");
+                       }
+
+                       FieldBuilder fb = tb.DefineField ("te\0st", typeof (int),
+                               FieldAttributes.Private);
+                       Assert.IsNotNull (fb, "#B1");
+                       Assert.AreEqual ("te\0st", fb.Name, "#B2");
+               }
+
+               [Test] // DefineField (String, Type, FieldAttributes)
+               public void DefineField1_Type_Null ()
+               {
+                       TypeBuilder tb = module.DefineType (genTypeName ());
+
+                       try {
+                               tb.DefineField ("test", (Type) null,
+                                       FieldAttributes.Private);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.AreEqual ("type", ex.ParamName, "#5");
+                       }
+               }
+
+#if NET_2_0
+               [Test] // DefineField (String, Type, Type [], Type [], FieldAttributes)
+               public void DefineField2_Type_Null ()
+               {
+                       TypeBuilder tb = module.DefineType (genTypeName ());
+
+                       try {
+                               tb.DefineField ("test", (Type) null, Type.EmptyTypes,
+                                       Type.EmptyTypes, FieldAttributes.Private);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.AreEqual ("type", ex.ParamName, "#5");
+                       }
+               }
+#endif
+
                [Test]
                public void TestDefineInitializedData ()
                {