Skip to content

Commit 226758e

Browse files
authored
Merge pull request #137 from brandhuf/fix-structs
Create distinct types for structs and enums
2 parents 7718249 + a5d9194 commit 226758e

35 files changed

+586
-374
lines changed

ArchUnitNET/Domain/Architecture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10-
using ArchUnitNET.Fluent;
1110

1211
namespace ArchUnitNET.Domain
1312
{
@@ -36,6 +35,8 @@ public Architecture(IEnumerable<Assembly> allAssemblies, IEnumerable<Namespace>
3635
public IEnumerable<Class> Classes => Types.OfType<Class>();
3736
public IEnumerable<Interface> Interfaces => Types.OfType<Interface>();
3837
public IEnumerable<Attribute> Attributes => Types.OfType<Attribute>();
38+
public IEnumerable<Struct> Structs => Types.OfType<Struct>();
39+
public IEnumerable<Enum> Enums => Types.OfType<Enum>();
3940
public IEnumerable<Class> ReferencedClasses => ReferencedTypes.OfType<Class>();
4041
public IEnumerable<Interface> ReferencedInterfaces => ReferencedTypes.OfType<Interface>();
4142
public IEnumerable<Attribute> ReferencedAttributes => ReferencedTypes.OfType<Attribute>();
@@ -62,7 +63,7 @@ public override bool Equals(object obj)
6263
return true;
6364
}
6465

65-
return obj.GetType() == GetType() && Equals((Architecture) obj);
66+
return obj.GetType() == GetType() && Equals((Architecture)obj);
6667
}
6768

6869
private bool Equals(Architecture other)

ArchUnitNET/Domain/Attribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace ArchUnitNET.Domain
99
public class Attribute : Class
1010
{
1111
public Attribute(IType type, bool? isAbstract, bool? isSealed) : base(type, isAbstract,
12-
isSealed, false, false)
12+
isSealed)
1313
{
1414
}
1515

16-
public Attribute(Class cls) : base(cls.Type, cls.IsAbstract, cls.IsSealed, cls.IsValueType, cls.IsEnum)
16+
public Attribute(Class cls) : base(cls.Type, cls.IsAbstract, cls.IsSealed)
1717
{
1818
}
1919
}

