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 <[email protected]>
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())) {

Reply via email to