Skip to content

Commit

Permalink
Merge pull request #35 from brandhuf/fix-generics
Browse files Browse the repository at this point in the history
Fix dependencies to generics and compiler generated types
  • Loading branch information
fgather authored Jan 19, 2021
2 parents fbad039 + 687dc8c commit 591c5ed
Show file tree
Hide file tree
Showing 99 changed files with 3,624 additions and 1,463 deletions.
10 changes: 9 additions & 1 deletion ArchUnitNET/Domain/Architecture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@ public class Architecture
private readonly ObjectProviderCache _objectProviderCache;

public Architecture(IEnumerable<Assembly> allAssemblies, IEnumerable<Namespace> namespaces,
IEnumerable<IType> types)
IEnumerable<IType> types, IEnumerable<GenericParameter> genericParameters,
IEnumerable<IType> referencedTypes)
{
_allAssemblies = allAssemblies;
Namespaces = namespaces;
Types = types;
GenericParameters = genericParameters;
ReferencedTypes = referencedTypes;
_objectProviderCache = new ObjectProviderCache(this);
}

public IEnumerable<Assembly> Assemblies => _allAssemblies.Where(assembly => !assembly.IsOnlyReferenced);
public IEnumerable<Namespace> Namespaces { get; }
public IEnumerable<IType> Types { get; }
public IEnumerable<GenericParameter> GenericParameters { get; }
public IEnumerable<IType> ReferencedTypes { get; }
public IEnumerable<Class> Classes => Types.OfType<Class>();
public IEnumerable<Interface> Interfaces => Types.OfType<Interface>();
public IEnumerable<Attribute> Attributes => Types.OfType<Attribute>();
public IEnumerable<Class> ReferencedClasses => ReferencedTypes.OfType<Class>();
public IEnumerable<Interface> ReferencedInterfaces => ReferencedTypes.OfType<Interface>();
public IEnumerable<Attribute> ReferencedAttributes => ReferencedTypes.OfType<Attribute>();
public IEnumerable<PropertyMember> PropertyMembers => Members.OfType<PropertyMember>();
public IEnumerable<FieldMember> FieldMembers => Members.OfType<FieldMember>();
public IEnumerable<MethodMember> MethodMembers => Members.OfType<MethodMember>();
Expand Down
9 changes: 5 additions & 4 deletions ArchUnitNET/Domain/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public bool? IsStruct

public Visibility Visibility => Type.Visibility;
public bool IsNested => Type.IsNested;
public bool IsGeneric => Type.IsGeneric;
public bool IsGenericParameter => Type.IsGenericParameter;
public bool IsStub => Type.IsStub;
public bool IsCompilerGenerated => Type.IsCompilerGenerated;
public string Name => Type.Name;
public string FullName => Type.FullName;

Expand All @@ -78,10 +82,7 @@ public bool? IsStruct
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;
public MemberList Members => Type.Members;


public List<IType> GenericTypeParameters => Type.GenericTypeParameters;
public IType GenericType => Type.GenericType;
public List<IType> GenericTypeArguments => Type.GenericTypeArguments;
public List<GenericParameter> GenericParameters => Type.GenericParameters;

public bool ImplementsInterface(Interface intf)
{
Expand Down
63 changes: 63 additions & 0 deletions ArchUnitNET/Domain/Dependencies/AccessFieldDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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;

namespace ArchUnitNET.Domain.Dependencies
{
public class AccessFieldDependency : IMemberMemberDependency
{
// ReSharper disable once SuggestBaseTypeForParameter
public AccessFieldDependency(IMember originMember, FieldMember accessedField)
{
OriginMember = originMember;
TargetMember = accessedField;
}

public IMember OriginMember { get; }
public IMember TargetMember { get; }

public IEnumerable<GenericArgument> TargetGenericArguments => Enumerable.Empty<GenericArgument>();
public IEnumerable<GenericArgument> TargetMemberGenericArguments => Enumerable.Empty<GenericArgument>();

public IType Origin => OriginMember.DeclaringType;
public IType Target => TargetMember.DeclaringType;

public bool TargetIsArray => false;
public IEnumerable<int> TargetArrayDimensions => Enumerable.Empty<int>();

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

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

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

private bool Equals(IMemberMemberDependency other)
{
return Equals(TargetMember, other.TargetMember) && Equals(OriginMember, other.OriginMember);
}

public override int GetHashCode()
{
unchecked
{
return ((TargetMember != null ? TargetMember.GetHashCode() : 0) * 397) ^
(OriginMember != null ? OriginMember.GetHashCode() : 0);
}
}
}
}
58 changes: 3 additions & 55 deletions ArchUnitNET/Domain/Dependencies/AttributeMemberDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,11 @@

namespace ArchUnitNET.Domain.Dependencies
{
public class AttributeMemberDependency : IMemberTypeDependency
public class AttributeMemberDependency : MemberTypeInstanceDependency
{
public AttributeMemberDependency(IMember member, Attribute attribute)
public AttributeMemberDependency(IMember member, ITypeInstance<Attribute> attributeInstance)
: base(member, attributeInstance)
{
OriginMember = member;
Target = attribute;
}

public IType Target { get; } //attribute

public IMember OriginMember { get; } //object with attribute

public IType Origin => OriginMember.DeclaringType; //class of object with attribute

public bool Equals(AttributeMemberDependency other)
{
if (ReferenceEquals(null, other))
{
return false;
}

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

return Equals(Target, other.Target) && Equals(OriginMember, other.OriginMember);
}

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

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

if (obj.GetType() != GetType())
{
return false;
}

return Equals((AttributeMemberDependency) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Target != null ? Target.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (OriginMember != null ? OriginMember.GetHashCode() : 0);
return hashCode;
}
}
}
}
55 changes: 3 additions & 52 deletions ArchUnitNET/Domain/Dependencies/AttributeTypeDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,11 @@

