You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by he...@apache.org on 2024/08/23 13:27:16 UTC

(sling-org-apache-sling-testing-osgi-mock) 02/03: Don't re-instantiate reference collection on rebind but just clear it

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

henrykuijpers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit bb6a7165040be246d29badd003225c8fd8d570bc
Author: Bart Thierens <ba...@kbc.be>
AuthorDate: Thu Aug 22 17:56:26 2024 +0200

    Don't re-instantiate reference collection on rebind but just clear it
---
 .../java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index e742af2..05e656d 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -845,15 +845,22 @@ final class OsgiServiceUtil {
         }
     }
 
+    @SuppressWarnings("unchecked")
     private static void bindCollectionReference(
             Reference reference, BundleContext bundleContext, Object target, Field field) {
         try {
             field.setAccessible(true);
+            Collection<Object> collection = (Collection<Object>) field.get(target);
+            if (collection == null) {
+                collection = newCollectionInstance(field.getType());
+            } else {
+                collection.clear();
+            }
+
             List<ServiceInfo<?>> matchingServices =
                     getMatchingServices(reference.getInterfaceTypeAsClass(), bundleContext, reference.getTarget());
             matchingServices.sort(Comparator.comparing(ServiceInfo::getServiceReference));
 
-            Collection<Object> collection = newCollectionInstance(field.getType());
             if (FieldCollectionType.REFERENCE.equals(reference.getFieldCollectionType())) {
                 matchingServices.stream().map(ServiceInfo::getServiceReference).forEach(collection::add);
             } else if (FieldCollectionType.SERVICE.equals(reference.getFieldCollectionType())) {