You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:54:56 UTC

[sling-org-apache-sling-models-impl] 03/10: SLING-3429 - fix NPE in OSGiServiceInjector which impacted multi-valued properties

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.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git

commit 53a6c314c6b9595f8776360164f0fa2b5b22d3d6
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Mon Mar 3 17:58:16 2014 +0000

    SLING-3429 - fix NPE in OSGiServiceInjector which impacted multi-valued properties
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1573637 13f79535-47bb-0310-9956-ffa450edef68
---
 .../models/impl/injectors/OSGiServiceInjector.java |  6 ++++
 .../sling/models/impl/OSGiInjectionTest.java       | 25 ++++++++++++++
 .../testmodels/classes/OptionalArrayOSGiModel.java | 36 ++++++++++++++++++++
 .../testmodels/classes/OptionalListOSGiModel.java  | 38 ++++++++++++++++++++++
 4 files changed, 105 insertions(+)

diff --git a/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
index 78a73bb..59a9636 100644
--- a/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
+++ b/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
@@ -148,6 +148,9 @@ public class OSGiServiceInjector implements Injector {
             Class<?> injectedClass = (Class<?>) type;
             if (injectedClass.isArray()) {
                 Object[] services = getServices(adaptable, injectedClass.getComponentType(), filterString, callbackRegistry);
+                if (services == null) {
+                    return null;
+                }
                 Object arr = Array.newInstance(injectedClass.getComponentType(), services.length);
                 for (int i = 0; i < services.length; i++) {
                     Array.set(arr, i, services[i]);
@@ -168,6 +171,9 @@ public class OSGiServiceInjector implements Injector {
 
             Class<?> serviceType = (Class<?>) ptype.getActualTypeArguments()[0];
             Object[] services = getServices(adaptable, serviceType, filterString, callbackRegistry);
+            if (services == null) {
+                return null;
+            }
             return Arrays.asList(services);
         } else {
             log.warn("Cannot handle type {}", type);
diff --git a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
index b26eb13..9812003 100644
--- a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
+++ b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
@@ -30,6 +30,8 @@ import org.apache.sling.models.impl.injectors.OSGiServiceInjector;
 import org.apache.sling.models.testmodels.classes.ArrayOSGiModel;
 import org.apache.sling.models.testmodels.classes.CollectionOSGiModel;
 import org.apache.sling.models.testmodels.classes.ListOSGiModel;
+import org.apache.sling.models.testmodels.classes.OptionalArrayOSGiModel;
+import org.apache.sling.models.testmodels.classes.OptionalListOSGiModel;
 import org.apache.sling.models.testmodels.classes.RequestOSGiModel;
 import org.apache.sling.models.testmodels.classes.SetOSGiModel;
 import org.apache.sling.models.testmodels.classes.SimpleOSGiModel;
@@ -161,6 +163,29 @@ public class OSGiInjectionTest {
     }
 
     @Test
+    public void testOptionalArrayOSGiModel() throws Exception {
+
+        Resource res = mock(Resource.class);
+
+        OptionalArrayOSGiModel model = factory.getAdapter(res, OptionalArrayOSGiModel.class);
+        assertNotNull(model);
+        assertNull(model.getServices());
+
+        verifyNoMoreInteractions(res);
+    }
+
+    @Test
+    public void testOptionalListOSGiModel() throws Exception {
+        Resource res = mock(Resource.class);
+
+        OptionalListOSGiModel model = factory.getAdapter(res, OptionalListOSGiModel.class);
+        assertNotNull(model);
+        assertNull(model.getServices());
+
+        verifyNoMoreInteractions(res);
+    }
+
+    @Test
     public void testCollectionOSGiModel() throws Exception {
         ServiceReference ref1 = mock(ServiceReference.class);
         ServiceInterface service1 = mock(ServiceInterface.class);
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
new file mode 100644
index 0000000..84aac68
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalArrayOSGiModel.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = Resource.class)
+public class OptionalArrayOSGiModel {
+    
+    @Inject @Optional
+    private ServiceInterface[] services;
+    
+    public ServiceInterface[] getServices() {
+        return services;
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
new file mode 100644
index 0000000..b8a2146
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/OptionalListOSGiModel.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.testmodels.classes;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Optional;
+import org.apache.sling.models.testmodels.interfaces.ServiceInterface;
+
+@Model(adaptables = Resource.class)
+public class OptionalListOSGiModel {
+    
+    @Inject @Optional
+    private List<ServiceInterface> services;
+    
+    public List<ServiceInterface> getServices() {
+        return services;
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.