1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-03-28 14:56:28 +01:00

Bug 568228: Ensure no duplicated classes are registered

Change-Id: I0bcb4764887d73e7b57770e2ff397a06a4247624
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
This commit is contained in:
Torbjörn Svensson 2020-11-19 19:54:49 +01:00 committed by Jonah Graham
parent 163ae5f819
commit 7818b0f921

View file

@ -158,6 +158,8 @@ abstract public class AbstractDsfService implements IDsfService, IDsfStatusConst
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void register(String[] classes, Dictionary properties) {
// Use a HashSet to avoid duplicates
Set<String> classSet = new HashSet<>(Arrays.asList(classes));
/*
* If this service has already been registered, make sure we
@ -167,12 +169,7 @@ abstract public class AbstractDsfService implements IDsfService, IDsfStatusConst
*/
if (fRegistration != null) {
String[] previousClasses = (String[]) fRegistration.getReference().getProperty(Constants.OBJECTCLASS);
// Use a HashSet to avoid duplicates
Set<String> newClasses = new HashSet<>();
newClasses.addAll(Arrays.asList(previousClasses));
newClasses.addAll(Arrays.asList(classes));
classes = newClasses.toArray(new String[0]);
classSet.addAll(Arrays.asList(previousClasses));
/*
* Also keep all previous properties.
@ -192,18 +189,9 @@ abstract public class AbstractDsfService implements IDsfService, IDsfStatusConst
* Ensure that the list of classes contains the base DSF service
* interface, as well as the actual class type of this object.
*/
if (!Arrays.asList(classes).contains(IDsfService.class.getName())) {
String[] newClasses = new String[classes.length + 1];
System.arraycopy(classes, 0, newClasses, 1, classes.length);
newClasses[0] = IDsfService.class.getName();
classes = newClasses;
}
if (!Arrays.asList(classes).contains(getClass().getName())) {
String[] newClasses = new String[classes.length + 1];
System.arraycopy(classes, 0, newClasses, 1, classes.length);
newClasses[0] = getClass().getName();
classes = newClasses;
}
classSet.add(IDsfService.class.getName());
classSet.add(getClass().getName());
/*
* Make sure that the session ID is set in the service properties.
* The session ID distinguishes this service instance from instances
@ -211,7 +199,7 @@ abstract public class AbstractDsfService implements IDsfService, IDsfStatusConst
*/
properties.put(PROP_SESSION_ID, getSession().getId());
fProperties = properties;
fRegistration = getBundleContext().registerService(classes, this, properties);
fRegistration = getBundleContext().registerService(classSet.toArray(String[]::new), this, properties);
/*
* Retrieve the OBJECTCLASS property directly from the service