Skip to content

Commit

Permalink
CAUSEWAY-2297: mixed-in member optimization: keep mixin spec
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Nov 18, 2024
1 parent 0f7dbde commit 83884c8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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}.
Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -126,7 +126,7 @@ public ObjectActionMixedIn(
protected InteractionHead headFor(final ManagedObject owner) {
return InteractionHead.mixin(
owner,
mixinAdapterFor(mixinType, owner));
mixinAdapterFor(mixinSpec, owner));
}

@Override
Expand Down Expand Up @@ -163,7 +163,7 @@ public CanVector<ManagedObject> getChoices(
}

protected ManagedObject mixinAdapterFor(final ManagedObject mixeeAdapter) {
return mixinAdapterFor(mixinType, mixeeAdapter);
return mixinAdapterFor(mixinSpec, mixeeAdapter);
}

@Override
Expand All @@ -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)");

Expand All @@ -199,7 +199,7 @@ public ManagedObject execute(

@Override
public ObjectSpecification getMixinType() {
return getSpecificationLoader().loadSpecification(mixinType);
return mixinSpec;
}

@Getter(lazy=true, onMethod_ = {@Override})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ private Stream<ObjectAssociation> 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);
}

Expand Down Expand Up @@ -828,7 +828,7 @@ private Stream<ObjectActionMixedIn> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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}).
Expand Down Expand Up @@ -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),
Expand All @@ -116,7 +116,7 @@ public OneToManyAssociationMixedIn(
super.getFeatureIdentifier(),
mixinAction.getFacetedMethod());

this.mixinType = mixinType;
this.mixinSpec = mixinSpec;
this.mixinAction = mixinAction;
this.mixeeSpec = mixeeSpec;

Expand All @@ -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);
}

Expand Down Expand Up @@ -171,7 +171,7 @@ public ObjectSpecification getDeclaringType() {

@Override
public ObjectSpecification getMixinType() {
return getSpecificationLoader().loadSpecification(mixinType);
return mixinSpec;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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}.
Expand All @@ -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),
Expand All @@ -97,7 +97,7 @@ public OneToOneAssociationMixedIn(
super.getFeatureIdentifier(),
mixinAction.getFacetedMethod());

this.mixinType = mixinType;
this.mixinSpec = mixinSpec;
this.mixinAction = mixinAction;
this.mixeeSpec = mixeeSpec;

Expand All @@ -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() {
Expand Down Expand Up @@ -154,7 +154,7 @@ public ObjectSpecification getDeclaringType() {

@Override
public ObjectSpecification getMixinType() {
return getSpecificationLoader().loadSpecification(mixinType);
return mixinSpec;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ class _MixedInMemberFactory {

Function<ObjectActionDefault, ObjectActionMixedIn> 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<ObjectActionDefault, ObjectAssociation> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 83884c8

Please sign in to comment.