sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1619212 - in /sling/trunk/bundles/extensions/models/impl/src: main/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/impl/ test/java/org/apache/sling/models/testmodels/classes/ test/java/org/apache/sling/models/testmodel...
Date Wed, 20 Aug 2014 19:42:00 GMT
Author: justin
Date: Wed Aug 20 19:42:00 2014
New Revision: 1619212

URL: http://svn.apache.org/r1619212
Log:
SLING-3863 - correcting behavior where optional methods and constructor parameters were injected
with non-null wrapper classes.

Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java
    sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1619212&r1=1619211&r2=1619212&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
Wed Aug 20 19:42:00 2014
@@ -249,8 +249,6 @@ public class ModelAdapterFactory impleme
          * @return true if injection was successful otherwise false
          */
         public boolean inject(AnnotatedElement element, Object value);
-
-        public boolean shouldInjectPrimitiveInitValue();
     }
 
     private static class SetFieldCallback implements InjectCallback {
@@ -265,11 +263,6 @@ public class ModelAdapterFactory impleme
         public boolean inject(AnnotatedElement element, Object value) {
             return setField((Field) element, object, value);
         }
-
-        @Override
-        public boolean shouldInjectPrimitiveInitValue() {
-            return false;
-        }
     }
 
     private static class SetMethodsCallback implements InjectCallback {
@@ -284,13 +277,8 @@ public class ModelAdapterFactory impleme
         public boolean inject(AnnotatedElement element, Object value) {
             return setMethod((Method) element, methods, value);
         }
-
-        @Override
-        public boolean shouldInjectPrimitiveInitValue() {
-            return true;
-        }
     }
