Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,10 @@ public sealed class DerivedHandler : HandlerBase
generatedSource,
Does.Contain(
"var knownServiceTypes0 = new global::System.Collections.Generic.HashSet<global::System.Type>();"));
Assert.That(
generatedSource,
Does.Contain(
"// Remaining runtime interface discovery target: GFramework.Cqrs.Abstractions.Cqrs.IRequestHandler<Dep.VisibilityScope.ProtectedRequest, Dep.VisibilityScope.ProtectedResponse[]>"));
Assert.That(
generatedSource,
Does.Contain(
Expand Down
46 changes: 41 additions & 5 deletions GFramework.SourceGenerators/Cqrs/CqrsHandlerRegistryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private static bool IsHandlerCandidate(SyntaxNode node)
ImmutableArray.CreateBuilder<ReflectedImplementationRegistrationSpec>(handlerInterfaces.Length);
var preciseReflectedRegistrations =
ImmutableArray.CreateBuilder<PreciseReflectedRegistrationSpec>(handlerInterfaces.Length);
var runtimeDiscoveredHandlerInterfaceLogNames =
ImmutableArray.CreateBuilder<string>(handlerInterfaces.Length);
var requiresRuntimeInterfaceDiscovery = false;
foreach (var handlerInterface in handlerInterfaces)
{
Expand Down Expand Up @@ -128,6 +130,7 @@ private static bool IsHandlerCandidate(SyntaxNode node)
// 对这些边角场景保留“已知接口静态注册 + 剩余接口运行时补洞”的组合路径,
// 避免单个未知接口把同实现上的其它已知注册全部拖回整实现反射发现。
requiresRuntimeInterfaceDiscovery = true;
runtimeDiscoveredHandlerInterfaceLogNames.Add(GetLogDisplayName(handlerInterface));
}

return new HandlerCandidateAnalysis(
Expand All @@ -137,7 +140,8 @@ private static bool IsHandlerCandidate(SyntaxNode node)
reflectedImplementationRegistrations.ToImmutable(),
preciseReflectedRegistrations.ToImmutable(),
canReferenceImplementation ? null : GetReflectionTypeMetadataName(type),
requiresRuntimeInterfaceDiscovery);
requiresRuntimeInterfaceDiscovery,
runtimeDiscoveredHandlerInterfaceLogNames.ToImmutable());
}

private static void Execute(SourceProductionContext context, GenerationEnvironment generationEnvironment,
Expand Down Expand Up @@ -182,7 +186,8 @@ private static List<ImplementationRegistrationSpec> CollectRegistrations(
candidate.ReflectedImplementationRegistrations,
candidate.PreciseReflectedRegistrations,
candidate.ReflectionTypeMetadataName,
candidate.RequiresRuntimeInterfaceDiscovery));
candidate.RequiresRuntimeInterfaceDiscovery,
candidate.RuntimeDiscoveredHandlerInterfaceLogNames));
}

registrations.Sort(static (left, right) =>
Expand Down Expand Up @@ -655,6 +660,13 @@ private static void AppendOrderedImplementationRegistrations(
builder.Append(" var ");
builder.Append(knownServiceTypesVariableName);
builder.AppendLine(" = new global::System.Collections.Generic.HashSet<global::System.Type>();");
foreach (var runtimeDiscoveredHandlerInterfaceLogName in registration
.RuntimeDiscoveredHandlerInterfaceLogNames)
{
builder.Append(" // Remaining runtime interface discovery target: ");
builder.Append(runtimeDiscoveredHandlerInterfaceLogName);
builder.AppendLine();
}
}

foreach (var orderedRegistration in orderedRegistrations)
Expand Down Expand Up @@ -1090,7 +1102,8 @@ private readonly record struct ImplementationRegistrationSpec(
ImmutableArray<ReflectedImplementationRegistrationSpec> ReflectedImplementationRegistrations,
ImmutableArray<PreciseReflectedRegistrationSpec> PreciseReflectedRegistrations,
string? ReflectionTypeMetadataName,
bool RequiresRuntimeInterfaceDiscovery);
bool RequiresRuntimeInterfaceDiscovery,
ImmutableArray<string> RuntimeDiscoveredHandlerInterfaceLogNames);

private readonly struct HandlerCandidateAnalysis : IEquatable<HandlerCandidateAnalysis>
{
Expand All @@ -1101,7 +1114,8 @@ public HandlerCandidateAnalysis(
ImmutableArray<ReflectedImplementationRegistrationSpec> reflectedImplementationRegistrations,
ImmutableArray<PreciseReflectedRegistrationSpec> preciseReflectedRegistrations,
string? reflectionTypeMetadataName,
bool requiresRuntimeInterfaceDiscovery)
bool requiresRuntimeInterfaceDiscovery,
ImmutableArray<string> runtimeDiscoveredHandlerInterfaceLogNames)
{
ImplementationTypeDisplayName = implementationTypeDisplayName;
ImplementationLogName = implementationLogName;
Expand All @@ -1110,6 +1124,7 @@ public HandlerCandidateAnalysis(
PreciseReflectedRegistrations = preciseReflectedRegistrations;
ReflectionTypeMetadataName = reflectionTypeMetadataName;
RequiresRuntimeInterfaceDiscovery = requiresRuntimeInterfaceDiscovery;
RuntimeDiscoveredHandlerInterfaceLogNames = runtimeDiscoveredHandlerInterfaceLogNames;
}

public string ImplementationTypeDisplayName { get; }
Expand All @@ -1126,6 +1141,8 @@ public HandlerCandidateAnalysis(

public bool RequiresRuntimeInterfaceDiscovery { get; }

public ImmutableArray<string> RuntimeDiscoveredHandlerInterfaceLogNames { get; }

public bool Equals(HandlerCandidateAnalysis other)
{
if (!string.Equals(ImplementationTypeDisplayName, other.ImplementationTypeDisplayName,
Expand All @@ -1136,7 +1153,9 @@ public bool Equals(HandlerCandidateAnalysis other)
RequiresRuntimeInterfaceDiscovery != other.RequiresRuntimeInterfaceDiscovery ||
Registrations.Length != other.Registrations.Length ||
ReflectedImplementationRegistrations.Length != other.ReflectedImplementationRegistrations.Length ||
PreciseReflectedRegistrations.Length != other.PreciseReflectedRegistrations.Length)
PreciseReflectedRegistrations.Length != other.PreciseReflectedRegistrations.Length ||
RuntimeDiscoveredHandlerInterfaceLogNames.Length !=
other.RuntimeDiscoveredHandlerInterfaceLogNames.Length)
{
return false;
}
Expand All @@ -1160,6 +1179,17 @@ public bool Equals(HandlerCandidateAnalysis other)
return false;
}

for (var index = 0; index < RuntimeDiscoveredHandlerInterfaceLogNames.Length; index++)
{
if (!string.Equals(
RuntimeDiscoveredHandlerInterfaceLogNames[index],
other.RuntimeDiscoveredHandlerInterfaceLogNames[index],
StringComparison.Ordinal))
{
return false;
}
}

return true;
}

Expand Down Expand Up @@ -1194,6 +1224,12 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ preciseReflectedRegistration.GetHashCode();
}

foreach (var runtimeDiscoveredHandlerInterfaceLogName in RuntimeDiscoveredHandlerInterfaceLogNames)
{
hashCode = (hashCode * 397) ^
StringComparer.Ordinal.GetHashCode(runtimeDiscoveredHandlerInterfaceLogName);
}

return hashCode;
}
}
Expand Down
Loading