| | | 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) |
| | 142 | 25 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 11 | 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 |
| | 131 | 31 | | if (!IsAccessibleFromGeneratedCode(typeSymbol, isCurrentAssembly)) |
| | 0 | 32 | | return false; |
| | | 33 | | |
| | 131 | 34 | | if (typeSymbol.IsAbstract) |
| | 0 | 35 | | return false; |
| | | 36 | | |
| | 131 | 37 | | if (typeSymbol.IsStatic) |
| | 0 | 38 | | return false; |
| | | 39 | | |
| | 131 | 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 |
| | 131 | 45 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 0 | 46 | | return false; |
| | | 47 | | |
| | 131 | 48 | | if (typeSymbol.ContainingType != null) |
| | 0 | 49 | | return false; |
| | | 50 | | |
| | 131 | 51 | | if (IsCompilerGenerated(typeSymbol)) |
| | 0 | 52 | | return false; |
| | | 53 | | |
| | 131 | 54 | | if (InheritsFrom(typeSymbol, "System.Exception")) |
| | 0 | 55 | | return false; |
| | | 56 | | |
| | 131 | 57 | | if (InheritsFrom(typeSymbol, "System.Attribute")) |
| | 105 | 58 | | return false; |
| | | 59 | | |
| | 26 | 60 | | if (typeSymbol.IsRecord) |
| | 0 | 61 | | return false; |
| | | 62 | | |
| | 26 | 63 | | if (HasDoNotAutoRegisterAttribute(typeSymbol)) |
| | 5 | 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" |
| | 21 | 68 | | if (HasUnsatisfiedRequiredMembers(typeSymbol)) |
| | 0 | 69 | | return false; |
| | | 70 | | |
| | 21 | 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 |
| | 84 | 81 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 82 | | { |
| | 21 | 83 | | if (ctor.IsStatic) |
| | | 84 | | continue; |
| | | 85 | | |
| | | 86 | | // If a constructor has [SetsRequiredMembers], it handles all required members |
| | 42 | 87 | | foreach (var attr in ctor.GetAttributes()) |
| | | 88 | | { |
| | 0 | 89 | | if (attr.AttributeClass?.Name == "SetsRequiredMembersAttribute" || |
| | 0 | 90 | | attr.AttributeClass?.ToDisplayString() == "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttrib |
| | | 91 | | { |
| | 0 | 92 | | return false; // This constructor handles required members |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Check for required properties (including inherited) |
| | 21 | 98 | | var currentType = typeSymbol; |
| | 63 | 99 | | while (currentType != null) |
| | | 100 | | { |
| | 504 | 101 | | foreach (var member in currentType.GetMembers()) |
| | | 102 | | { |
| | 210 | 103 | | if (member is IPropertySymbol property && property.IsRequired) |
| | 0 | 104 | | return true; |
| | 210 | 105 | | if (member is IFieldSymbol field && field.IsRequired) |
| | 0 | 106 | | return true; |
| | | 107 | | } |
| | 42 | 108 | | currentType = currentType.BaseType; |
| | | 109 | | } |
| | | 110 | | |
| | 21 | 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 |
| | 26 | 120 | | if (HasDoNotAutoRegisterAttributeDirect(typeSymbol)) |
| | 5 | 121 | | return true; |
| | | 122 | | |
| | | 123 | | // Check all implemented interfaces |
| | 50 | 124 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 125 | | { |
| | 4 | 126 | | if (HasDoNotAutoRegisterAttributeDirect(iface)) |
| | 0 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 21 | 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 | | { |
| | 71 | 138 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 139 | | { |
| | 8 | 140 | | var attributeClass = attribute.AttributeClass; |
| | 8 | 141 | | if (attributeClass == null) |
| | | 142 | | continue; |
| | | 143 | | |
| | 8 | 144 | | var name = attributeClass.Name; |
| | 8 | 145 | | if (name == DoNotAutoRegisterAttributeName || name == DoNotInjectAttributeName) |
| | 5 | 146 | | return true; |
| | | 147 | | |
| | 3 | 148 | | var fullName = attributeClass.ToDisplayString(); |
| | 3 | 149 | | if (fullName == DoNotAutoRegisterAttributeFullName || fullName == DoNotInjectAttributeFullName) |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | |
| | 25 | 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 | | { |
| | 488 | 161 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 162 | | { |
| | 113 | 163 | | var name = attribute.AttributeClass?.ToDisplayString(); |
| | 113 | 164 | | if (name == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") |
| | 0 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | // Also check if the name indicates compiler generation |
| | 131 | 169 | | var typeName = typeSymbol.Name; |
| | 131 | 170 | | if (typeName.StartsWith("<", StringComparison.Ordinal) || |
| | 131 | 171 | | typeName.Contains("__") || |
| | 131 | 172 | | typeName.Contains("<>")) |
| | | 173 | | { |
| | 0 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | |
| | 131 | 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 | | { |
| | 262 | 185 | | var currentType = typeSymbol.BaseType; |
| | 524 | 186 | | while (currentType != null) |
| | | 187 | | { |
| | 367 | 188 | | if (currentType.ToDisplayString() == baseTypeName) |
| | 105 | 189 | | return true; |
| | 262 | 190 | | currentType = currentType.BaseType; |
| | | 191 | | } |
| | 157 | 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 |
| | 131 | 203 | | var accessibility = typeSymbol.DeclaredAccessibility; |
| | | 204 | | |
| | 131 | 205 | | if (isCurrentAssembly) |
| | | 206 | | { |
| | | 207 | | // For current assembly, allow public or internal |
| | 131 | 208 | | if (accessibility != Accessibility.Public && accessibility != Accessibility.Internal) |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | | 213 | | // For referenced assemblies, only public is accessible |
| | 0 | 214 | | if (accessibility != Accessibility.Public) |
| | 0 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Check all containing types (for nested types) |
| | 131 | 219 | | var containingType = typeSymbol.ContainingType; |
| | 131 | 220 | | while (containingType != null) |
| | | 221 | | { |
| | 0 | 222 | | var containingAccessibility = containingType.DeclaredAccessibility; |
| | 0 | 223 | | if (isCurrentAssembly) |
| | | 224 | | { |
| | 0 | 225 | | if (containingAccessibility != Accessibility.Public && containingAccessibility != Accessibility.Internal |
| | 0 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | else |
| | | 229 | | { |
| | 0 | 230 | | if (containingAccessibility != Accessibility.Public) |
| | 0 | 231 | | return false; |
| | | 232 | | } |
| | 0 | 233 | | containingType = containingType.ContainingType; |
| | | 234 | | } |
| | | 235 | | |
| | 131 | 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 | | { |
| | 0 | 244 | | var ns = typeSymbol.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | | 245 | | |
| | | 246 | | // Skip types from system assemblies |
| | 0 | 247 | | if (ns.StartsWith("System", StringComparison.Ordinal)) |
| | 0 | 248 | | return true; |
| | | 249 | | |
| | | 250 | | // Check if from mscorlib or similar |
| | 0 | 251 | | var assembly = typeSymbol.ContainingAssembly; |
| | 0 | 252 | | if (assembly != null) |
| | | 253 | | { |
| | 0 | 254 | | var assemblyName = assembly.Name; |
| | 0 | 255 | | if (assemblyName == "mscorlib" || |
| | 0 | 256 | | assemblyName == "System.Runtime" || |
| | 0 | 257 | | assemblyName == "System.Private.CoreLib" || |
| | 0 | 258 | | assemblyName == "netstandard") |
| | | 259 | | { |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | } |