This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit fd0324df4dae97d2becea26c48a6074d4b2d8835 Author: Justin Edelson <[email protected]> AuthorDate: Wed Aug 20 19:42:00 2014 +0000 SLING-3863 - correcting behavior where optional methods and constructor parameters were injected with non-null wrapper classes. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1619212 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/models/impl/ModelAdapterFactory.java | 42 ++++++-------- .../sling/models/impl/OptionalPrimitivesTest.java | 41 +++++++++++++- .../classes/OptionalPrimitivesModel.java | 66 ++++++++++++++++++++-- .../OptionalPrimitivesModel.java | 60 +++++++++++++++++++- .../interfaces/OptionalPrimitivesModel.java | 36 ++++++++++-- 5 files changed, 206 insertions(+), 39 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 a81cc31..1880e66 100644 --- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java +++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java @@ -249,8 +249,6 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable { * @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 implements AdapterFactory, Runnable { 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 implements AdapterFactory, Runnable { 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 implements AdapterFactory, Runnable { 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 implements AdapterFactory, Runnable { // 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 implements AdapterFactory, Runnable { 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 implements AdapterFactory, Runnable { 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 implements AdapterFactory, Runnable { 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); } } diff --git a/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java b/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java index 7e7637a..0fd45c5 100644 --- a/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java +++ b/src/test/java/org/apache/sling/models/impl/OptionalPrimitivesTest.java @@ -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()); } } diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java index f97afa9..2f9aad2 100644 --- a/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java +++ b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalPrimitivesModel.java @@ -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; + } + } diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java index c6f51c2..e465fc4 100644 --- a/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java +++ b/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/OptionalPrimitivesModel.java @@ -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; + } + } diff --git a/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java b/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java index 8f8ed54..1a07c18 100644 --- a/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java +++ b/src/test/java/org/apache/sling/models/testmodels/interfaces/OptionalPrimitivesModel.java @@ -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(); + } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