-    
+
     private static class SetConstructorParameterCallback implements InjectCallback {
 
         private final List<Object> parameterValues;
@@ -303,15 +291,11 @@ public class ModelAdapterFactory impleme
         public boolean inject(AnnotatedElement element, Object value) {
             return setConstructorParameter((ConstructorParameter)element, parameterValues,
value);
         }
-
-        @Override
-        public boolean shouldInjectPrimitiveInitValue() {
-            return true;
-        }
     }
 
     private boolean injectElement(final AnnotatedElement element, final Object adaptable,
final Type type,
-            final Model modelAnnotation, final DisposalCallbackRegistry registry, InjectCallback
callback) {
+            final boolean injectPrimitiveInitialValue, final Model modelAnnotation, final
DisposalCallbackRegistry registry,
+            final InjectCallback callback) {
 
         InjectAnnotationProcessor annotationProcessor = null;
         String source = getSource(element);
@@ -350,7 +334,7 @@ public class ModelAdapterFactory impleme
         // if default is not set, check if mandatory
         if (!wasInjectionSuccessful) {
             if (isOptional(element, modelAnnotation, annotationProcessor)) {
-                if (callback.shouldInjectPrimitiveInitValue()) {
+                if (injectPrimitiveInitialValue) {
                     injectPrimitiveInitialValue(element, type, callback);
                 }
             } else {
@@ -372,8 +356,13 @@ public class ModelAdapterFactory impleme
         Set<Method> requiredMethods = new HashSet<Method>();
 
         for (Method method : injectableMethods) {
-            Type returnType = mapPrimitiveClasses(method.getGenericReturnType());
-            if (!injectElement(method, adaptable, returnType, modelAnnotation, registry,
callback)) {
+            Type genericReturnType = method.getGenericReturnType();
+            Type returnType = mapPrimitiveClasses(genericReturnType);
+            boolean isPrimitive = false;
+            if (returnType != genericReturnType) {
+                isPrimitive = true;
+            }
+            if (!injectElement(method, adaptable, returnType, isPrimitive, modelAnnotation,
registry, callback)) {
                 requiredMethods.add(method);
             }
         }
@@ -467,7 +456,7 @@ public class ModelAdapterFactory impleme
         Set<Field> injectableFields = collectInjectableFields(type);
         for (Field field : injectableFields) {
             Type fieldType = mapPrimitiveClasses(field.getGenericType());
-            if (!injectElement(field, adaptable, fieldType, modelAnnotation, registry, callback))
{
+            if (!injectElement(field, adaptable, fieldType, false, modelAnnotation, registry,
callback)) {
                 requiredFields.add(field);
             }
         }
@@ -532,9 +521,14 @@ public class ModelAdapterFactory impleme
 
         for (int i = 0; i < parameterTypes.length; i++) {
             Type genericType = mapPrimitiveClasses(parameterTypes[i]);
+
+            boolean isPrimitive = false;
+            if (parameterTypes[i] != genericType) {
+                isPrimitive = true;
+            }
             ConstructorParameter constructorParameter = new ConstructorParameter(
                     constructor.getParameterAnnotations()[i], constructor.getParameterTypes()[i],
genericType, i);
-            if (!injectElement(constructorParameter, adaptable, genericType, modelAnnotation,
registry, callback)) {
+            if (!injectElement(constructorParameter, adaptable, genericType, isPrimitive,
modelAnnotation, registry, callback)) {
                 requiredParameters.add(constructorParameter);
             }
         }

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java?rev=1619212&r1=1619211&r2=1619212&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java
Wed Aug 20 19:42:00 2014
@@ -18,6 +18,7 @@ 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.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -66,6 +67,8 @@ public class OptionalPrimitivesTest {
         org.apache.sling.models.testmodels.classes.OptionalPrimitivesModel model
                 = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.OptionalPrimitivesModel.class);
         assertNotNull(model);
+
+        // make sure primitives are initialized with initial value
         assertEquals(0, model.getByteValue());
         assertEquals(0, model.getShortValue());
         assertEquals(0, model.getIntValue());
@@ -74,7 +77,17 @@ public class OptionalPrimitivesTest {
         assertEquals(0.0d, model.getDoubleValue(), 0.00001d);
         assertEquals('\u0000', model.getCharValue());
         assertEquals(false, model.getBooleanValue());
-    }
+
+        // make sure object wrapper of primitives are null
+        assertNull(model.getByteObjectValue());
+        assertNull(model.getShortObjectValue());
+        assertNull(model.getIntObjectValue());
+        assertNull(model.getLongObjectValue());
+        assertNull(model.getFloatObjectValue());
+        assertNull(model.getDoubleObjectValue());
+        assertNull(model.getCharObjectValue());
+        assertNull(model.getBooleanObjectValue());
+}
 
     @Test
     public void testConstructorInjection() {
@@ -86,6 +99,8 @@ public class OptionalPrimitivesTest {
         org.apache.sling.models.testmodels.classes.constructorinjection.OptionalPrimitivesModel
model
                 = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.OptionalPrimitivesModel.class);
         assertNotNull(model);
+
+        // make sure primitives are initialized with initial value
         assertEquals(0, model.getByteValue());
         assertEquals(0, model.getShortValue());
         assertEquals(0, model.getIntValue());
@@ -94,7 +109,17 @@ public class OptionalPrimitivesTest {
         assertEquals(0.0d, model.getDoubleValue(), 0.00001d);
         assertEquals('\u0000', model.getCharValue());
         assertEquals(false, model.getBooleanValue());
-    }
+
+        // make sure object wrapper of primitives are null
+        assertNull(model.getByteObjectValue());
+        assertNull(model.getShortObjectValue());
+        assertNull(model.getIntObjectValue());
+        assertNull(model.getLongObjectValue());
+        assertNull(model.getFloatObjectValue());
+        assertNull(model.getDoubleObjectValue());
+        assertNull(model.getCharObjectValue());
+        assertNull(model.getBooleanObjectValue());
+}
 
     @Test
     public void testFieldInjectionInterface() {
@@ -106,6 +131,8 @@ public class OptionalPrimitivesTest {
         org.apache.sling.models.testmodels.interfaces.OptionalPrimitivesModel model
                 = factory.getAdapter(res, org.apache.sling.models.testmodels.interfaces.OptionalPrimitivesModel.class);
         assertNotNull(model);
+
+        // make sure primitives are initialized with initial value
         assertEquals(0, model.getByteValue());
         assertEquals(0, model.getShortValue());
         assertEquals(0, model.getIntValue());
@@ -114,6 +141,16 @@ public class OptionalPrimitivesTest {
         assertEquals(0.0d, model.getDoubleValue(), 0.00001d);
         assertEquals('\u0000', model.getCharValue());
         assertEquals(false, model.getBooleanValue());
+
+        // make sure object wrapper of primitives are null
+        assertNull(model.getByteObjectValue());
+        assertNull(model.getShortObjectValue());
+        assertNull(model.getIntObjectValue());
+        assertNull(model.getLongObjectValue());
+        assertNull(model.getFloatObjectValue());
+        assertNull(model.getDoubleObjectValue());
+        assertNull(model.getCharObjectValue());
+        assertNull(model.getBooleanObjectValue());
     }
 
 }

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java?rev=1619212&r1=1619211&r2=1619212&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java
Wed Aug 20 19:42:00 2014
@@ -33,22 +33,46 @@ public class OptionalPrimitivesModel {
 
     @Inject @Optional
     private int intValue;
-    
+
     @Inject @Optional
     private long longValue;
-    
+
     @Inject @Optional
     private float floatValue;
-    
+
     @Inject @Optional
     private double doubleValue;
-    
+
     @Inject @Optional
     private char charValue;
-    
+
     @Inject @Optional
     private boolean booleanValue;
 
+    @Inject @Optional
+    private Byte byteObjectValue;
+
+    @Inject @Optional
+    private Short shortObjectValue;
+
+    @Inject @Optional
+    private Integer intObjectValue;
+
+    @Inject @Optional
+    private Long longObjectValue;
+
+    @Inject @Optional
+    private Float floatObjectValue;
+
+    @Inject @Optional
+    private Double doubleObjectValue;
+
+    @Inject @Optional
+    private Character charObjectValue;
+
+    @Inject @Optional
+    private Boolean booleanObjectValue;
+
     public byte getByteValue() {
         return this.byteValue;
     }
@@ -81,4 +105,36 @@ public class OptionalPrimitivesModel {
         return this.booleanValue;
     }
 
+    public Byte getByteObjectValue() {
+        return this.byteObjectValue;
+    }
+
+    public Short getShortObjectValue() {
+        return this.shortObjectValue;
+    }
+
+    public Integer getIntObjectValue() {
+        return this.intObjectValue;
+    }
+
+    public Long getLongObjectValue() {
+        return this.longObjectValue;
+    }
+
+    public Float getFloatObjectValue() {
+        return this.floatObjectValue;
+    }
+
+    public Double getDoubleObjectValue() {
+        return this.doubleObjectValue;
+    }
+
+    public Character getCharObjectValue() {
+        return this.charObjectValue;
+    }
+
+    public Boolean getBooleanObjectValue() {
+        return this.booleanObjectValue;
+    }
+
 }

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java?rev=1619212&r1=1619211&r2=1619212&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java
Wed Aug 20 19:42:00 2014
@@ -33,7 +33,15 @@ public class OptionalPrimitivesModel {
     private final double doubleValue;
     private final char charValue;
     private final boolean booleanValue;
-    
+    private final Byte byteObjectValue;
+    private final Short shortObjectValue;
+    private final Integer intObjectValue;
+    private final Long longObjectValue;
+    private final Float floatObjectValue;
+    private final Double doubleObjectValue;
+    private final Character charObjectValue;
+    private final Boolean booleanObjectValue;
+
     @Inject
     public OptionalPrimitivesModel(
             @Optional byte byteValue,
@@ -43,7 +51,15 @@ public class OptionalPrimitivesModel {
             @Optional float floatValue,
             @Optional double doubleValue,
             @Optional char charValue,
-            @Optional boolean booleanValue) {
+            @Optional boolean booleanValue,
+            @Optional Byte byteObjectValue,
+            @Optional Short shortObjectValue,
+            @Optional Integer intObjectValue,
+            @Optional Long longObjectValue,
+            @Optional Float floatObjectValue,
+            @Optional Double doubleObjectValue,
+            @Optional Character charObjectValue,
+            @Optional Boolean booleanObjectValue) {
         this.byteValue = byteValue;
         this.shortValue = shortValue;
         this.intValue = intValue;
@@ -52,6 +68,14 @@ public class OptionalPrimitivesModel {
         this.doubleValue = doubleValue;
         this.charValue = charValue;
         this.booleanValue = booleanValue;
+        this.byteObjectValue = byteObjectValue;
+        this.shortObjectValue = shortObjectValue;
+        this.intObjectValue = intObjectValue;
+        this.longObjectValue = longObjectValue;
+        this.floatObjectValue = floatObjectValue;
+        this.doubleObjectValue = doubleObjectValue;
+        this.charObjectValue = charObjectValue;
+        this.booleanObjectValue = booleanObjectValue;
     }
 
     public byte getByteValue() {
@@ -86,4 +110,36 @@ public class OptionalPrimitivesModel {
         return this.booleanValue;
     }
     
+    public Byte getByteObjectValue() {
+        return this.byteObjectValue;
+    }
+
+    public Short getShortObjectValue() {
+        return this.shortObjectValue;
+    }
+
+    public Integer getIntObjectValue() {
+        return this.intObjectValue;
+    }
+
+    public Long getLongObjectValue() {
+        return this.longObjectValue;
+    }
+
+    public Float getFloatObjectValue() {
+        return this.floatObjectValue;
+    }
+
+    public Double getDoubleObjectValue() {
+        return this.doubleObjectValue;
+    }
+
+    public Character getCharObjectValue() {
+        return this.charObjectValue;
+    }
+
+    public Boolean getBooleanObjectValue() {
+        return this.booleanObjectValue;
+    }
+    
 }

Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java?rev=1619212&r1=1619211&r2=1619212&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java
(original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java
Wed Aug 20 19:42:00 2014
@@ -33,20 +33,44 @@ public interface OptionalPrimitivesModel
 
     @Inject @Optional
     public int getIntValue();
-    
+
     @Inject @Optional
     public long getLongValue();
-    
+
     @Inject @Optional
     public float getFloatValue();
-    
+
     @Inject @Optional
     public double getDoubleValue();
-    
+
     @Inject @Optional
     public char getCharValue();
-    
+
     @Inject @Optional
     public boolean getBooleanValue();
-    
+
+    @Inject @Optional
+    public Byte getByteObjectValue();
+
+    @Inject @Optional
+    public Short getShortObjectValue();
+
+    @Inject @Optional
+    public Integer getIntObjectValue();
+
+    @Inject @Optional
+    public Long getLongObjectValue();
+
+    @Inject @Optional
+    public Float getFloatObjectValue();
+
+    @Inject @Optional
+    public Double getDoubleObjectValue();
+
+    @Inject @Optional
+    public Character getCharObjectValue();
+
+    @Inject @Optional
+    public Boolean getBooleanObjectValue();
+
 }



Mime
View raw message