From d9528b98dfdbe4d0d33b3180bdcd6e6528f0071a Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Sat, 7 Dec 2024 04:45:35 +0900 Subject: [PATCH] Remove ability to cache interfaces --- .../DependenciesClassCandidate.cs | 8 ---- .../Reflection/CachedAttributeTest.cs | 45 ------------------- .../SourceGeneration/CachedAttributeTest.cs | 45 ------------------- osu.Framework/Allocation/CachedAttribute.cs | 18 ++------ .../Markdown/Footnotes/MarkdownFootnote.cs | 2 + .../Markdown/IMarkdownTextComponent.cs | 2 - .../Markdown/IMarkdownTextFlowComponent.cs | 5 +-- .../Containers/Markdown/MarkdownCodeBlock.cs | 1 + .../Containers/Markdown/MarkdownContainer.cs | 2 + .../Containers/Markdown/MarkdownHeading.cs | 1 + .../Containers/Markdown/MarkdownLinkText.cs | 1 + .../Containers/Markdown/MarkdownParagraph.cs | 1 + .../Containers/Markdown/MarkdownQuoteBlock.cs | 1 + .../Containers/Markdown/MarkdownTableCell.cs | 1 + .../Markdown/MarkdownTextFlowContainer.cs | 1 + .../Markdown/NotImplementedMarkdown.cs | 1 + 16 files changed, 16 insertions(+), 119 deletions(-) diff --git a/osu.Framework.SourceGeneration/Generators/Dependencies/DependenciesClassCandidate.cs b/osu.Framework.SourceGeneration/Generators/Dependencies/DependenciesClassCandidate.cs index ffec483350..2cd53e90a9 100644 --- a/osu.Framework.SourceGeneration/Generators/Dependencies/DependenciesClassCandidate.cs +++ b/osu.Framework.SourceGeneration/Generators/Dependencies/DependenciesClassCandidate.cs @@ -28,14 +28,6 @@ public DependenciesClassCandidate(ClassDeclarationSyntax classSyntax, SemanticMo protected override void Process(INamedTypeSymbol symbol) { - // Process any [Cached] attributes on any interface on the class excluding base types. - foreach (var iFace in SyntaxHelpers.GetDeclaredInterfacesOnType(symbol)) - { - // Add an entry if this interface has a cached attribute. - foreach (var attrib in iFace.GetAttributes().Where(SyntaxHelpers.IsCachedAttribute)) - CachedInterfaces.Add(CachedAttributeData.FromInterfaceOrClass(iFace, attrib)); - } - // Process any [Cached] attributes on the class. foreach (var attrib in symbol.GetAttributes().Where(SyntaxHelpers.IsCachedAttribute)) CachedClasses.Add(CachedAttributeData.FromInterfaceOrClass(symbol, attrib)); diff --git a/osu.Framework.Tests/Dependencies/Reflection/CachedAttributeTest.cs b/osu.Framework.Tests/Dependencies/Reflection/CachedAttributeTest.cs index ee6de70e59..0fce9d8b43 100644 --- a/osu.Framework.Tests/Dependencies/Reflection/CachedAttributeTest.cs +++ b/osu.Framework.Tests/Dependencies/Reflection/CachedAttributeTest.cs @@ -272,17 +272,6 @@ public void TestCacheWithNonAutoGetter() Assert.Throws(() => DependencyActivator.MergeDependencies(provider, new DependencyContainer())); } - [Test] - public void TestCachedViaInterface() - { - var provider = new Provider25(); - - var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); - - Assert.IsNotNull(dependencies.Get()); - Assert.IsNotNull(dependencies.Get()); - } - [Test] public void TestInheritancePreservesCachingViaBaseType() { @@ -294,18 +283,6 @@ public void TestInheritancePreservesCachingViaBaseType() Assert.IsNull(dependencies.Get()); } - [Test] - public void TestImplementationOfDerivedInterfacePreservesCaching() - { - var provider = new Provider27(); - - var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); - - Assert.AreEqual(provider, dependencies.Get()); - Assert.IsNull(dependencies.Get()); - Assert.IsNull(dependencies.Get()); - } - private interface IProvidedInterface1 { } @@ -483,30 +460,8 @@ private class Provider24 : IDependencyInjectionCandidate public object Provided1 => null; } - private class Provider25 : IProviderInterface3 - { - } - private class Provider26 : Provider1 { } - - private class Provider27 : IProviderInterface4 - { - } - - [Cached] - private interface IProviderInterface3 : IProviderInterface2 - { - } - - [Cached] - private interface IProviderInterface2 : IDependencyInjectionCandidate - { - } - - private interface IProviderInterface4 : IProviderInterface2 - { - } } } diff --git a/osu.Framework.Tests/Dependencies/SourceGeneration/CachedAttributeTest.cs b/osu.Framework.Tests/Dependencies/SourceGeneration/CachedAttributeTest.cs index ac928cc1e9..aec7ddb46d 100644 --- a/osu.Framework.Tests/Dependencies/SourceGeneration/CachedAttributeTest.cs +++ b/osu.Framework.Tests/Dependencies/SourceGeneration/CachedAttributeTest.cs @@ -268,17 +268,6 @@ public void TestCacheWithNonAutoGetter() Assert.Throws(() => DependencyActivator.MergeDependencies(provider, new DependencyContainer())); } - [Test] - public void TestCachedViaInterface() - { - var provider = new Provider25(); - - var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); - - Assert.IsNotNull(dependencies.Get()); - Assert.IsNotNull(dependencies.Get()); - } - [Test] public void TestInheritancePreservesCachingViaBaseType() { @@ -290,18 +279,6 @@ public void TestInheritancePreservesCachingViaBaseType() Assert.IsNull(dependencies.Get()); } - [Test] - public void TestImplementationOfDerivedInterfacePreservesCaching() - { - var provider = new Provider27(); - - var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); - - Assert.AreEqual(provider, dependencies.Get()); - Assert.IsNull(dependencies.Get()); - Assert.IsNull(dependencies.Get()); - } - private interface IProvidedInterface1 { } @@ -469,30 +446,8 @@ private partial class Provider24 : IDependencyInjectionCandidate public object Provided1 => null; } - private partial class Provider25 : IProviderInterface3 - { - } - private partial class Provider26 : Provider1 { } - - private partial class Provider27 : IProviderInterface4 - { - } - - [Cached] - private interface IProviderInterface3 : IProviderInterface2 - { - } - - [Cached] - private interface IProviderInterface2 : IDependencyInjectionCandidate - { - } - - private interface IProviderInterface4 : IProviderInterface2 - { - } } } diff --git a/osu.Framework/Allocation/CachedAttribute.cs b/osu.Framework/Allocation/CachedAttribute.cs index 07228c1dde..401eef0784 100644 --- a/osu.Framework/Allocation/CachedAttribute.cs +++ b/osu.Framework/Allocation/CachedAttribute.cs @@ -17,7 +17,7 @@ namespace osu.Framework.Allocation { /// - /// An attribute that may be attached to a class, interface, field, or property definitions of a + /// An attribute that may be attached to a class, field, or property definitions of a /// to indicate that the value should be cached as a dependency. /// Cached values may be resolved through or . /// @@ -43,15 +43,10 @@ namespace osu.Framework.Allocation /// See the examples section of the property documentation for further information. /// /// - /// - /// If a class implements an interface annotated with , then instances of that class will cache themselves for their own children using the interface type. - /// As with classes, the is not inherited between interfaces either, - /// but an instance of a class will cache itself to children using all cacheable interface types that it implements. - /// /// /// [MeansImplicitUse] - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true, Inherited = false)] public class CachedAttribute : Attribute { private static readonly GlobalStatistic count_reflection_attributes = GlobalStatistics.Get("Dependencies", "Reflected [Cached]s"); @@ -62,7 +57,7 @@ public class CachedAttribute : Attribute /// The type to cache the value as. If null, the type depends on the type of member that the attribute is placed on: /// /// In the case of fields and properties, the attribute will use the concrete/most-derived type of the field/property's value. - /// In the case of classes and interfaces, the attribute will use the class/interface type on which the was directly placed. + /// In the case of classes, the attribute will use the class type on which the was directly placed. /// /// /// @@ -91,7 +86,6 @@ public class CachedAttribute : Attribute /// To achieve that effect, the has to be repeated on class B. /// /// - /// placed in interface inheritance hierarchies follows analogous rules to the ones described above for classes. /// /// public Type Type; @@ -128,12 +122,6 @@ internal static CacheDependencyDelegate CreateActivator(Type type) var additionActivators = new List>(); - foreach (var iface in type.GetInterfaces()) - { - foreach (var attribute in iface.GetCustomAttributes()) - additionActivators.Add((target, dc, info) => SourceGeneratorUtils.CacheDependency(dc, type, target, info, attribute.Type ?? iface, attribute.Name, null)); - } - foreach (var attribute in type.GetCustomAttributes()) additionActivators.Add((target, dc, info) => SourceGeneratorUtils.CacheDependency(dc, type, target, info, attribute.Type ?? type, attribute.Name, null)); diff --git a/osu.Framework/Graphics/Containers/Markdown/Footnotes/MarkdownFootnote.cs b/osu.Framework/Graphics/Containers/Markdown/Footnotes/MarkdownFootnote.cs index 5e8d529794..a6649a8921 100644 --- a/osu.Framework/Graphics/Containers/Markdown/Footnotes/MarkdownFootnote.cs +++ b/osu.Framework/Graphics/Containers/Markdown/Footnotes/MarkdownFootnote.cs @@ -12,6 +12,8 @@ namespace osu.Framework.Graphics.Containers.Markdown.Footnotes /// /// Visualises a single within a . /// + [Cached(typeof(IMarkdownTextComponent))] + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownFootnote : CompositeDrawable, IMarkdownTextComponent, IMarkdownTextFlowComponent { public readonly Footnote Footnote; diff --git a/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextComponent.cs b/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextComponent.cs index 9b1772eac8..45bbcf9c31 100644 --- a/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextComponent.cs +++ b/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextComponent.cs @@ -1,12 +1,10 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Framework.Allocation; using osu.Framework.Graphics.Sprites; namespace osu.Framework.Graphics.Containers.Markdown { - [Cached(typeof(IMarkdownTextComponent))] public interface IMarkdownTextComponent { /// diff --git a/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextFlowComponent.cs b/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextFlowComponent.cs index bc7c8a2556..ea68493f4f 100644 --- a/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextFlowComponent.cs +++ b/osu.Framework/Graphics/Containers/Markdown/IMarkdownTextFlowComponent.cs @@ -1,12 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Framework.Allocation; - namespace osu.Framework.Graphics.Containers.Markdown { - [Cached(Type = typeof(IMarkdownTextFlowComponent))] - public interface IMarkdownTextFlowComponent + public partial interface IMarkdownTextFlowComponent { /// /// Creates a to display text within this . diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownCodeBlock.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownCodeBlock.cs index 8a13d7d70a..c3a5711012 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownCodeBlock.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownCodeBlock.cs @@ -25,6 +25,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// code3 /// /// + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownCodeBlock : CompositeDrawable, IMarkdownTextFlowComponent { private readonly CodeBlock codeBlock; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownContainer.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownContainer.cs index 203773e4f5..2fc7c12500 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownContainer.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownContainer.cs @@ -24,6 +24,8 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// Visualises a markdown text document. /// + [Cached(typeof(IMarkdownTextComponent))] + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownContainer : CompositeDrawable, IMarkdownTextComponent, IMarkdownTextFlowComponent { private const int root_level = 0; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownHeading.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownHeading.cs index 8a115bd8af..9d80067d5c 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownHeading.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownHeading.cs @@ -15,6 +15,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// ## H2 /// ### H3 /// + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownHeading : CompositeDrawable, IMarkdownTextFlowComponent { private readonly HeadingBlock headingBlock; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownLinkText.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownLinkText.cs index 5845fb4c1b..3fabf2c979 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownLinkText.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownLinkText.cs @@ -17,6 +17,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// [link text](url) /// + [Cached(typeof(IMarkdownTextComponent))] public partial class MarkdownLinkText : CompositeDrawable, IHasTooltip, IMarkdownTextComponent { public LocalisableString TooltipText => Url; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownParagraph.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownParagraph.cs index 5a4e9701e7..04dbac5c2b 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownParagraph.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownParagraph.cs @@ -9,6 +9,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// Visualises a paragraph. /// + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownParagraph : CompositeDrawable, IMarkdownTextFlowComponent { private readonly ParagraphBlock paragraphBlock; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownQuoteBlock.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownQuoteBlock.cs index 094c745a58..6c1f7fe33c 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownQuoteBlock.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownQuoteBlock.cs @@ -14,6 +14,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// > Quote /// + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownQuoteBlock : CompositeDrawable, IMarkdownTextFlowComponent { private readonly QuoteBlock quoteBlock; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownTableCell.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownTableCell.cs index 8b8396e264..f3f577e165 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownTableCell.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownTableCell.cs @@ -17,6 +17,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// | cell 1 | cell 2 | /// + [Cached(typeof(IMarkdownTextFlowComponent))] public partial class MarkdownTableCell : CompositeDrawable, IMarkdownTextFlowComponent { public float ContentWidth => textFlow.TotalTextWidth; diff --git a/osu.Framework/Graphics/Containers/Markdown/MarkdownTextFlowContainer.cs b/osu.Framework/Graphics/Containers/Markdown/MarkdownTextFlowContainer.cs index 8ed9f0cf02..8798e9a934 100644 --- a/osu.Framework/Graphics/Containers/Markdown/MarkdownTextFlowContainer.cs +++ b/osu.Framework/Graphics/Containers/Markdown/MarkdownTextFlowContainer.cs @@ -20,6 +20,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// Markdown text flow container. /// + [Cached(typeof(IMarkdownTextComponent))] public partial class MarkdownTextFlowContainer : CustomizableTextContainer, IMarkdownTextComponent { public float TotalTextWidth => Padding.TotalHorizontal + FlowingChildren.Sum(x => x.BoundingBox.Size.X); diff --git a/osu.Framework/Graphics/Containers/Markdown/NotImplementedMarkdown.cs b/osu.Framework/Graphics/Containers/Markdown/NotImplementedMarkdown.cs index 022cd183ef..38ea8cd240 100644 --- a/osu.Framework/Graphics/Containers/Markdown/NotImplementedMarkdown.cs +++ b/osu.Framework/Graphics/Containers/Markdown/NotImplementedMarkdown.cs @@ -11,6 +11,7 @@ namespace osu.Framework.Graphics.Containers.Markdown /// /// Visualises a message that displays when a doesn't have a visual implementation. /// + [Cached(typeof(IMarkdownTextComponent))] public partial class NotImplementedMarkdown : CompositeDrawable, IMarkdownTextComponent { private readonly IMarkdownObject markdownObject;