ArchUnitNET/Domain/Class.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ namespace ArchUnitNET.Domain
1414
{
1515
public class Class : IType
1616
{
17-
public Class(IType type, bool? isAbstract = null, bool? isSealed = null, bool? isValueType = null,
18-
bool? isEnum = null)
17+
public Class(IType type, bool? isAbstract = null, bool? isSealed = null)
1918
{
2019
Type = type;
2120
IsAbstract = isAbstract;
2221
IsSealed = isSealed;
23-
IsValueType = isValueType;
24-
IsEnum = isEnum;
2522
}
2623

2724
public IType Type { get; }
@@ -42,21 +39,6 @@ public Class(IType type, bool? isAbstract = null, bool? isSealed = null, bool? i
4239
public IEnumerable<MethodMember> Constructors => Type.GetConstructors();
4340
public bool? IsAbstract { get; }
4441
public bool? IsSealed { get; }
45-
public bool? IsValueType { get; }
46-
public bool? IsEnum { get; }
47-
48-
public bool? IsStruct
49-
{
50-
get
51-
{
52-
if (IsValueType.HasValue && IsEnum.HasValue)
53-
{
54-
return IsValueType.Value && !IsEnum.Value;
55-
}
56-
57-
return null;
58-
}
59-
}
6042

6143
public IEnumerable<Class> InheritedClasses => BaseClass == null
6244
? Enumerable.Empty<Class>()
@@ -114,8 +96,7 @@ public override string ToString()
11496
private bool Equals(Class other)
11597
{
11698
return Equals(Type, other.Type) && Equals(IsAbstract, other.IsAbstract) &&
117-
Equals(IsSealed, other.IsSealed) && Equals(IsValueType, other.IsValueType) &&
118-
Equals(IsEnum, other.IsEnum);
99+
Equals(IsSealed, other.IsSealed);
119100
}
120101

121102
public override bool Equals(object obj)
@@ -140,8 +121,6 @@ public override int GetHashCode()
140121
var hashCode = Type != null ? Type.GetHashCode() : 0;
141122
hashCode = (hashCode * 397) ^ IsAbstract.GetHashCode();
142123
hashCode = (hashCode * 397) ^ IsSealed.GetHashCode();
143-
hashCode = (hashCode * 397) ^ IsValueType.GetHashCode();
144-
hashCode = (hashCode * 397) ^ IsEnum.GetHashCode();
145124
return hashCode;
146125
}
147126
}

ArchUnitNET/Domain/Dependencies/InheritsBaseClassDependency.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace ArchUnitNET.Domain.Dependencies
88
{
99
public class InheritsBaseClassDependency : TypeInstanceDependency
1010
{
11-
// ReSharper disable SuggestBaseTypeForParameter
12-
public InheritsBaseClassDependency(Class origin, ITypeInstance<Class> targetInstance)
11+
public InheritsBaseClassDependency(IType origin, ITypeInstance<IType> targetInstance)
1312
: base(origin, targetInstance)
1413
{
1514
}

ArchUnitNET/Domain/Enum.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2019 Florian Gather <[email protected]>
2+
// Copyright 2019 Paula Ruiz <[email protected]>
3+
// Copyright 2019 Fritz Brandhuber <[email protected]>
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using ArchUnitNET.Domain.Dependencies;
10+
using ArchUnitNET.Domain.Extensions;
11+
using JetBrains.Annotations;
12+
13+
namespace ArchUnitNET.Domain
14+
{
15+
public class Enum : IType
16+
{
17+
public Enum(IType type)
18+
{
19+
Type = type;
20+
}
21+
22+
public IType Type { get; }
23+
public string Name => Type.Name;
24+
public string FullName => Type.FullName;
25+
26+
[CanBeNull]
27+
public Class BaseClass =>
28+
(Class)Dependencies.OfType<InheritsBaseClassDependency>().FirstOrDefault()?.Target;
29+
30+
public IEnumerable<Class> InheritedClasses => BaseClass == null
31+
? Enumerable.Empty<Class>()
32+
: BaseClass.InheritedClasses.Concat(new[] { BaseClass });
33+
34+
public Visibility Visibility => Type.Visibility;
35+
public bool IsNested => Type.IsNested;
36+
public bool IsGeneric => Type.IsGeneric;
37+
public bool IsGenericParameter => Type.IsGenericParameter;
38+
public bool IsStub => Type.IsStub;
39+
public bool IsCompilerGenerated => Type.IsCompilerGenerated;
40+
41+
public Namespace Namespace => Type.Namespace;
42+
public Assembly Assembly => Type.Assembly;
43+
44+
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
45+
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;
46+
47+
public List<ITypeDependency> Dependencies => Type.Dependencies;
48+
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
49+
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;
50+
51+
public MemberList Members => Type.Members;
52+
public List<GenericParameter> GenericParameters => Type.GenericParameters;
53+
54+
public bool ImplementsInterface(Interface intf)
55+
{
56+
return Type.ImplementsInterface(intf);
57+
}
58+
59+
public bool ImplementsInterface(string pattern, bool useRegularExpressions = false)
60+
{
61+
return Type.ImplementsInterface(pattern, useRegularExpressions);
62+
}
63+
64+
public bool IsAssignableTo(IType assignableToType)
65+
{
66+
return this.GetAssignableTypes().Contains(assignableToType);
67+
}
68+
69+
public bool IsAssignableTo(string pattern, bool useRegularExpressions = false)
70+
{
71+
return pattern != null && this.GetAssignableTypes()
72+
.Any(type => type.FullNameMatches(pattern, useRegularExpressions));
73+
}
74+
75+
public override string ToString()
76+
{
77+
return FullName;
78+
}
79+
80+
private bool Equals(Enum other)
81+
{
82+
return Equals(Type, other.Type);
83+
}
84+
85+
public override bool Equals(object obj)
86+
{
87+
if (ReferenceEquals(null, obj))
88+
{
89+
return false;
90+
}
91+
92+
if (ReferenceEquals(this, obj))
93+
{
94+
return true;
95+
}
96+
97+
return obj.GetType() == GetType() && Equals((Enum)obj);
98+
}
99+
100+
public override int GetHashCode()
101+
{
102+
return Type != null ? Type.GetHashCode() : 0;
103+
}
104+
}
105+
}

ArchUnitNET/Domain/Extensions/TypeExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public static IEnumerable<IType> GetAssignableTypes(this IType type)
3131
return intf.ImplementedInterfaces.Concat(new[] {intf});
3232
case Class cls:
3333
return cls.InheritedClasses.Concat(new[] {cls}).Concat(cls.ImplementedInterfaces);
34+
case Struct str:
35+
return str.InheritedClasses.Concat(new IType[] {str}).Concat(str.ImplementedInterfaces);
36+
case Enum en:
37+
return en.InheritedClasses.Concat(new IType[] {en}).Concat(en.ImplementedInterfaces);
3438
default:
3539
return Enumerable.Empty<IType>();
3640
}

ArchUnitNET/Domain/Struct.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2019 Florian Gather <[email protected]>
2+
// Copyright 2019 Paula Ruiz <[email protected]>
3+
// Copyright 2019 Fritz Brandhuber <[email protected]>
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using ArchUnitNET.Domain.Dependencies;
10+
using ArchUnitNET.Domain.Extensions;
11+
using JetBrains.Annotations;
12+
13+
namespace ArchUnitNET.Domain
14+
{
15+
public class Struct : IType
16+
{
17+
public Struct(IType type)
18+
{
19+
Type = type;
20+
}
21+
22+
public IType Type { get; }
23+
public string Name => Type.Name;
24+
public string FullName => Type.FullName;
25+
26+
[CanBeNull]
27+
public Class BaseClass =>
28+
(Class) Dependencies.OfType<InheritsBaseClassDependency>().FirstOrDefault()?.Target;
29+
30+
public IEnumerable<Class> InheritedClasses => BaseClass == null
31+
? Enumerable.Empty<Class>()
32+
: BaseClass.InheritedClasses.Concat(new[] {BaseClass});
33+
34+
public Visibility Visibility => Type.Visibility;
35+
public bool IsNested => Type.IsNested;
36+
public bool IsGeneric => Type.IsGeneric;
37+
public bool IsGenericParameter => Type.IsGenericParameter;
38+
public bool IsStub => Type.IsStub;
39+
public bool IsCompilerGenerated => Type.IsCompilerGenerated;
40+
41+
public Namespace Namespace => Type.Namespace;
42+
public Assembly Assembly => Type.Assembly;
43+
44+
public IEnumerable<Attribute> Attributes => AttributeInstances.Select(instance => instance.Type);
45+
public List<AttributeInstance> AttributeInstances => Type.AttributeInstances;
46+
47+
public List<ITypeDependency> Dependencies => Type.Dependencies;
48+
public List<ITypeDependency> BackwardsDependencies => Type.BackwardsDependencies;
49+
public IEnumerable<IType> ImplementedInterfaces => Type.ImplementedInterfaces;
50+
51+
public MemberList Members => Type.Members;
52+
public List<GenericParameter> GenericParameters => Type.GenericParameters;
53+
54+
public bool ImplementsInterface(Interface intf)
55+
{
56+
return Type.ImplementsInterface(intf);
57+
}
58+
59+
public bool ImplementsInterface(string pattern, bool useRegularExpressions = false)
60+
{
61+
return Type.ImplementsInterface(pattern, useRegularExpressions);
62+
}
63+
64+
public bool IsAssignableTo(IType assignableToType)
65+
{
66+
return this.GetAssignableTypes().Contains(assignableToType);
67+
}
68+
69+
public bool IsAssignableTo(string pattern, bool useRegularExpressions = false)
70+
{
71+
return pattern != null && this.GetAssignableTypes()
72+
.Any(type => type.FullNameMatches(pattern, useRegularExpressions));
73+
}
74+
75+
public override string ToString()
76+
{
77+
return FullName;
78+
}
79+
80+
private bool Equals(Struct other)
81+
{
82+
return Equals(Type, other.Type);
83+
}
84+
85+
public override bool Equals(object obj)
86+
{
87+
if (ReferenceEquals(null, obj))
88+
{
89+
return false;
90+
}
91+
92+
if (ReferenceEquals(this, obj))
93+
{
94+
return true;
95+
}
96+
97+
return obj.GetType() == GetType() && Equals((Struct) obj);
98+
}
99+
100+
public override int GetHashCode()
101+
{
102+
return Type != null ? Type.GetHashCode() : 0;
103+
}
104+
}
105+
}

ArchUnitNET/Fluent/Syntax/Elements/Types/Classes/ClassConditionsDefinition.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ public static ICondition<Class> BeSealed()
2323
"is not sealed");
2424
}
2525

