You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/12/03 15:22:05 UTC

[sling-org-apache-sling-models-impl] branch bugfix/SLING-8079-PostConstructReturningFalse created (now c571014)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch bugfix/SLING-8079-PostConstructReturningFalse
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git.


      at c571014  SLING-8079 throw PostConstructException for createModel in case PostConstruct method returned false

This branch includes the following new commits:

     new c571014  SLING-8079 throw PostConstructException for createModel in case PostConstruct method returned false

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-models-impl] 01/01: SLING-8079 throw PostConstructException for createModel in case PostConstruct method returned false

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/SLING-8079-PostConstructReturningFalse
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git

commit c571014bccab10c4722f82e8001b062b12169929
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Dec 3 16:21:56 2021 +0100

    SLING-8079 throw PostConstructException for createModel in case
    PostConstruct method returned false
    
    Prevent creation of exception object for simple adaptations
---
 .../org/apache/sling/models/impl/ModelAdapterFactory.java | 11 +++++++++--
 src/main/java/org/apache/sling/models/impl/Result.java    |  9 +++++++++
 .../org/apache/sling/models/impl/PostConstructTest.java   | 15 ++++++++++-----
 ...ostConstuctModel.java => FalsePostConstructModel.java} |  2 +-
 4 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index 4cfb367..520606b 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -265,7 +265,11 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
     public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
         Result<AdapterType> result = internalCreateModel(adaptable, type);
         if (!result.wasSuccessful()) {
-            log.warn("Could not adapt to model", result.getThrowable());
+            if (result == Result.POST_CONSTRUCT_PREVENTED_MODEL_CONSTRUCTION) {
+                log.warn("Could not adapt to model as PostConstruct method returned false"); // do no construct runtime exception in this case
+            } else {
+                log.warn("Could not adapt to model", result.getThrowable());
+            }
             return null;
         } else {
             return result.getValue();
@@ -359,7 +363,7 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
     }
 
     @SuppressWarnings("unchecked")
-    private <ModelType> Result<ModelType> internalCreateModel(final Object adaptable, final Class<ModelType> requestedType) {
+    <ModelType> Result<ModelType> internalCreateModel(final Object adaptable, final Class<ModelType> requestedType) {
         Result<ModelType> result;
         ThreadInvocationCounter threadInvocationCounter = invocationCountThreadLocal.get();
         if (threadInvocationCounter.isMaximumReached()) {
@@ -754,6 +758,9 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
         }
         try {
             object = invokePostConstruct(object);
+            if (object == null) {
+                return (Result<ModelType>) Result.POST_CONSTRUCT_PREVENTED_MODEL_CONSTRUCTION;
+            }
         } catch (InvocationTargetException e) {
             return new Result<>(new PostConstructException("Post-construct method has thrown an exception for model " + modelClass.getType(), e.getCause()));
         } catch (IllegalAccessException e) {
diff --git a/src/main/java/org/apache/sling/models/impl/Result.java b/src/main/java/org/apache/sling/models/impl/Result.java
index d056c92..df9612c 100644
--- a/src/main/java/org/apache/sling/models/impl/Result.java
+++ b/src/main/java/org/apache/sling/models/impl/Result.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.models.impl;
 
+import org.apache.sling.models.factory.PostConstructException;
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -88,5 +89,13 @@ public class Result<SuccessObjectType> {
         return object != null;
     }
 
+    public static final Result<Object> POST_CONSTRUCT_PREVENTED_MODEL_CONSTRUCTION = new Result<Object>((RuntimeException)null) {
 
+        @Override
+        public @NotNull RuntimeException getThrowable() {
+            // generate exception lazily
+            return new PostConstructException("PostConstruct method returned false", null);
+        }
+        
+    };
 }
diff --git a/src/test/java/org/apache/sling/models/impl/PostConstructTest.java b/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
index 90c1b92..f7eca88 100644
--- a/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
+++ b/src/test/java/org/apache/sling/models/impl/PostConstructTest.java
@@ -19,12 +19,13 @@ package org.apache.sling.models.impl;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.models.factory.PostConstructException;
 import org.apache.sling.models.testmodels.classes.FailingPostConstuctModel;
-import org.apache.sling.models.testmodels.classes.FalsePostConstuctModel;
+import org.apache.sling.models.testmodels.classes.FalsePostConstructModel;
 import org.apache.sling.models.testmodels.classes.SubClass;
 import org.apache.sling.models.testmodels.classes.SubClassOverriddenPostConstruct;
 import org.apache.sling.models.testmodels.classes.TruePostConstuctModel;
@@ -46,7 +47,7 @@ public class PostConstructTest {
     public void setup() {
         factory = AdapterFactoryTest.createModelAdapterFactory();
         // no injectors are necessary
-        factory.adapterImplementations.addClassesAsAdapterAndImplementation(SubClass.class, SubClassOverriddenPostConstruct.class, FailingPostConstuctModel.class, FalsePostConstuctModel.class, TruePostConstuctModel.class);
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(SubClass.class, SubClassOverriddenPostConstruct.class, FailingPostConstuctModel.class, FalsePostConstructModel.class, TruePostConstuctModel.class);
     }
 
     @Test
@@ -71,7 +72,7 @@ public class PostConstructTest {
 
     @Test
     public void testPostConstructMethodWhichReturnsFalse() {
-        FalsePostConstuctModel model = factory.getAdapter(resource, FalsePostConstuctModel.class);
+        FalsePostConstructModel model = factory.getAdapter(resource, FalsePostConstructModel.class);
         assertNull(model);
     }
 
@@ -81,9 +82,13 @@ public class PostConstructTest {
         assertNotNull(model);
     }
 
-    @Test(expected = IllegalStateException.class)
+    @Test(expected = PostConstructException.class)
     public void testPostConstructMethodWhichReturnsFalseCreateModel() {
-        factory.createModel(resource, FalsePostConstuctModel.class);
+        factory.createModel(resource, FalsePostConstructModel.class);
+    }
+
+    public void testPostConstructMethodWhichReturnsFalseInternalCreateModel() {
+        assertSame(Result.POST_CONSTRUCT_PREVENTED_MODEL_CONSTRUCTION, factory.internalCreateModel(resource, FalsePostConstructModel.class));
     }
 
     @Test
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstructModel.java
similarity index 96%
rename from src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java
rename to src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstructModel.java
index 4c6b724..a1bd687 100644
--- a/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstuctModel.java
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/FalsePostConstructModel.java
@@ -24,7 +24,7 @@ import org.apache.sling.models.annotations.Model;
 import javax.annotation.PostConstruct;
 
 @Model(adaptables=Resource.class)
-public class FalsePostConstuctModel {
+public class FalsePostConstructModel {
 
     @PostConstruct
     protected boolean pc() throws Exception {