SLING-6318 - improve optimization by avoiding null return values from prepareValues
authorJustin Edelson <justin@apache.org>
Wed, 30 Nov 2016 17:47:13 +0000 (17:47 +0000)
committerJustin Edelson <justin@apache.org>
Wed, 30 Nov 2016 17:47:13 +0000 (17:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1772093 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
src/main/java/org/apache/sling/models/impl/injectors/BindingsInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java
src/main/java/org/apache/sling/models/impl/injectors/ValuePreparer.java

index b9385f4..69ad2e8 100644 (file)
@@ -458,18 +458,10 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
                         final ValuePreparer preparer = (ValuePreparer) injector;
                         Object fromMap = preparedValues.get(preparer);
                         if (fromMap != null) {
-                            if (ObjectUtils.NULL.equals(fromMap)) {
-                                preparedValue = null;
-                            } else {
-                                preparedValue = fromMap;
-                            }
+                            preparedValue = fromMap;
                         } else {
                             preparedValue = preparer.prepareValue(injectionAdaptable);
-                            if (preparedValue == null) {
-                                preparedValues.put(preparer, ObjectUtils.NULL);
-                            } else {
-                                preparedValues.put(preparer, preparedValue);
-                            }
+                            preparedValues.put(preparer, preparedValue);
                         }
                     }
 
index 73ab708..740c369 100644 (file)
@@ -22,6 +22,7 @@ import java.lang.reflect.Type;
 import javax.annotation.Nonnull;
 import javax.servlet.ServletRequest;
 
+import org.apache.commons.lang.ObjectUtils;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
@@ -51,6 +52,9 @@ public class BindingsInjector implements Injector, StaticInjectAnnotationProcess
 
     public Object getValue(@Nonnull Object adaptable, String name, @Nonnull Type type, @Nonnull AnnotatedElement element,
             @Nonnull DisposalCallbackRegistry callbackRegistry) {
+        if (adaptable == ObjectUtils.NULL) {
+            return null;
+        }
         SlingBindings bindings = getBindings(adaptable);
         if (bindings == null) {
             return null;
@@ -86,7 +90,8 @@ public class BindingsInjector implements Injector, StaticInjectAnnotationProcess
 
     @Override
     public Object prepareValue(Object adaptable) {
-        return getBindings(adaptable);
+        Object prepared = getBindings(adaptable);
+        return prepared != null ? prepared : ObjectUtils.NULL;
     }
 
     private static class ScriptVariableAnnotationProcessor extends AbstractInjectAnnotationProcessor2 {
index 68532c9..190dcff 100644 (file)
@@ -27,6 +27,7 @@ import java.util.List;
 import javax.annotation.Nonnull;
 
 import org.apache.commons.lang.ClassUtils;
+import org.apache.commons.lang.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
@@ -59,6 +60,9 @@ public class ValueMapInjector extends AbstractInjector implements Injector, Inje
 
     public Object getValue(@Nonnull Object adaptable, String name, @Nonnull Type type, @Nonnull AnnotatedElement element,
             @Nonnull DisposalCallbackRegistry callbackRegistry) {
+        if (adaptable == ObjectUtils.NULL) {
+            return null;
+        }
         ValueMap map = getValueMap(adaptable);
         if (map == null) {
             return null;
@@ -135,7 +139,8 @@ public class ValueMapInjector extends AbstractInjector implements Injector, Inje
 
     @Override
     public Object prepareValue(final Object adaptable) {
-        return getValueMap(adaptable);
+        Object prepared = getValueMap(adaptable);
+        return prepared != null ? prepared : ObjectUtils.NULL;
     }
 
     @Override
index 772aafa..c9a7c80 100644 (file)
@@ -19,7 +19,18 @@ package org.apache.sling.models.impl.injectors;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 
+/**
+ * Optimization interface for Injectors which wish to avoid repeated accessing of some object
+ * based on the adaptable. If an Injector implements this interface, it must also be prepared
+ * to handle the case where ObjectUtils.NULL is passed as the adaptable.
+ */
 public interface ValuePreparer {
 
-    @CheckForNull Object prepareValue(@Nonnull Object adaptable);
+    /**
+     * Prepare a value from the adaptable.
+     *
+     * @param adaptable the adaptable
+     * @return a prepared value or ObjectUtils.NULL if a value is not preparable
+     */
+    @Nonnull Object prepareValue(@Nonnull Object adaptable);
 }