| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Roslyn.Shared; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Shared helper utilities for discovering injectable types from Roslyn symbols. |
| | | 7 | | /// Used by both Generators and Analyzers to ensure consistent type discovery logic. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class TypeDiscoveryHelper |
| | | 10 | | { |
| | | 11 | | private const string DoNotAutoRegisterAttributeName = "DoNotAutoRegisterAttribute"; |
| | | 12 | | private const string DoNotAutoRegisterAttributeFullName = "NexusLabs.Needlr.DoNotAutoRegisterAttribute"; |
| | | 13 | | private const string DoNotInjectAttributeName = "DoNotInjectAttribute"; |
| | | 14 | | private const string DoNotInjectAttributeFullName = "NexusLabs.Needlr.DoNotInjectAttribute"; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Determines whether a type symbol represents a concrete injectable type. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 20 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly (allows internal typ |
| | | 21 | | /// <returns>True if the type is a valid injectable type; otherwise, false.</returns> |
| | | 22 | | public static bool IsInjectableType(INamedTypeSymbol typeSymbol, bool isCurrentAssembly = false) |
| | | 23 | | { |
| | | 24 | | // Must be a class (not interface, struct, enum, delegate) |
| | 1420271 | 25 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 532324 | 26 | | return false; |
| | | 27 | | |
| | | 28 | | // Must be accessible from generated code |
| | | 29 | | // - Current assembly: internal and public types are accessible |
| | | 30 | | // - Referenced assemblies: only public types are accessible |
| | 887947 | 31 | | if (!IsAccessibleFromGeneratedCode(typeSymbol, isCurrentAssembly)) |
| | 0 | 32 | | return false; |
| | | 33 | | |
| | 887947 | 34 | | if (typeSymbol.IsAbstract) |
| | 136105 | 35 | | return false; |
| | | 36 | | |
| | 751842 | 37 | | if (typeSymbol.IsStatic) |
| | 86577 | 38 | | return false; |
| | | 39 | | |
| | 665265 | 40 | | if (typeSymbol.IsUnboundGenericType) |
| | 0 | 41 | | return false; |
| | | 42 | | |
| | | 43 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | | 44 | | // These cannot be instantiated directly and would produce invalid typeof() expressions |
| | 665265 | 45 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 39425 | 46 | | return false; |
| | | 47 | | |
| | 625840 | 48 | | if (typeSymbol.ContainingType != null) |
| | 1 | 49 | | return false; |
| | | 50 | | |
| | 625839 | 51 | | if (IsCompilerGenerated(typeSymbol)) |
| | 0 | 52 | | return false; |
| | | 53 | | |
| | 625839 | 54 | | if (InheritsFrom(typeSymbol, "System.Exception")) |
| | 67681 | 55 | | return false; |
| | | 56 | | |
| | 558158 | 57 | | if (InheritsFrom(typeSymbol, "System.Attribute")) |
| | 155183 | 58 | | return false; |
| | | 59 | | |
| | 402975 | 60 | | if (typeSymbol.IsRecord) |
| | 2758 | 61 | | return false; |
| | | 62 | | |
| | 400217 | 63 | | if (HasDoNotAutoRegisterAttribute(typeSymbol)) |
| | 15 | 64 | | return false; |
| | | 65 | | |
| | | 66 | | // Exclude types with required members that can't be set via constructor |
| | | 67 | | // These would cause compilation errors: "Required member 'X' must be set" |
| | 400202 | 68 | | if (HasUnsatisfiedRequiredMembers(typeSymbol)) |
| | 3 | 69 | | return false; |
| | | 70 | | |
| | 400199 | 71 | | return true; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Checks if a type has required members that aren't satisfied by any constructor |
| | | 76 | | /// with [SetsRequiredMembers] attribute. |
| | | 77 | | /// </summary> |
| | | 78 | | public static bool HasUnsatisfiedRequiredMembers(INamedTypeSymbol typeSymbol) |
| | | 79 | | { |
| | | 80 | | // Check if any constructor has [SetsRequiredMembers] attribute |
| | 4408389 | 81 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 82 | | { |
| | 1408694 | 83 | | if (ctor.IsStatic) |
| | | 84 | | continue; |
| | | 85 | | |
| | | 86 | | // If a constructor has [SetsRequiredMembers], it handles all required members |
| | 3961931 | 87 | | foreach (var attr in ctor.GetAttributes()) |
| | | 88 | | { |
| | 572272 | 89 | | if (attr.AttributeClass?.Name == "SetsRequiredMembersAttribute" || |
| | 572272 | 90 | | attr.AttributeClass?.ToDisplayString() == "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttrib |
| | | 91 | | { |
| | 1 | 92 | | return false; // This constructor handles required members |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Check for required properties (including inherited) |
| | 795500 | 98 | | var currentType = typeSymbol; |
| | 3177289 | 99 | | while (currentType != null) |
| | | 100 | | { |
| | 91519197 | 101 | | foreach (var member in currentType.GetMembers()) |
| | | 102 | | { |
| | 43377807 | 103 | | if (member is IPropertySymbol property && property.IsRequired) |
| | 4 | 104 | | return true; |
| | 43377803 | 105 | | if (member is IFieldSymbol field && field.IsRequired) |
| | 1 | 106 | | return true; |
| | | 107 | | } |
| | 2381789 | 108 | | currentType = currentType.BaseType; |
| | | 109 | | } |
| | | 110 | | |
| | 795495 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Checks if a type has the [DoNotAutoRegister] attribute (directly or on interfaces). |
| | | 116 | | /// </summary> |
| | | 117 | | public static bool HasDoNotAutoRegisterAttribute(INamedTypeSymbol typeSymbol) |
| | | 118 | | { |
| | | 119 | | // Check the type itself |
| | 404004 | 120 | | if (HasDoNotAutoRegisterAttributeDirect(typeSymbol)) |
| | 3 | 121 | | return true; |
| | | 122 | | |
| | | 123 | | // Check all implemented interfaces |
| | 1508732 | 124 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 125 | | { |
| | 350372 | 126 | | if (HasDoNotAutoRegisterAttributeDirect(iface)) |
| | 14 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 403987 | 130 | | return false; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Checks if a type has the [DoNotAutoRegister] or [DoNotInject] attribute directly applied. |
| | | 135 | | /// </summary> |
| | | 136 | | public static bool HasDoNotAutoRegisterAttributeDirect(INamedTypeSymbol typeSymbol) |
| | | 137 | | { |
| | 10085691 | 138 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 139 | | { |
| | 3141035 | 140 | | var attributeClass = attribute.AttributeClass; |
| | 3141035 | 141 | | if (attributeClass == null) |
| | | 142 | | continue; |
| | | 143 | | |
| | 3141035 | 144 | | var name = attributeClass.Name; |
| | 3141035 | 145 | | if (name == DoNotAutoRegisterAttributeName || name == DoNotInjectAttributeName) |
| | 23 | 146 | | return true; |
| | | 147 | | |
| | 3141012 | 148 | | var fullName = attributeClass.ToDisplayString(); |
| | 3141012 | 149 | | if (fullName == DoNotAutoRegisterAttributeFullName || fullName == DoNotInjectAttributeFullName) |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | |
| | 1901799 | 153 | | return false; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Checks if a type is compiler-generated. |
| | | 158 | | /// </summary> |
| | | 159 | | public static bool IsCompilerGenerated(INamedTypeSymbol typeSymbol) |
| | | 160 | | { |
| | 8154534 | 161 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 162 | | { |
| | 2630139 | 163 | | var name = attribute.AttributeClass?.ToDisplayString(); |
| | 2630139 | 164 | | if (name == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") |
| | 2058 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | // Also check if the name indicates compiler generation |
| | 1446099 | 169 | | var typeName = typeSymbol.Name; |
| | 1446099 | 170 | | if (typeName.StartsWith("<", StringComparison.Ordinal) || |
| | 1446099 | 171 | | typeName.Contains("__") || |
| | 1446099 | 172 | | typeName.Contains("<>")) |
| | | 173 | | { |
| | 63894 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | |
| | 1382205 | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Checks if a type inherits from a base type by name. |
| | | 182 | | /// </summary> |
| | | 183 | | public static bool InheritsFrom(INamedTypeSymbol typeSymbol, string baseTypeName) |
| | | 184 | | { |
| | 1193079 | 185 | | var currentType = typeSymbol.BaseType; |
| | 3130553 | 186 | | while (currentType != null) |
| | | 187 | | { |
| | 2161092 | 188 | | if (currentType.ToDisplayString() == baseTypeName) |
| | 223618 | 189 | | return true; |
| | 1937474 | 190 | | currentType = currentType.BaseType; |
| | | 191 | | } |
| | 969461 | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Checks if a type is accessible from generated code. |
| | | 197 | | /// For types in the current assembly, internal and public types are accessible. |
| | | 198 | | /// For types in referenced assemblies, only public types are accessible. |
| | | 199 | | /// </summary> |
| | | 200 | | public static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol typeSymbol, bool isCurrentAssembly) |
| | | 201 | | { |
| | | 202 | | // Check the type itself |
| | 1775882 | 203 | | var accessibility = typeSymbol.DeclaredAccessibility; |
| | | 204 | | |
| | 1775882 | 205 | | if (isCurrentAssembly) |
| | | 206 | | { |
| | | 207 | | // For current assembly, allow public or internal |
| | 1210 | 208 | | if (accessibility != Accessibility.Public && accessibility != Accessibility.Internal) |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | | 213 | | // For referenced assemblies, only public is accessible |
| | 1774672 | 214 | | if (accessibility != Accessibility.Public) |
| | 0 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Check all containing types (for nested types) |
| | 1775882 | 219 | | var containingType = typeSymbol.ContainingType; |
| | 1775883 | 220 | | while (containingType != null) |
| | | 221 | | { |
| | 1 | 222 | | var containingAccessibility = containingType.DeclaredAccessibility; |
| | 1 | 223 | | if (isCurrentAssembly) |
| | | 224 | | { |
| | 0 | 225 | | if (containingAccessibility != Accessibility.Public && containingAccessibility != Accessibility.Internal |
| | 0 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | else |
| | | 229 | | { |
| | 1 | 230 | | if (containingAccessibility != Accessibility.Public) |
| | 0 | 231 | | return false; |
| | | 232 | | } |
| | 1 | 233 | | containingType = containingType.ContainingType; |
| | | 234 | | } |
| | | 235 | | |
| | 1775882 | 236 | | return true; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Checks if a type is from the System namespace or system assemblies. |
| | | 241 | | /// </summary> |
| | | 242 | | public static bool IsSystemType(INamedTypeSymbol typeSymbol) |
| | | 243 | | { |
| | 896786 | 244 | | var ns = typeSymbol.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | | 245 | | |
| | | 246 | | // Skip types from system assemblies |
| | 896786 | 247 | | if (ns.StartsWith("System", StringComparison.Ordinal)) |
| | 890913 | 248 | | return true; |
| | | 249 | | |
| | | 250 | | // Check if from mscorlib or similar |
| | 5873 | 251 | | var assembly = typeSymbol.ContainingAssembly; |
| | 5873 | 252 | | if (assembly != null) |
| | | 253 | | { |
| | 5873 | 254 | | var assemblyName = assembly.Name; |
| | 5873 | 255 | | if (assemblyName == "mscorlib" || |
| | 5873 | 256 | | assemblyName == "System.Runtime" || |
| | 5873 | 257 | | assemblyName == "System.Private.CoreLib" || |
| | 5873 | 258 | | assemblyName == "netstandard") |
| | | 259 | | { |
| | 4147 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 1726 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | } |