Skip to content

Commit

Permalink
Merge pull request #93 from brandhuf/feature_attribute_arguments
Browse files Browse the repository at this point in the history
Feature attribute arguments
  • Loading branch information
fgather authored Jul 22, 2021
2 parents 01b1298 + bca65eb commit 0783ab4
Show file tree
Hide file tree
Showing 28 changed files with 3,478 additions and 43 deletions.
59 changes: 59 additions & 0 deletions ArchUnitNET/Domain/AttributeArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

using System.Collections.Generic;

namespace ArchUnitNET.Domain
{
public class AttributeArgument : ITypeInstance<IType>
{
private readonly ITypeInstance<IType> _typeInstance;

public AttributeArgument(object value, ITypeInstance<IType> typeInstance)
{
Value = value;
_typeInstance = typeInstance;
}

public readonly object Value;

public IType Type => _typeInstance.Type;
public IEnumerable<GenericArgument> GenericArguments => _typeInstance.GenericArguments;
public bool IsArray => _typeInstance.IsArray;
public IEnumerable<int> ArrayDimensions => _typeInstance.ArrayDimensions;

private bool Equals(AttributeArgument other)
{
return Equals(_typeInstance, other._typeInstance) && Equals(Value, other.Value);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj.GetType() == GetType() && Equals((AttributeArgument) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = _typeInstance != null ? Type.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (Value != null ? Value.GetHashCode() : 0);
return hashCode;
}
}
}
}
32 changes: 32 additions & 0 deletions ArchUnitNET/Domain/AttributeInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Loader;

namespace ArchUnitNET.Domain
{
public class AttributeInstance : TypeInstance<Attribute>
{
// ReSharper disable SuggestBaseTypeForParameter
public AttributeInstance(Attribute attribute, IEnumerable<AttributeArgument> attributeArguments) :
base(attribute)
{
AttributeArguments = attributeArguments;
}

public AttributeInstance(Attribute attribute) : base(attribute)
{
AttributeArguments = Enumerable.Empty<AttributeArgument>();
}

public IEnumerable<AttributeArgument> AttributeArguments { get; }

public bool HasAttributeArguments => AttributeArguments.Any();
}
}
50 changes: 50 additions & 0 deletions ArchUnitNET/Domain/AttributeNamedArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

namespace ArchUnitNET.Domain
{
public class AttributeNamedArgument : AttributeArgument
{
public AttributeNamedArgument(string name, object value, ITypeInstance<IType> typeInstance) : base(value,
typeInstance)
{
Name = name;
}

public readonly string Name;

private bool Equals(AttributeNamedArgument other)
{
return base.Equals(other) && Equals(Name, other.Name);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj.GetType() == GetType() && Equals((AttributeNamedArgument) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
return hashCode;
}
}
}
}
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public bool? IsStruct
public List<ITypeDependency> Dependencies => Type.Dependencies;
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;

public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;

