Skip to content

Commit 1bfa1e6

Browse files
authored
Merge pull request #301 from TNG/292-missing-classes-in-architecture
Loader: Match Types By Assembly Qualified Name
2 parents 09fae24 + 977dd7e commit 1bfa1e6

13 files changed

+81
-5
lines changed

ArchUnit.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AttributeAssembly", "TestAs
2828
EndProject
2929
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisibilityAssembly", "TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj", "{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}"
3030
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoaderTestAssembly", "TestAssemblies\LoaderTestAssembly\LoaderTestAssembly.csproj", "{0243F2D4-AC89-4561-A936-D647B6BB821F}"
32+
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OtherLoaderTestAssembly", "TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj", "{5A24529B-1794-4080-ADCC-77440BA0A0B3}"
34+
EndProject
3135
Global
3236
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3337
Debug|Any CPU = Debug|Any CPU
@@ -82,6 +86,14 @@ Global
8286
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
8387
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
8488
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.Build.0 = Release|Any CPU
89+
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
90+
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Debug|Any CPU.Build.0 = Debug|Any CPU
91+
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Release|Any CPU.ActiveCfg = Release|Any CPU
92+
{0243F2D4-AC89-4561-A936-D647B6BB821F}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
96+
{5A24529B-1794-4080-ADCC-77440BA0A0B3}.Release|Any CPU.Build.0 = Release|Any CPU
8597
EndGlobalSection
8698
GlobalSection(SolutionProperties) = preSolution
8799
HideSolutionNode = FALSE
@@ -90,5 +102,7 @@ Global
90102
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
91103
{FB457140-47B4-4B20-8505-BA9BFC73C705} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
92104
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
105+
{0243F2D4-AC89-4561-A936-D647B6BB821F} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
106+
{5A24529B-1794-4080-ADCC-77440BA0A0B3} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
93107
EndGlobalSection
94108
EndGlobal

ArchUnitNET/Loader/ArchBuilder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,17 @@ public void LoadTypesForModule(ModuleDefinition module, string namespaceFilter)
111111
.ForEach(typeDefinition =>
112112
{
113113
var type = _typeFactory.GetOrCreateTypeFromTypeReference(typeDefinition);
114-
if (!_architectureTypes.ContainsKey(type.FullName) && !type.IsCompilerGenerated)
114+
var assemblyQualifiedName = System.Reflection.Assembly.CreateQualifiedName(
115+
module.Assembly.Name.Name,
116+
typeDefinition.FullName
117+
);
118+
if (
119+
!_architectureTypes.ContainsKey(assemblyQualifiedName)
120+
&& !type.IsCompilerGenerated
121+
)
115122
{
116123
currentTypes.Add(type);
117-
_architectureTypes.Add(type.FullName, type);
124+
_architectureTypes.Add(assemblyQualifiedName, type);
118125
}
119126
});
120127

ArchUnitNET/Loader/TypeFactory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
//
55
// SPDX-License-Identifier: Apache-2.0
66

7-
using System;
87
using System.Collections.Generic;
98
using System.Linq;
10-
using System.Reflection;
119
using System.Runtime.CompilerServices;
1210
using ArchUnitNET.Domain;
1311
using ArchUnitNET.Loader.LoadTasks;

ArchUnitNET/Loader/TypeRegistry.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ public ITypeInstance<IType> GetOrCreateTypeFromTypeReference(
2323
[NotNull] Func<string, ITypeInstance<IType>> createFunc
2424
)
2525
{
26+
var assemblyQualifiedName = System.Reflection.Assembly.CreateQualifiedName(
27+
typeReference.Module.Assembly.FullName,
28+
typeReference.BuildFullName()
29+
);
2630
return RegistryUtils.GetFromDictOrCreateAndAdd(
27-
typeReference.BuildFullName(),
31+
assemblyQualifiedName,
2832
_allTypes,
2933
createFunc
3034
);

ArchUnitNETTests/ArchUnitNETTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" />
1313
<ProjectReference Include="..\TestAssemblies\AttributeAssembly\AttributeAssembly.csproj" />
1414
<ProjectReference Include="..\TestAssemblies\DependencyAssembly\DependencyAssembly.csproj" />
15+
<ProjectReference Include="..\TestAssemblies\LoaderTestAssembly\LoaderTestAssembly.csproj" />
16+
<ProjectReference
17+
Include="..\TestAssemblies\OtherLoaderTestAssembly\OtherLoaderTestAssembly.csproj" />
1518
<ProjectReference Include="..\TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj" />
1619
</ItemGroup>
1720

ArchUnitNETTests/Loader/ArchLoaderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public void LoadAssemblies()
2525
Assert.NotEmpty(ArchUnitNETTestAssemblyArchitectureWithDependencies.Assemblies);
2626
}
2727

28+
[Fact]
29+
public void SameFullNameInMultipleAssemblies()
30+
{
31+
var types = LoaderTestArchitecture.Types.Where(type =>
32+
type.Namespace.FullName == "DuplicateClassAcrossAssemblies"
33+
);
34+
Assert.Equal(2, types.Count());
35+
Assert.Single(types.Where(type => type.Assembly.Name.StartsWith("LoaderTestAssembly")));
36+
Assert.Single(
37+
types.Where(type => type.Assembly.Name.StartsWith("OtherLoaderTestAssembly"))
38+
);
39+
}
40+
2841
[Fact]
2942
public void LoadAssembliesIncludingRecursiveDependencies()
3043
{

ArchUnitNETTests/StaticTestArchitectures.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public static class StaticTestArchitectures
3333
.LoadAssemblies(typeof(TypeDependencyNamespace.BaseClass).Assembly)
3434
.Build();
3535

36+
public static readonly Architecture LoaderTestArchitecture = new ArchLoader()
37+
.LoadAssemblies(
38+
typeof(LoaderTestAssembly.LoaderTestAssembly).Assembly,
39+
typeof(OtherLoaderTestAssembly.OtherLoaderTestAssembly).Assembly
40+
)
41+
.Build();
42+
3643
public static readonly Architecture VisibilityArchitecture = new ArchLoader()
3744
.LoadAssemblies(typeof(VisibilityNamespace.PublicClass).Assembly)
3845
.Build();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace DuplicateClassAcrossAssemblies;
2+
3+
internal class DuplicateClass { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace LoaderTestAssembly;
2+
3+
public class LoaderTestAssembly { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace DuplicateClassAcrossAssemblies;
2+
3+
internal class DuplicateClass { }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace OtherLoaderTestAssembly;
2+
3+
public class OtherLoaderTestAssembly { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)