26-
public static ICondition<Class> BeValueTypes()
27-
{
28-
return new SimpleCondition<Class>(cls => !cls.IsValueType.HasValue || cls.IsValueType.Value,
29-
"be value types", "is no value type");
30-
}
31-
32-
public static ICondition<Class> BeEnums()
33-
{
34-
return new SimpleCondition<Class>(cls => !cls.IsEnum.HasValue || cls.IsEnum.Value, "be enums",
35-
"is no enum");
36-
}
37-
38-
public static ICondition<Class> BeStructs()
39-
{
40-
return new SimpleCondition<Class>(cls => !cls.IsStruct.HasValue || cls.IsStruct.Value, "be structs",
41-
"is no struct");
42-
}
43-
4426

4527
//Negations
4628

@@ -56,23 +38,5 @@ public static ICondition<Class> NotBeSealed()
5638
return new SimpleCondition<Class>(cls => !cls.IsSealed.HasValue || !cls.IsSealed.Value, "not be sealed",
5739
"is sealed");
5840
}
59-
60-
public static ICondition<Class> NotBeValueTypes()
61-
{
62-
return new SimpleCondition<Class>(cls => !cls.IsValueType.HasValue || !cls.IsValueType.Value,
63-
"not be value types", "is a value type");
64-
}
65-
66-
public static ICondition<Class> NotBeEnums()
67-
{
68-
return new SimpleCondition<Class>(cls => !cls.IsEnum.HasValue || !cls.IsEnum.Value, "not be enums",
69-
"is an enum");
70-
}
71-
72-
public static ICondition<Class> NotBeStructs()
73-
{
74-
return new SimpleCondition<Class>(cls => !cls.IsStruct.HasValue || !cls.IsStruct.Value, "not be structs",
75-
"is a struct");
76-
}
7741
}
7842
}

0 commit comments

Comments
 (0)