Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IAST] Removed Activator calls in SourceGenerator for performance reasons #5052

Merged
merged 1 commit into from
Jan 12, 2024
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 @@ -158,7 +158,7 @@ private static IReadOnlyList<string> GetAspectSources(Compilation compilation, I
var arguments = attribute.ConstructorArguments.Select(a => GetArgument(a)).ToArray();
var type = Resolve(attribute);
if (type == null) { return null; }
var res = Activator.CreateInstance(type, arguments);
var res = Helpers.CreateAttributeInstance(type, arguments); // Activator.CreateInstance(type, arguments);
return res;
}
catch (Exception err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Datadog.Trace.Iast;
using Datadog.Trace.Iast.Dataflow;

namespace Datadog.Trace.SourceGenerators.AspectsDefinitions
{
Expand All @@ -23,5 +24,44 @@ internal static class Helpers
Array.Copy(array, destinationArray, array.Length);
return destinationArray;
}

internal static object? CreateAttributeInstance(Type attributeType, object?[] arguments)
{
int x = 0;
if (attributeType == typeof(AspectClassAttribute))
{
if (arguments == null || arguments.Length == 0) { return new AspectClassAttribute(); }
else if (arguments.Length == 1) { return new AspectClassAttribute((string)arguments[x++]!); }
else if (arguments.Length == 3) { return new AspectClassAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
else if (arguments.Length == 4 && arguments[1]?.GetType() == typeof(AspectFilter)) { return new AspectClassAttribute((string)arguments[x++]!, (AspectFilter)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
else if (arguments.Length == 4) { return new AspectClassAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
}
else if (attributeType == typeof(AspectMethodReplaceAttribute))
{
if (arguments.Length == 1) { return new AspectMethodReplaceAttribute((string)arguments[x++]!); }
else if (arguments.Length == 2) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!); }
else if (arguments.Length == 3 && arguments[1]?.GetType() == typeof(string)) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (string)arguments[x++]!, (AspectFilter[])arguments[x++]!); }
else if (arguments.Length == 3) { return new AspectMethodReplaceAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
}
else if (attributeType == typeof(AspectCtorReplaceAttribute))
{
if (arguments.Length == 1) { return new AspectCtorReplaceAttribute((string)arguments[x++]!); }
else if (arguments.Length == 2) { return new AspectCtorReplaceAttribute((string)arguments[x++]!, (AspectFilter[])arguments[x++]!); }
else if (arguments.Length == 3) { return new AspectCtorReplaceAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
}
else if (attributeType == typeof(AspectMethodInsertAfterAttribute))
{
if (arguments.Length == 1) { return new AspectMethodInsertAfterAttribute((string)arguments[x++]!); }
else if (arguments.Length == 3) { return new AspectMethodInsertAfterAttribute((string)arguments[x++]!, (AspectType)arguments[x++]!, (VulnerabilityType[])arguments[x++]!); }
}
else if (attributeType == typeof(AspectMethodInsertBeforeAttribute))
{
if (arguments.Length == 2) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int[])arguments[x++]!); }
else if (arguments.Length == 3 && arguments[1]?.GetType() == typeof(int)) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int)arguments[x++]!, (bool)arguments[x++]!); }
else if (arguments.Length == 3) { return new AspectMethodInsertBeforeAttribute((string)arguments[x++]!, (int[])arguments[x++]!, (bool[])arguments[x++]!); }
}

throw new ArgumentException($"No constructor helper for {attributeType} and {arguments.Length} arguments", "attributeType");
}
}
}
Loading