[reflection] Convert internal GetCustomAttributes calls to Attribute[] (mono/mono...
authorRyan Lucia <rylucia@microsoft.com>
Thu, 2 Jan 2020 16:58:15 +0000 (11:58 -0500)
committerGitHub <noreply@github.com>
Thu, 2 Jan 2020 16:58:15 +0000 (11:58 -0500)
* [reflection] Avoid creating object[] in GetCustomAttributesBase

* [reflection] Migrate Attribute type checking to CustomAttribute.cs

Commit migrated from https://github.com/mono/mono/commit/ff6294d2311932902a9e790bbcc79f109a602d0c

src/mono/netcore/CoreFX.issues.rsp
src/mono/netcore/System.Private.CoreLib/src/System/Attribute.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

index 142c5c7..0fd5a67 100644 (file)
 -nonamespace Tests.Integration
 
 ####################################################################
-##  System.Composition.TypedParts.Tests
-####################################################################
-
-# Missing assembly Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
-# https://github.com/mono/mono/issues/15170
--nomethod System.Composition.Hosting.Tests.ContainerConfigurationTests.WithAssemblies_Assemblies_ThrowsCompositionFailedExceptionOnCreation
--nomethod System.Composition.Hosting.Tests.ContainerConfigurationTests.WithAssembly_Assembly_ThrowsCompositionFailedExceptionOnCreation
-
-####################################################################
 ##  System.Data.Common.Tests
 ####################################################################
 
index ca05813..3a67f4d 100644 (file)
@@ -35,48 +35,28 @@ namespace System
 
                public static Attribute[] GetCustomAttributes (Assembly element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
                public static Attribute[] GetCustomAttributes (Assembly element, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes (element, inherit);
-               public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
-               public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
+               public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
                public static Attribute[] GetCustomAttributes (MemberInfo element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
                public static Attribute[] GetCustomAttributes (MemberInfo element, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes (element, inherit);
-               public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
-               public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
+               public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
                public static Attribute[] GetCustomAttributes (Module element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
                public static Attribute[] GetCustomAttributes (Module element, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes (element, inherit);
-               public static Attribute[] GetCustomAttributes (Module element, Type attributeType) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
-               public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static Attribute[] GetCustomAttributes (Module element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
+               public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
                public static Attribute[] GetCustomAttributes (ParameterInfo element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
                public static Attribute[] GetCustomAttributes (ParameterInfo element, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes (element, inherit);
-               public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
-               public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit) => (Attribute[])GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
-
-               internal static Attribute[] GetCustomAttributes (ICustomAttributeProvider element, Type attributeType, bool inherit)
-               {
-                       if (attributeType == null)
-                               throw new ArgumentNullException (nameof (attributeType));
-                       if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute))
-                               throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
-
-                       return (Attribute[])CustomAttribute.GetCustomAttributes (element, attributeType, inherit);
-               }
-
-               public static bool IsDefined (Assembly element, Type attributeType) => IsDefined ((ICustomAttributeProvider)element, attributeType, true);
-               public static bool IsDefined (Assembly element, Type attributeType, bool inherit) => IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
-               public static bool IsDefined (MemberInfo element, Type attributeType) => IsDefined ((ICustomAttributeProvider)element, attributeType, true);
-               public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit) => IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
-               public static bool IsDefined (Module element, Type attributeType) => IsDefined ((ICustomAttributeProvider)element, attributeType, true);
-               public static bool IsDefined (Module element, Type attributeType, bool inherit) => IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
-               public static bool IsDefined (ParameterInfo element, Type attributeType) => IsDefined ((ICustomAttributeProvider)element, attributeType, true);
-               public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit) => IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
-
-               internal static bool IsDefined (ICustomAttributeProvider element, Type attributeType, bool inherit)
-               {
-                       if (attributeType == null)
-                               throw new ArgumentNullException (nameof (attributeType));
-                       if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute))
-                               throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
-
-                       return CustomAttribute.IsDefined (element, attributeType, inherit);
-               }
+               public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
+               public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
+
+               public static bool IsDefined (Assembly element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
+               public static bool IsDefined (Assembly element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static bool IsDefined (MemberInfo element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
+               public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static bool IsDefined (Module element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
+               public static bool IsDefined (Module element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
+               public static bool IsDefined (ParameterInfo element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
+               public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
        }
 }
index 28966a2..03880d3 100644 (file)
@@ -53,7 +53,7 @@ namespace System.Reflection
                }
        
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, Type attributeType, bool pseudoAttrs);
+               internal static extern Attribute[] GetCustomAttributesInternal (ICustomAttributeProvider obj, Type attributeType, bool pseudoAttrs);
 
                internal static object[] GetPseudoCustomAttributes (ICustomAttributeProvider obj, Type attributeType) {
                        object[] pseudoAttrs = null;
@@ -120,7 +120,7 @@ namespace System.Reflection
                        if (!inheritedOnly) {
                                object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
                                if (pseudoAttrs != null) {
-                                       object[] res = new object [attrs.Length + pseudoAttrs.Length];
+                                       object[] res = new Attribute [attrs.Length + pseudoAttrs.Length];
                                        System.Array.Copy (attrs, res, attrs.Length);
                                        System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
                                        return res;
@@ -135,13 +135,16 @@ namespace System.Reflection
                        if (obj == null)
                                throw new ArgumentNullException (nameof (obj));
                        if (attributeType == null)
-                               throw new ArgumentNullException (nameof (attributeType));       
+                               throw new ArgumentNullException (nameof (attributeType));
+                       if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute)&& attributeType != typeof (CustomAttribute) && attributeType != typeof (System.Object))
+                               throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
 
                        if (attributeType == typeof (CustomAttribute))
                                attributeType = null;
-
                        if (attributeType == typeof (Attribute))
                                attributeType = null;
+                       if (attributeType == typeof (System.Object))
+                               attributeType = null;
 
                        object[] r;
                        object[] res = GetCustomAttributesBase (obj, attributeType, false);
@@ -505,7 +508,9 @@ namespace System.Reflection
                internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
                {
                        if (attributeType == null)
-                               throw new ArgumentNullException ("attributeType");
+                               throw new ArgumentNullException (nameof (attributeType));
+                       if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute))
+                               throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
 
                        AttributeUsageAttribute usage = null;
                        do {