Skip to content

Commit

Permalink
[reflection] Convert internal GetCustomAttributes calls to Attribute[] (
Browse files Browse the repository at this point in the history
mono/mono#18176)

* [reflection] Avoid creating object[] in GetCustomAttributesBase

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


Commit migrated from mono/mono@ff6294d
  • Loading branch information
CoffeeFlux authored Jan 2, 2020
1 parent 4f79325 commit 0844cb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
9 changes: 0 additions & 9 deletions src/mono/netcore/CoreFX.issues.rsp
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,6 @@
# same issue (only System.ComponentModel.Composition uses this namespace)
-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
####################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,28 @@ partial class Attribute

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static bool IsUserCattrProvider (object obj)
}

[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;
Expand Down Expand Up @@ -120,7 +120,7 @@ internal static object[] GetCustomAttributesBase (ICustomAttributeProvider obj,
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;
Expand All @@ -135,13 +135,16 @@ internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type
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);
Expand Down Expand Up @@ -505,7 +508,9 @@ static CustomAttributeData[] GetPseudoCustomAttributesData (Type type)
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 {
Expand Down

0 comments on commit 0844cb7

Please sign in to comment.