Skip to content

Commit

Permalink
fix: deduplicate types by assembly qualified name
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Linne <[email protected]>
  • Loading branch information
alexanderlinne committed Oct 25, 2024
1 parent aa44379 commit 4272e7f
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 295 deletions.
46 changes: 15 additions & 31 deletions ArchUnitNET/Domain/GenericParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,42 @@ namespace ArchUnitNET.Domain
{
public class GenericParameter : IType
{
private readonly string _declarerFullName;
internal readonly IEnumerable<ITypeInstance<IType>> TypeInstanceConstraints;

public GenericParameter(
string declarerFullName,
ITypeInstance<IType> declaringTypeInstance,
[CanBeNull] MethodMemberInstance declaringMethodInstance,
string fullName,
string name,
GenericParameterVariance variance,
IEnumerable<ITypeInstance<IType>> typeConstraints,
bool hasReferenceTypeConstraint,
bool hasNotNullableValueTypeConstraint,
bool hasDefaultConstructorConstraint,
bool isCompilerGenerated,
bool declarerIsMethod
bool isCompilerGenerated
)
{
_declarerFullName = declarerFullName;
DeclaringTypeInstance = declaringTypeInstance;
DeclaringMethodInstance = declaringMethodInstance;
FullName = fullName;
Name = name;
Variance = variance;
TypeInstanceConstraints = typeConstraints;
HasReferenceTypeConstraint = hasReferenceTypeConstraint;
HasNotNullableValueTypeConstraint = hasNotNullableValueTypeConstraint;
HasDefaultConstructorConstraint = hasDefaultConstructorConstraint;
IsCompilerGenerated = isCompilerGenerated;
DeclarerIsMethod = declarerIsMethod;
}

public IType DeclaringType { get; private set; }
public ITypeInstance<IType> DeclaringTypeInstance { get; }
public IType DeclaringType => DeclaringTypeInstance.Type;

[CanBeNull]
public IMember DeclaringMethod { get; private set; }
public bool DeclarerIsMethod { get; }
public MethodMemberInstance DeclaringMethodInstance { get; }

[CanBeNull]
public IMember DeclaringMethod => DeclaringMethodInstance?.Member;
public bool DeclarerIsMethod => DeclaringMethodInstance != null;
public GenericParameterVariance Variance { get; }
public IEnumerable<IType> TypeConstraints =>
TypeInstanceConstraints.Select(instance => instance.Type);
Expand All @@ -59,7 +64,7 @@ bool declarerIsMethod
|| TypeConstraints.Any();

public string Name { get; }
public string FullName => _declarerFullName + "+<" + Name + ">";
public string FullName { get; }
public string AssemblyQualifiedName =>
System.Reflection.Assembly.CreateQualifiedName(
DeclaringType.Assembly.FullName,
Expand All @@ -84,27 +89,6 @@ bool declarerIsMethod
public bool IsNested => true;
public bool IsStub => true;

internal void AssignDeclarer(IMember declaringMethod)
{
if (!declaringMethod.FullName.Equals(_declarerFullName))
{
throw new InvalidOperationException("Full name of declaring member doesn't match.");
}

DeclaringType = declaringMethod.DeclaringType;
DeclaringMethod = declaringMethod;
}

internal void AssignDeclarer(IType declaringType)
{
if (!declaringType.FullName.Equals(_declarerFullName))
{
throw new InvalidOperationException("Full name of declaring type doesn't match.");
}

DeclaringType = declaringType;
}

public bool Equals(GenericParameter other)
{
if (ReferenceEquals(null, other))
Expand Down
4 changes: 1 addition & 3 deletions ArchUnitNET/Domain/MethodMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public MethodMember(
string fullName,
IType declaringType,
Visibility visibility,
ITypeInstance<IType> returnTypeInstance,
bool isVirtual,
MethodForm methodForm,
bool isGeneric,
Expand All @@ -35,7 +34,6 @@ public MethodMember(
);
DeclaringType = declaringType;
Visibility = visibility;
ReturnTypeInstance = returnTypeInstance;
IsVirtual = isVirtual;
MethodForm = methodForm;
IsGeneric = isGeneric;
Expand All @@ -52,7 +50,7 @@ public MethodMember(
new List<ITypeInstance<IType>>();
public IEnumerable<IType> Parameters =>
ParameterInstances.Select(instance => instance.Type);
public ITypeInstance<IType> ReturnTypeInstance { get; }
public ITypeInstance<IType> ReturnTypeInstance { get; internal set; }
public IType ReturnType => ReturnTypeInstance.Type;
public bool IsStub { get; }
public bool IsCompilerGenerated { get; }
Expand Down
4 changes: 0 additions & 4 deletions ArchUnitNET/Loader/ArchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public ArchBuilder()
_assemblyRegistry = new AssemblyRegistry();
_namespaceRegistry = new NamespaceRegistry();
_loadTaskRegistry = new LoadTaskRegistry();
var typeRegistry = new TypeRegistry();
var methodMemberRegistry = new MethodMemberRegistry();
_typeFactory = new TypeFactory(
typeRegistry,
methodMemberRegistry,
_loadTaskRegistry,
_assemblyRegistry,
_namespaceRegistry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private void AddTypeGenericParameterDependencies()
{
foreach (var genericParameter in _type.GenericParameters)
{
genericParameter.AssignDeclarer(_type);
foreach (var typeInstanceConstraint in genericParameter.TypeInstanceConstraints)
{
var dependency = new TypeGenericParameterTypeConstraintDependency(
Expand All @@ -49,7 +48,6 @@ private void AddMemberGenericParameterDependencies()
{
foreach (var genericParameter in member.GenericParameters)
{
genericParameter.AssignDeclarer(member);
foreach (var typeInstanceConstraint in genericParameter.TypeInstanceConstraints)
{
var dependency = new MemberGenericParameterTypeConstraintDependency(
Expand Down
15 changes: 9 additions & 6 deletions ArchUnitNET/Loader/LoadTasks/AddMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ namespace ArchUnitNET.Loader.LoadTasks
internal class AddMembers : ILoadTask
{
private readonly MemberList _memberList;
private readonly IType _type;
private readonly ITypeInstance<IType> _typeInstance;
private readonly TypeDefinition _typeDefinition;
private readonly TypeFactory _typeFactory;

public AddMembers(
IType type,
ITypeInstance<IType> typeInstance,
TypeDefinition typeDefinition,
TypeFactory typeFactory,
MemberList memberList
)
{
_type = type;
_typeInstance = typeInstance;
_typeDefinition = typeDefinition;
_typeFactory = typeFactory;
_memberList = memberList;
Expand All @@ -53,7 +53,10 @@ private IEnumerable<IMember> CreateMembers([NotNull] TypeDefinition typeDefiniti
.Concat(
typeDefinition.Methods.Select(method =>
_typeFactory
.GetOrCreateMethodMemberFromMethodReference(_type, method)
.GetOrCreateMethodMemberFromMethodReference(
_typeInstance,
method
)
.Member
)
)
Expand All @@ -72,7 +75,7 @@ private IMember CreateFieldMember([NotNull] FieldDefinition fieldDefinition)
var isCompilerGenerated = fieldDefinition.IsCompilerGenerated();
var writeAccessor = GetWriteAccessor(fieldDefinition);
return new FieldMember(
_type,
_typeInstance.Type,
fieldDefinition.Name,
fieldDefinition.FullName,
visibility,
Expand All @@ -96,7 +99,7 @@ private IMember CreatePropertyMember(PropertyDefinition propertyDefinition)
|| (propertyDefinition.GetMethod != null && propertyDefinition.GetMethod.IsStatic);
var writeAccessor = GetWriteAccessor(propertyDefinition);
return new PropertyMember(
_type,
_typeInstance.Type,
propertyDefinition.Name,
propertyDefinition.FullName,
propertyType,
Expand Down
39 changes: 0 additions & 39 deletions ArchUnitNET/Loader/MethodMemberRegistry.cs

This file was deleted.

11 changes: 8 additions & 3 deletions ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
// SPDX-License-Identifier: Apache-2.0
//

using System;
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain;
using JetBrains.Annotations;
using Mono.Cecil;
using Attribute = ArchUnitNET.Domain.Attribute;

namespace ArchUnitNET.Loader
{
Expand All @@ -25,9 +27,12 @@ TypeFactory typeFactory
var attributeType = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(
attributeTypeReference
);
var attribute = attributeType.Type is Class cls
? new Attribute(cls)
: new Attribute(attributeType.Type, null, null);
if (!(attributeType.Type is Attribute attribute))
{
throw new ArgumentException(
$"Attribute type {attributeType.Type.FullName} is not an attribute."
);
}

var attributeArguments = new List<AttributeArgument>();

Expand Down
4 changes: 2 additions & 2 deletions ArchUnitNET/Loader/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override string ToString()

private bool Equals(Type other)
{
return string.Equals(FullName, other.FullName);
return string.Equals(AssemblyQualifiedName, other.AssemblyQualifiedName);
}

public override bool Equals(object obj)
Expand All @@ -104,7 +104,7 @@ public override bool Equals(object obj)

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

0 comments on commit 4272e7f

Please sign in to comment.