This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 15c0d92649 ISIS-3295: FactoryService: java-doc polishing and minor
code cleanup
15c0d92649 is described below
commit 15c0d92649b801bf681c32a5a70bbe207f3d0e35
Author: Andi Huber <[email protected]>
AuthorDate: Fri Nov 25 08:47:14 2022 +0100
ISIS-3295: FactoryService: java-doc polishing and minor code cleanup
---
.../applib/services/factory/FactoryService.java | 52 +++++++++++-----------
.../factory/FactoryServiceDefault.java | 13 +-----
.../testdomain/factory/ViewModelFactoryTest.java | 30 +++++++++++--
3 files changed, 53 insertions(+), 42 deletions(-)
diff --git
a/api/applib/src/main/java/org/apache/causeway/applib/services/factory/FactoryService.java
b/api/applib/src/main/java/org/apache/causeway/applib/services/factory/FactoryService.java
index bd194e39af..499833b7a4 100644
---
a/api/applib/src/main/java/org/apache/causeway/applib/services/factory/FactoryService.java
+++
b/api/applib/src/main/java/org/apache/causeway/applib/services/factory/FactoryService.java
@@ -36,13 +36,11 @@ import lombok.NonNull;
public interface FactoryService {
/**
- * General purpose factory method, to automatically get or create an
instance of
- * {@code requiredType}.
- *
+ * Gets or creates an instance of {@code requiredType}, with injection
points resolved and any
+ * life-cycle callback processed.
* <p>
* Maps onto one of the specialized factory methods {@link #get(Class)} or
{@link #create(Class)}
* based on the type's meta-data.
- * </p>
*
* @param <T>
* @param requiredType
@@ -50,25 +48,19 @@ public interface FactoryService {
* @throws UnrecoverableException if instance creation failed
* @throws IllegalArgumentException if requiredType is not recognized by
the meta-model
*
+ * @see #get(Class)
+ * @see #create(Class)
+ *
* @since 2.0
*
*/
-
- /**
- * Gets an instance (possibly shared or independent) of the specified
type, with injection points resolved and any
- * life-cycle callback processed.
- * @param requiredType
- * @param <T>
- */
<T> T getOrCreate(@NonNull Class<T> requiredType);
/**
- * Gets an instance (possibly shared or independent) of the specified
{@code requiredType},
- * with injection points resolved
- * and any life-cycle callback processed.
+ * Gets a <i>Spring</i> managed bean of {@code requiredType}.
*
* @param <T>
- * @param requiredType - only applicable to IoC container managed types
+ * @param requiredType - must be a <i>Spring</i> managed type
* @return (non-null), an instance of {@code requiredType}, if available
and unique
* (i.e. not multiple candidates found with none marked as primary)
*
@@ -83,13 +75,11 @@ public interface FactoryService {
/**
* Creates a new detached entity instance, with injection points resolved
* and defaults applied.
- *
* <p>
- * The entity will be detacted, in other words not yet persisted.
- * </p>
+ * The entity will not yet be persisted, in other words: its not yet known
to the persistence layer.
*
* @param <T>
- * @param domainClass - only applicable to entity types
+ * @param domainClass - must be an entity type
* @throws IllegalArgumentException if domainClass is not an entity type
* @apiNote forces the domainClass to be added to the meta-model if not
already
* @since 2.0
@@ -98,6 +88,8 @@ public interface FactoryService {
/**
* Creates a new detached entity instance, with injection points resolved.
+ * <p>
+ * The entity will not yet be persisted, in other words: its not yet known
to the persistence layer.
*
* @param <T>
* @param entity - most likely just new-ed up, without injection points
resolved
@@ -119,8 +111,10 @@ public interface FactoryService {
<T> T mixin(@NonNull Class<T> mixinClass, @NonNull Object mixedIn);
/**
- * Creates a new ViewModel instance, with injection points resolved,
- * and initialized according to the given {@code bookmark}
+ * Creates a new ViewModel instance,
+ * initialized with given {@code bookmark} (if any)
+ * then resolves any injection points
+ * and calls post-construct (if any).
*
* @param viewModelClass
* @param bookmark - ignored if {@code null}
@@ -132,7 +126,8 @@ public interface FactoryService {
/**
* Creates a new ViewModel instance,
- * with injection points resolved
+ * with injection points resolved,
+ * post-construct called
* and defaults applied.
* @param viewModelClass
* @throws IllegalArgumentException if viewModelClass is not a viewmodel
type
@@ -140,11 +135,13 @@ public interface FactoryService {
* @since 2.0
*/
default <T> T viewModel(@NonNull final Class<T> viewModelClass) {
- return viewModel(viewModelClass, /*bookmark*/null);
+ return viewModel(viewModelClass, (Bookmark)null);
}
/**
- * Resolves injection points for given ViewModel instance.
+ * Resolves injection points for
+ * and calls post-construct on
+ * given view-model instance.
* @param viewModel - most likely just new-ed up, without injection points
resolved
* @throws IllegalArgumentException if viewModelClass is not a viewmodel
type
* @apiNote forces the viewModel's class to be added to the meta-model if
not already
@@ -154,10 +151,11 @@ public interface FactoryService {
/**
* Creates a new instance of the specified class,
- * with injection points resolved
+ * with injection points resolved,
+ * post-construct called
* and defaults applied.
- * @param domainClass - not applicable to IoC container managed types
- * @throws IllegalArgumentException if domainClass is an IoC container
managed type,
+ * @param domainClass - must be a <i>Spring</i> managed type
+ * @throws IllegalArgumentException if domainClass is a <i>Spring</i>
managed type,
* or not recognized by the meta-model
* @apiNote forces the domainClass to be added to the meta-model if not
already
* @since 2.0
diff --git
a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/factory/FactoryServiceDefault.java
b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/factory/FactoryServiceDefault.java
index ad94b06aa2..37535df515 100644
---
a/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/factory/FactoryServiceDefault.java
+++
b/core/runtimeservices/src/main/java/org/apache/causeway/core/runtimeservices/factory/FactoryServiceDefault.java
@@ -37,7 +37,6 @@ import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
import org.apache.causeway.core.config.environment.CausewaySystemEnvironment;
import org.apache.causeway.core.metamodel.facets.object.mixin.MixinFacet;
-import
org.apache.causeway.core.metamodel.facets.object.viewmodel.ViewModelFacet;
import org.apache.causeway.core.metamodel.object.ManagedObject;
import
org.apache.causeway.core.metamodel.services.objectlifecycle.ObjectLifecyclePublisher;
import org.apache.causeway.core.metamodel.spec.ObjectSpecification;
@@ -131,7 +130,7 @@ public class FactoryServiceDefault implements
FactoryService {
throw _Exceptions.illegalArgument("Type '%s' is not recogniced as
a ViewModel by the framework.",
viewModelClass);
}
- val viewModelFacet = getViewModelFacet(spec);
+ val viewModelFacet = spec.viewmodelFacetElseFail();
val viewModel = viewModelFacet.instantiate(spec,
Optional.ofNullable(bookmark));
objectLifecyclePublisher().onPostCreate(viewModel);
return _Casts.uncheckedCast(viewModel.getPojo());
@@ -154,16 +153,6 @@ public class FactoryServiceDefault implements
FactoryService {
return specificationLoader.specForTypeElseFail(type);
}
- private ViewModelFacet getViewModelFacet(final @NonNull
ObjectSpecification spec) {
- val viewModelFacet = spec.getFacet(ViewModelFacet.class);
- if(viewModelFacet==null) {
- throw _Exceptions.illegalArgument("Type '%s' must be recogniced as
a ViewModel, "
- + "that is the type's meta-model "
- + "must have an associated ViewModelFacet: ",
spec.getCorrespondingClass());
- }
- return viewModelFacet;
- }
-
private Object createObject(final ObjectSpecification spec) {
// already handles injection and publishing
val domainObject = spec.createObject();
diff --git
a/regressiontests/stable-factory/src/test/java/org/apache/causeway/testdomain/factory/ViewModelFactoryTest.java
b/regressiontests/stable-factory/src/test/java/org/apache/causeway/testdomain/factory/ViewModelFactoryTest.java
index 42fa0fc29c..81dc49a1de 100644
---
a/regressiontests/stable-factory/src/test/java/org/apache/causeway/testdomain/factory/ViewModelFactoryTest.java
+++
b/regressiontests/stable-factory/src/test/java/org/apache/causeway/testdomain/factory/ViewModelFactoryTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.causeway.testdomain.factory;
+import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
@@ -55,10 +56,18 @@ class ViewModelFactoryTest extends
CausewayIntegrationTestAbstract {
@DomainObject(nature = Nature.VIEW_MODEL)
public static class SimpleViewModel {
@Inject private RepositoryService repository;
+ private boolean postConstructCalled;
- public boolean areInjectionPointsResolved() {
- return repository!=null;
+ public void assertInitialized() {
+ assertNotNull(repository, ()->"repository (field) not injected");
+ assertTrue(postConstructCalled, ()->"@PostConstruct not called");
}
+
+ @PostConstruct
+ void onPostConstruct() {
+ postConstructCalled = true;
+ }
+
}
@DomainObject(nature = Nature.VIEW_MODEL)
@@ -67,16 +76,23 @@ class ViewModelFactoryTest extends
CausewayIntegrationTestAbstract {
@Inject private RepositoryService repository;
@Getter private final String name;
+ private boolean postConstructCalled;
public void assertInitialized() {
assertNotNull(repository, ()->"repository (field) not injected");
assertEquals("aName", getName(), ()->"unexpected name (constructor
arg)");
+ assertTrue(postConstructCalled, ()->"@PostConstruct not called");
}
@Override
public String viewModelMemento() {
return "aName";
}
+
+ @PostConstruct
+ void onPostConstruct() {
+ postConstructCalled = true;
+ }
}
@@ -87,17 +103,25 @@ class ViewModelFactoryTest extends
CausewayIntegrationTestAbstract {
@Inject private RepositoryService repository;
private final ServiceRegistry registry;
@Getter private final String name;
+ private boolean postConstructCalled;
public void assertInitialized() {
assertNotNull(repository, ()->"repository (field) not injected");
assertNotNull(registry, ()->"registry (constructor arg) not
injected");
assertEquals("aName", getName(), ()->"unexpected name (constructor
arg)");
+ assertTrue(postConstructCalled, ()->"@PostConstruct not called");
}
@Override
public String viewModelMemento() {
return "aName";
}
+
+ @PostConstruct
+ void onPostConstruct() {
+ postConstructCalled = true;
+ }
+
}
// -- TESTS
@@ -105,7 +129,7 @@ class ViewModelFactoryTest extends
CausewayIntegrationTestAbstract {
@Test
void sampleViewModel_shouldHave_injectionPointsResolved() {
val sampleViewModel = factoryService.viewModel(SimpleViewModel.class);
- assertTrue(sampleViewModel.areInjectionPointsResolved());
+ sampleViewModel.assertInitialized();
}
@Test