public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;
public MemberList Members => Type.Members;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ArchUnitNET.Domain.Dependencies
{
public class AttributeMemberDependency : MemberTypeInstanceDependency
{
public AttributeMemberDependency(IMember member, ITypeInstance<Attribute> attributeInstance)
public AttributeMemberDependency(IMember member, AttributeInstance attributeInstance)
: base(member, attributeInstance)
{
}
Expand Down
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/Dependencies/AttributeTypeDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ArchUnitNET.Domain.Dependencies
{
public class AttributeTypeDependency : TypeInstanceDependency
{
public AttributeTypeDependency(IType origin, ITypeInstance<Attribute> attributeInstance)
public AttributeTypeDependency(IType origin, AttributeInstance attributeInstance)
: base(origin, attributeInstance)
{
}
Expand Down
4 changes: 2 additions & 2 deletions ArchUnitNET/Domain/Extensions/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public static class AttributeExtensions
{
public static bool HasAttribute(this IHasAttributes a, string pattern, bool useRegularExpressions = false)
{
return a.Attributes.Any(attribute => attribute.FullNameMatches(pattern, useRegularExpressions));
return a.Attributes.Any(attribute => attribute.Type.FullNameMatches(pattern, useRegularExpressions));
}

public static bool OnlyHasAttributes(this IHasAttributes a, string pattern, bool useRegularExpressions = false)
{
return a.Attributes.IsNullOrEmpty() ||
a.Attributes.All(attribute => attribute.FullNameMatches(pattern, useRegularExpressions));
a.Attributes.All(attribute => attribute.Type.FullNameMatches(pattern, useRegularExpressions));
}
}
}
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/Extensions/MemberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static bool HasFieldTypeDependencies(this IMember member, bool getBackwar

public static Attribute GetAttributeFromMember(this IMember member, Class attributeClass)
{
return member.Attributes.Find(attribute => attribute.FullName.Equals(attributeClass.FullName));
return member.Attributes.FirstOrDefault(attribute => attribute.FullName.Equals(attributeClass.FullName));
}

public static bool HasMethodSignatureDependency(this IMember member,
Expand Down
2 changes: 1 addition & 1 deletion ArchUnitNET/Domain/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static bool HasMemberWithFullName(this IType type, string fullname)

public static Attribute GetAttributeOfType(this IType type, Class attributeClass)
{
return type.Attributes.Find(attribute => attribute.FullName.Equals(attributeClass.FullName));
return type.Attributes.FirstOrDefault(attribute => attribute.FullName.Equals(attributeClass.FullName));
}

public static bool ResidesInNamespace(this IType e, string pattern, bool useRegularExpressions = false)
Expand Down
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/FieldMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public FieldMember(IType declaringType, string name, string fullName, Visibility
public bool IsCompilerGenerated { get; }
public bool IsGeneric => false;
public List<GenericParameter> GenericParameters => new List<GenericParameter>();
public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances { get; } = new List<AttributeInstance>();
public List<IMemberTypeDependency> MemberDependencies { get; } = new List<IMemberTypeDependency>();
public List<IMemberTypeDependency> MemberBackwardsDependencies { get; } = new List<IMemberTypeDependency>();
public List<ITypeDependency> Dependencies => MemberDependencies.Cast<ITypeDependency>().ToList();
Expand Down
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/GenericParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public GenericParameter(string declarerFullName, string name, GenericParameterVa
public string Name { get; }
public string FullName => _declarerFullName + "+<" + Name + ">";
public bool IsCompilerGenerated { get; }
public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances { get; } = new List<AttributeInstance>();

public List<ITypeDependency> Dependencies { get; } = new List<ITypeDependency>();
public List<ITypeDependency> BackwardsDependencies { get; } = new List<ITypeDependency>();
Expand Down
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/IHasAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ArchUnitNET.Domain
{
public interface IHasAttributes
{
List<Attribute> Attributes { get; }
IEnumerable<Attribute> Attributes { get; }
List<AttributeInstance> AttributeInstances { get; }
}
}
4 changes: 3 additions & 1 deletion ArchUnitNET/Domain/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain.Dependencies;
using ArchUnitNET.Domain.Extensions;

Expand All @@ -31,7 +32,8 @@ public Interface(IType type)
public Namespace Namespace => Type.Namespace;
public Assembly Assembly => Type.Assembly;

public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;

public List<ITypeDependency> Dependencies => Type.Dependencies;
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
Expand Down
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/MethodMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public MethodMember(string name, string fullName, IType declaringType, Visibilit
public bool IsGeneric { get; }
public List<GenericParameter> GenericParameters { get; } = new List<GenericParameter>();
public Visibility Visibility { get; }
public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances { get; } = new List<AttributeInstance>();
public List<IMemberTypeDependency> MemberDependencies { get; } = new List<IMemberTypeDependency>();
public List<IMemberTypeDependency> MemberBackwardsDependencies { get; } = new List<IMemberTypeDependency>();
public List<ITypeDependency> Dependencies => MemberDependencies.Cast<ITypeDependency>().ToList();
Expand Down
3 changes: 2 additions & 1 deletion ArchUnitNET/Domain/PropertyMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public PropertyMember(IType declaringType, string name, string fullName, ITypeIn
public string Name { get; }
public string FullName { get; }
public IType DeclaringType { get; }
public List<Attribute> Attributes { get; } = new List<Attribute>();
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
public List<AttributeInstance> AttributeInstances { get; } = new List<AttributeInstance>();

public List<IMemberTypeDependency> MemberDependencies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain;

namespace ArchUnitNET.Loader
namespace ArchUnitNET.Domain
{
public class TypeInstance<T> : ITypeInstance<T> where T : IType
{
Expand Down
Loading

0 comments on commit 0783ab4

Please sign in to comment.