namespace ArchUnitNET.Domain.Dependencies
{
public class AttributeTypeDependency : ITypeDependency
public class AttributeTypeDependency : TypeInstanceDependency
{
public AttributeTypeDependency(IType origin, Attribute target)
public AttributeTypeDependency(IType origin, ITypeInstance<Attribute> attributeInstance)
: base(origin, attributeInstance)
{
Origin = origin;
Target = target;
}

public IType Origin { get; }
public IType Target { get; }

public bool Equals(AttributeTypeDependency other)
{
if (ReferenceEquals(null, other))
{
return false;
}

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

return Equals(Target, other.Target) && Equals(Origin, other.Origin);
}

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

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

if (obj.GetType() != GetType())
{
return false;
}

return Equals((AttributeTypeDependency) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Target != null ? Target.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (Origin != null ? Origin.GetHashCode() : 0);
return hashCode;
}
}
}
}
58 changes: 4 additions & 54 deletions ArchUnitNET/Domain/Dependencies/BodyTypeMemberDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,12 @@

namespace ArchUnitNET.Domain.Dependencies
{
public class BodyTypeMemberDependency : IMemberTypeDependency
public class BodyTypeMemberDependency : MemberTypeInstanceDependency
{
public BodyTypeMemberDependency(MethodMember method, IType target)
// ReSharper disable once SuggestBaseTypeForParameter
public BodyTypeMemberDependency(MethodMember method, ITypeInstance<IType> targetTypeInstance)
: base(method, targetTypeInstance)
{
OriginMember = method;
Target = target;
}

public IMember OriginMember { get; }

public IType Origin => OriginMember.DeclaringType;
public IType Target { get; }

public bool Equals(BodyTypeMemberDependency other)
{
if (ReferenceEquals(null, other))
{
return false;
}

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

return Equals(OriginMember, other.OriginMember) && Equals(Target, other.Target);
}

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

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

if (obj.GetType() != GetType())
{
return false;
}

return Equals((BodyTypeMemberDependency) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = OriginMember != null ? OriginMember.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (Target != null ? Target.GetHashCode() : 0);
return hashCode;
}
}
}
}
17 changes: 17 additions & 0 deletions ArchUnitNET/Domain/Dependencies/CastTypeDependency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.Dependencies
{
public class CastTypeDependency : MemberTypeInstanceDependency
{
public CastTypeDependency(IMember originMember, ITypeInstance<IType> castTypeInstance)
: base(originMember, castTypeInstance)
{
}
}
}
51 changes: 2 additions & 49 deletions ArchUnitNET/Domain/Dependencies/FieldTypeDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,11 @@

namespace ArchUnitNET.Domain.Dependencies
{
public class FieldTypeDependency : IMemberTypeDependency
public class FieldTypeDependency : MemberTypeInstanceDependency
{
private readonly FieldMember _originMember;

public FieldTypeDependency(FieldMember field)
: base(field, field)
{
_originMember = field;
}

public IType Target => _originMember.Type;
public IMember OriginMember => _originMember;

public IType Origin => OriginMember.DeclaringType;

public bool Equals(FieldTypeDependency other)
{
if (ReferenceEquals(null, other))
{
return false;
}

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

return Equals(_originMember, other._originMember);
}

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

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

if (obj.GetType() != GetType())
{
return false;
}

return Equals((FieldTypeDependency) obj);
}

public override int GetHashCode()
{
return _originMember != null ? _originMember.GetHashCode() : 0;
}
}
}
Loading

0 comments on commit 591c5ed

Please sign in to comment.