Skip to content

Commit

Permalink
Check for duplicate add
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Ehrenmüller committed Apr 25, 2022
1 parent 02649f3 commit 701c8bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
46 changes: 25 additions & 21 deletions DeepCopy.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,35 @@ private void AddDeepCopyConstructors(IEnumerable<TypeDefinition> targets)
foreach (var target in targets)
Run(target, () =>
{
if (target.HasCopyConstructor(out var constructor))
var hasAttribute = target.TryRemove(DeepCopyAttribute.AddDeepCopyConstructor, out var attribute);
if (!target.HasCopyConstructor(out var constructor))
{
if (target.Has(DeepCopyAttribute.AddDeepCopyConstructor, out var attribute)
&& attribute.GetArgument("Overwrite", false)
|| OverwriteByDefault)
{
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
}
else if (!constructor.Resolve().IsPublic)
{
if (!OverwriteByDefault)
WriteWarning($"Non-public constructor for {target.FullName} will be overwritten");
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
}
else
throw new WeavingException(@"Type already has a copy constructor
- Use [DeepCopyConstructor] on constructor to inject deep copy code
- Use [AddDeepCopyConstructor(Overwrite=true)] on type to replace existing constructor
- Set global config <DeepCopy OverwriteByDefault=""True"" /> in FodyWeavers.xml");
AddDeepConstructor(target, null);
return;
}
else

var constructorResolved = constructor.Resolve();
if (constructorResolved.Has(DeepCopyAttribute.DeepCopyConstructor)
|| constructorResolved.Has(DeepCopyAttribute.InjectDeepCopy))
return;

if (hasAttribute
&& attribute.GetArgument("Overwrite", false)
|| OverwriteByDefault)
{
AddDeepConstructor(target, null);
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
}
else if (!constructorResolved.IsPublic)
{
if (!OverwriteByDefault)
WriteWarning($"Non-public constructor for {target.FullName} will be overwritten");
AddDeepConstructor(target, ModuleDefinition.ImportReference(constructor).Resolve());
}
target.TryRemove(DeepCopyAttribute.AddDeepCopyConstructor);
else
throw new WeavingException(@"Type already has a copy constructor
- Use [DeepCopyConstructor] on constructor to inject deep copy code
- Use [AddDeepCopyConstructor(Overwrite=true)] on type to replace existing constructor
- Set global config <DeepCopy OverwriteByDefault=""True"" /> in FodyWeavers.xml");
});
}

Expand Down
7 changes: 6 additions & 1 deletion DeepCopy.Fody/Utils/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public static bool Has(this ICustomAttributeProvider attributeProvider, DeepCopy

public static bool TryRemove(this ICustomAttributeProvider attributeProvider, DeepCopyAttribute name)
{
if (!attributeProvider.Has(name, out var attribute))
return TryRemove(attributeProvider, name, out _);
}

public static bool TryRemove(this ICustomAttributeProvider attributeProvider, DeepCopyAttribute name, out CustomAttribute attribute)
{
if (!attributeProvider.Has(name, out attribute))
return false;
attributeProvider.CustomAttributes.Remove(attribute);
return true;
Expand Down

0 comments on commit 701c8bf

Please sign in to comment.