From 83884c840c1ab8df7da1dfc876d2c9e0f4f252ed Mon Sep 17 00:00:00 2001 From: Andi Huber Date: Mon, 18 Nov 2024 06:53:04 +0100 Subject: [PATCH] CAUSEWAY-2297: mixed-in member optimization: keep mixin spec --- .../specimpl/ObjectActionMixedIn.java | 18 +++++++++--------- .../specimpl/ObjectMemberAbstract.java | 7 +++---- .../specimpl/ObjectSpecificationAbstract.java | 4 ++-- .../specimpl/OneToManyAssociationMixedIn.java | 14 +++++++------- .../specimpl/OneToOneAssociationMixedIn.java | 14 +++++++------- .../specimpl/_MixedInMemberFactory.java | 10 +++++----- .../facets/FacetFactoryTestAbstract.java | 9 ++++++--- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectActionMixedIn.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectActionMixedIn.java index 8530baf7e42..08b373575d5 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectActionMixedIn.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectActionMixedIn.java @@ -58,11 +58,11 @@ public class ObjectActionMixedIn public static class forTesting { public static ObjectActionMixedIn forMixinMain( final ObjectSpecification mixeeSpec, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName, final FacetedMethod facetedMethod) { final ObjectActionDefault mixinAction = (ObjectActionDefault) ObjectActionDefault.forTesting.forMixinMain(facetedMethod); - return new ObjectActionMixedIn(mixinType, mixinMethodName, mixinAction, mixeeSpec); + return new ObjectActionMixedIn(mixinSpec, mixinMethodName, mixinAction, mixeeSpec); } } @@ -71,7 +71,7 @@ public static ObjectActionMixedIn forMixinMain( /** * The type of the mixin (providing the action), eg annotated with @{@link DomainObject DomainObject}#{@link DomainObject#nature() nature} of {@link org.apache.causeway.applib.annotation.Nature#MIXIN MIXIN}. */ - private final Class mixinType; + private final ObjectSpecification mixinSpec; /** * The {@link ObjectActionDefault} for the action being mixed in (ie on the {@link #mixinType}. @@ -90,7 +90,7 @@ public static ObjectActionMixedIn forMixinMain( private final FacetHolder facetHolder; public ObjectActionMixedIn( - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName, final ObjectActionDefault mixinAction, final ObjectSpecification mixeeSpec) { @@ -106,7 +106,7 @@ public ObjectActionMixedIn( this.facetHolder = FacetHolder.layered( super.getFeatureIdentifier(), mixinAction.getFacetedMethod()); - this.mixinType = mixinType; + this.mixinSpec = mixinSpec; this.mixinAction = mixinAction; this.mixeeSpec = mixeeSpec; @@ -126,7 +126,7 @@ public ObjectActionMixedIn( protected InteractionHead headFor(final ManagedObject owner) { return InteractionHead.mixin( owner, - mixinAdapterFor(mixinType, owner)); + mixinAdapterFor(mixinSpec, owner)); } @Override @@ -163,7 +163,7 @@ public CanVector getChoices( } protected ManagedObject mixinAdapterFor(final ManagedObject mixeeAdapter) { - return mixinAdapterFor(mixinType, mixeeAdapter); + return mixinAdapterFor(mixinSpec, mixeeAdapter); } @Override @@ -173,7 +173,7 @@ public ManagedObject execute( final InteractionInitiatedBy interactionInitiatedBy) { final ManagedObject owner = head.getOwner(); - final ManagedObject target = mixinAdapterFor(mixinType, owner); + final ManagedObject target = mixinAdapterFor(mixinSpec, owner); _Assert.assertEquals(target.getSpecification(), head.getTarget().getSpecification(), "head has the wrong target (should be a mixed-in adapter, but is the mixee adapter)"); @@ -199,7 +199,7 @@ public ManagedObject execute( @Override public ObjectSpecification getMixinType() { - return getSpecificationLoader().loadSpecification(mixinType); + return mixinSpec; } @Getter(lazy=true, onMethod_ = {@Override}) diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectMemberAbstract.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectMemberAbstract.java index 362ccefc1e9..0073c560a0b 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectMemberAbstract.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectMemberAbstract.java @@ -51,6 +51,7 @@ import org.apache.causeway.core.metamodel.object.ManagedObject; import org.apache.causeway.core.metamodel.object.ManagedObjects; import org.apache.causeway.core.metamodel.services.command.CommandDtoFactory; +import org.apache.causeway.core.metamodel.spec.ObjectSpecification; import org.apache.causeway.core.metamodel.spec.feature.MixedInMember; import org.apache.causeway.core.metamodel.spec.feature.ObjectMember; import org.apache.causeway.schema.cmd.v2.CommandDto; @@ -265,17 +266,15 @@ public final boolean isOneToOneAssociation() { // -- MIXIN ADAPTER FACTORY protected ManagedObject mixinAdapterFor( - final @NonNull Class mixinType, + final @NonNull ObjectSpecification mixinSpec, final @NonNull ManagedObject mixee) { - var mixinSpec = getSpecificationLoader().loadSpecification(mixinType); - // nullable for action parameter mixins if(ManagedObjects.isNullOrUnspecifiedOrEmpty(mixee)) { return ManagedObject.empty(mixinSpec); } - var mixinPojo = getMetaModelContext().getFactoryService().mixin(mixinType, mixee.getPojo()); + var mixinPojo = getFactoryService().mixin(mixinSpec.getCorrespondingClass(), mixee.getPojo()); return ManagedObject.mixin(mixinSpec, mixinPojo); } diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectSpecificationAbstract.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectSpecificationAbstract.java index 968f87655a3..11d64e1d6c6 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectSpecificationAbstract.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/ObjectSpecificationAbstract.java @@ -786,7 +786,7 @@ private Stream createMixedInAssociation(final Class mixinT return mixinSpec.streamActions(ActionScope.ANY, MixedIn.EXCLUDED) .filter(_SpecPredicates::isMixedInAssociation) .map(ObjectActionDefault.class::cast) - .map(_MixedInMemberFactory.mixedInAssociation(this, mixinType, mixinMethodName)) + .map(_MixedInMemberFactory.mixedInAssociation(this, mixinSpec, mixinMethodName)) .peek(facetProcessor::processMemberOrder); } @@ -828,7 +828,7 @@ private Stream createMixedInAction(final Class mixinType .filter(this::whenIsValueThenIsAlsoConstructorMixin) .filter(_SpecPredicates::isMixedInAction) .map(ObjectActionDefault.class::cast) - .map(_MixedInMemberFactory.mixedInAction(this, mixinType, mixinMethodName)) + .map(_MixedInMemberFactory.mixedInAction(this, mixinSpec, mixinMethodName)) .peek(facetProcessor::processMemberOrder); } diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToManyAssociationMixedIn.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToManyAssociationMixedIn.java index 6010bf065c9..9355df2f50a 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToManyAssociationMixedIn.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToManyAssociationMixedIn.java @@ -55,11 +55,11 @@ public class OneToManyAssociationMixedIn public static class forTesting { public static OneToManyAssociationMixedIn forMixinMain( final ObjectSpecification mixeeSpec, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName, final FacetedMethod facetedMethod) { final ObjectActionDefault mixinAction = (ObjectActionDefault) ObjectActionDefault.forTesting.forMixinMain(facetedMethod); - return new OneToManyAssociationMixedIn(mixeeSpec, mixinAction, mixinType, mixinMethodName); + return new OneToManyAssociationMixedIn(mixeeSpec, mixinAction, mixinSpec, mixinMethodName); } } @@ -70,7 +70,7 @@ public static OneToManyAssociationMixedIn forMixinMain( * {@link org.apache.causeway.applib.annotation.DomainObject} with a {@link DomainObject#nature() nature} of * {@link org.apache.causeway.applib.annotation.Nature#MIXIN MIXIN}. */ - private final Class mixinType; + private final ObjectSpecification mixinSpec; /** * The {@link ObjectActionDefault} for the action being mixed in (ie on the {@link #mixinType}). @@ -106,7 +106,7 @@ private static ObjectSpecification typeOfSpec( public OneToManyAssociationMixedIn( final ObjectSpecification mixeeSpec, final ObjectActionDefault mixinAction, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName) { super(identifierForMixedInCollection(mixeeSpec, mixinAction), @@ -116,7 +116,7 @@ public OneToManyAssociationMixedIn( super.getFeatureIdentifier(), mixinAction.getFacetedMethod()); - this.mixinType = mixinType; + this.mixinSpec = mixinSpec; this.mixinAction = mixinAction; this.mixeeSpec = mixeeSpec; @@ -140,7 +140,7 @@ public OneToManyAssociationMixedIn( @Override protected InteractionHead headFor(final ManagedObject mixedInAdapter) { - var mixinAdapter = mixinAdapterFor(mixinType, mixedInAdapter); + var mixinAdapter = mixinAdapterFor(mixinSpec, mixedInAdapter); return InteractionHead.mixin(mixedInAdapter, mixinAdapter); } @@ -171,7 +171,7 @@ public ObjectSpecification getDeclaringType() { @Override public ObjectSpecification getMixinType() { - return getSpecificationLoader().loadSpecification(mixinType); + return mixinSpec; } @Override diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToOneAssociationMixedIn.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToOneAssociationMixedIn.java index 3a82a7efa64..b1b30ca7c9e 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToOneAssociationMixedIn.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/OneToOneAssociationMixedIn.java @@ -52,11 +52,11 @@ public class OneToOneAssociationMixedIn public static class forTesting { public static OneToOneAssociationMixedIn forMixinMain( final ObjectSpecification mixeeSpec, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName, final FacetedMethod facetedMethod) { final ObjectActionDefault mixinAction = (ObjectActionDefault) ObjectActionDefault.forTesting.forMixinMain(facetedMethod); - return new OneToOneAssociationMixedIn(mixeeSpec, mixinAction, mixinType, mixinMethodName); + return new OneToOneAssociationMixedIn(mixeeSpec, mixinAction, mixinSpec, mixinMethodName); } } @@ -65,7 +65,7 @@ public static OneToOneAssociationMixedIn forMixinMain( /** * The type of the mixin (providing the action), eg annotated with {@link org.apache.causeway.applib.annotation.Mixin}. */ - private final Class mixinType; + private final ObjectSpecification mixinSpec; /** * The {@link ObjectActionDefault} for the action being mixed in (ie on the {@link #mixinType}. @@ -86,7 +86,7 @@ public static OneToOneAssociationMixedIn forMixinMain( public OneToOneAssociationMixedIn( final ObjectSpecification mixeeSpec, final ObjectActionDefault mixinAction, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName) { super( identifierForMixedInProperty(mixeeSpec, mixinAction), @@ -97,7 +97,7 @@ public OneToOneAssociationMixedIn( super.getFeatureIdentifier(), mixinAction.getFacetedMethod()); - this.mixinType = mixinType; + this.mixinSpec = mixinSpec; this.mixinAction = mixinAction; this.mixeeSpec = mixeeSpec; @@ -122,7 +122,7 @@ public OneToOneAssociationMixedIn( protected InteractionHead headFor(final ManagedObject mixeeAdapter) { return InteractionHead.mixin( mixeeAdapter, - mixinAdapterFor(mixinType, mixeeAdapter)); + mixinAdapterFor(mixinSpec, mixeeAdapter)); } private DisabledFacet disabledFacet() { @@ -154,7 +154,7 @@ public ObjectSpecification getDeclaringType() { @Override public ObjectSpecification getMixinType() { - return getSpecificationLoader().loadSpecification(mixinType); + return mixinSpec; } @Override diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/_MixedInMemberFactory.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/_MixedInMemberFactory.java index 34f6d9c7829..d860d91112c 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/_MixedInMemberFactory.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/specloader/specimpl/_MixedInMemberFactory.java @@ -33,24 +33,24 @@ class _MixedInMemberFactory { Function mixedInAction( final ObjectSpecification mixeeSpec, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName) { return mixinAction -> new ObjectActionMixedIn( - mixinType, mixinMethodName, mixinAction, mixeeSpec); + mixinSpec, mixinMethodName, mixinAction, mixeeSpec); } Function mixedInAssociation( final ObjectSpecification mixeeSpec, - final Class mixinType, + final ObjectSpecification mixinSpec, final String mixinMethodName) { return mixinAction -> mixinAction.getReturnType().isSingular() ? new OneToOneAssociationMixedIn( - mixeeSpec, mixinAction, mixinType, mixinMethodName) + mixeeSpec, mixinAction, mixinSpec, mixinMethodName) : new OneToManyAssociationMixedIn( - mixeeSpec, mixinAction, mixinType, mixinMethodName); + mixeeSpec, mixinAction, mixinSpec, mixinMethodName); } } diff --git a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java index 17bcccd1ae2..e87495cf639 100644 --- a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java +++ b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java @@ -253,8 +253,9 @@ protected void actionScenarioMixedIn( methodRemover, facetedMethod, true); final ObjectSpecification mixeeSpec = getSpecificationLoader().loadSpecification(declaringClass); + final ObjectSpecification mixinSpec = getSpecificationLoader().loadSpecification(mixinClass); final ObjectActionMixedIn mixedInAct = - ObjectActionMixedIn.forTesting.forMixinMain(mixeeSpec, mixinClass, "act", facetedMethod); + ObjectActionMixedIn.forTesting.forMixinMain(mixeeSpec, mixinSpec, "act", facetedMethod); consumer.accept(processMethodContext, mixeeSpec, facetedMethod, mixedInAct); } @@ -347,8 +348,9 @@ protected void propertyScenarioMixedIn( methodRemover, facetedMethod, true); final ObjectSpecification mixeeSpec = getSpecificationLoader().loadSpecification(declaringClass); + final ObjectSpecification mixinSpec = getSpecificationLoader().loadSpecification(mixinClass); final OneToOneAssociationMixedIn mixedInProp = - OneToOneAssociationMixedIn.forTesting.forMixinMain(mixeeSpec, mixinClass, "prop", facetedMethod); + OneToOneAssociationMixedIn.forTesting.forMixinMain(mixeeSpec, mixinSpec, "prop", facetedMethod); consumer.accept(processMethodContext, mixeeSpec, facetedMethod, mixedInProp); } @@ -407,8 +409,9 @@ protected void collectionScenarioMixedIn( methodRemover, facetedMethod, true); final ObjectSpecification mixeeSpec = getSpecificationLoader().loadSpecification(declaringClass); + final ObjectSpecification mixinSpec = getSpecificationLoader().loadSpecification(mixinClass); final OneToManyAssociationMixedIn mixedInColl = - OneToManyAssociationMixedIn.forTesting.forMixinMain(mixeeSpec, mixinClass, "coll", facetedMethod); + OneToManyAssociationMixedIn.forTesting.forMixinMain(mixeeSpec, mixinSpec, "coll", facetedMethod); consumer.accept(processMethodContext, mixeeSpec, facetedMethod, mixedInColl); }