From 6c85b1bccdb64109a42b639700e7a3ca6d39bcb9 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 15 Apr 2020 01:44:37 +0200 Subject: [PATCH] Add and fix more null annotations (#1421) Signed-off-by: Wouter Born --- .../internal/ConnectionValidator.java | 25 +++++---- .../xml/internal/BindingInfoReader.java | 2 + .../xml/internal/BindingInfoXmlProvider.java | 21 ++++---- .../xml/internal/XmlBindingInfoProvider.java | 13 ++--- .../core/config/core/ConfigDescription.java | 7 ++- .../config/xml/AbstractXmlBasedProvider.java | 21 ++++---- .../AbstractXmlConfigDescriptionProvider.java | 2 + .../xml/internal/ConfigDescriptionReader.java | 2 + .../ConfigDescriptionXmlProvider.java | 16 ++---- .../xml/osgi/XmlDocumentBundleTracker.java | 53 +++++++------------ .../config/xml/osgi/XmlDocumentProvider.java | 8 +-- .../xml/osgi/XmlDocumentProviderFactory.java | 3 +- .../config/xml/util/XmlDocumentReader.java | 27 +++++----- .../item/internal/GenericItemProvider.java | 4 +- .../xml/internal/ThingDescriptionReader.java | 2 + .../xml/internal/ThingTypeXmlProvider.java | 47 +++++++--------- .../internal/XmlChannelGroupTypeProvider.java | 2 + .../xml/internal/XmlChannelTypeProvider.java | 2 + .../xml/internal/XmlThingTypeProvider.java | 48 ++++++----------- .../registry/AbstractManagedProvider.java | 7 ++- .../common/registry/AbstractRegistry.java | 2 +- .../core/common/registry/Identifiable.java | 3 +- .../core/common/registry/Registry.java | 3 +- .../core/service/ReadyMarkerUtils.java | 2 + .../factory/ManagedThingProviderOSGiTest.java | 2 +- 25 files changed, 143 insertions(+), 181 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ConnectionValidator.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ConnectionValidator.java index eb8ea658c..8a1a46738 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ConnectionValidator.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/ConnectionValidator.java @@ -22,6 +22,8 @@ import java.util.Set; import java.util.regex.Pattern; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.automation.Action; import org.openhab.core.automation.Condition; import org.openhab.core.automation.Module; @@ -43,6 +45,7 @@ import org.openhab.core.automation.type.TriggerType; * @author Benedikt Niehues - validation of connection-types respects inheriting types * @author Ana Dimova - new reference syntax: list[index], map["key"], bean.field */ +@NonNullByDefault public class ConnectionValidator { public static final String CONFIG_REFERENCE_PATTERN = "\\${1}\\{{1}[A-Za-z0-9_-]+\\}{1}|\\${1}[A-Za-z0-9_-]+"; @@ -58,9 +61,6 @@ public class ConnectionValidator { * @throws IllegalArgumentException when validation fails. */ public static void validateConnections(ModuleTypeRegistry mtRegistry, Rule r) { - if (r == null) { - throw new IllegalArgumentException("Validation of rule has failed! Rule must not be null!"); - } validateConnections(mtRegistry, r.getTriggers(), r.getConditions(), r.getActions()); } @@ -74,9 +74,8 @@ public class ConnectionValidator { * @param actions is a list with actions of the rule whose connections have to be validated * @throws IllegalArgumentException when validation fails. */ - public static void validateConnections(ModuleTypeRegistry mtRegistry, - @NonNull List triggers, @NonNull List conditions, - @NonNull List actions) { + public static void validateConnections(ModuleTypeRegistry mtRegistry, List triggers, + List conditions, List actions) { if (!triggers.isEmpty()) { for (Condition condition : conditions) { validateConditionConnections(mtRegistry, condition, triggers); @@ -100,7 +99,7 @@ public class ConnectionValidator { * @throws IllegalArgumentException when validation fails. */ private static void validateActionConnections(ModuleTypeRegistry mtRegistry, Action action, - @NonNull List triggers, @NonNull List actions) { + List triggers, List actions) { ActionType type = (ActionType) mtRegistry.get(action.getTypeUID()); // get module type of the condition if (type == null) { // if module type not exists in the system - throws exception @@ -146,7 +145,7 @@ public class ConnectionValidator { * @throws IllegalArgumentException when validation fails. */ private static void checkConnection(ModuleTypeRegistry mtRegistry, Connection connection, Input input, - @NonNull List triggers, @NonNull List actions) { + List triggers, List actions) { Map actionsMap = new HashMap<>(); for (Action a : actions) { actionsMap.put(a.getId(), a); @@ -175,8 +174,8 @@ public class ConnectionValidator { * @param triggers is a list with triggers of the rule on which the condition belongs * @throws IllegalArgumentException when validation fails. */ - private static void validateConditionConnections(ModuleTypeRegistry mtRegistry, @NonNull Condition condition, - @NonNull List triggers) { + private static void validateConditionConnections(ModuleTypeRegistry mtRegistry, Condition condition, + List triggers) { ConditionType type = (ConditionType) mtRegistry.get(condition.getTypeUID()); // get module type of the condition if (type == null) { // if module type not exists in the system - throws exception @@ -221,7 +220,7 @@ public class ConnectionValidator { * @throws IllegalArgumentException when validation fails. */ private static void checkConnection(ModuleTypeRegistry mtRegistry, Connection connection, Input input, - @NonNull List triggers) { + List triggers) { Map triggersMap = new HashMap<>(); for (Trigger trigger : triggers) { triggersMap.put(trigger.getId(), trigger); @@ -261,7 +260,7 @@ public class ConnectionValidator { return; } String outputName = connection.getOutputName(); - if (outputs != null && !outputs.isEmpty()) { + if (!outputs.isEmpty()) { for (Output output : outputs) { if (output.getName().equals(outputName)) { if (input.getType().equals("*")) { @@ -311,7 +310,7 @@ public class ConnectionValidator { return connections; } - private static Connection getConnection(String inputName, String reference) { + private static Connection getConnection(String inputName, @Nullable String reference) { if (reference == null || !Pattern.matches(CONNECTION_PATTERN, reference)) { throw new IllegalArgumentException("Wrong format of Connection : " + inputName + ": " + reference); } diff --git a/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoReader.java b/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoReader.java index 88bea5511..5d1f62d58 100644 --- a/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoReader.java +++ b/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoReader.java @@ -14,6 +14,7 @@ package org.openhab.core.binding.xml.internal; import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionParameter; import org.openhab.core.config.core.ConfigDescriptionParameterGroup; @@ -41,6 +42,7 @@ import com.thoughtworks.xstream.XStream; * @author Alex Tugarev - Extended by options and filter criteria * @author Chris Jackson - Add parameter groups */ +@NonNullByDefault public class BindingInfoReader extends XmlDocumentReader { /** diff --git a/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoXmlProvider.java b/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoXmlProvider.java index b20f1959e..60daef437 100644 --- a/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoXmlProvider.java +++ b/bundles/org.openhab.core.binding.xml/src/main/java/org/openhab/core/binding/xml/internal/BindingInfoXmlProvider.java @@ -13,7 +13,6 @@ package org.openhab.core.binding.xml.internal; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.binding.BindingInfo; import org.openhab.core.binding.BindingInfoProvider; import org.openhab.core.config.core.ConfigDescription; @@ -68,20 +67,18 @@ public class BindingInfoXmlProvider implements XmlDocumentProvider implements BindingInfoProvider, XmlDocumentProviderFactory { @@ -65,8 +67,10 @@ public class XmlBindingInfoProvider extends AbstractXmlBasedProvider(getAll(locale)); } - @Reference(target = "(esh.scope=core.xml.binding)") - public void setConfigDescriptionProvider(ConfigDescriptionProvider configDescriptionProvider) { - this.configDescriptionProvider = (AbstractXmlConfigDescriptionProvider) configDescriptionProvider; - } - - public void unsetConfigDescriptionProvider(ConfigDescriptionProvider configDescriptionProvider) { - this.configDescriptionProvider = null; - } - @Override protected @Nullable BindingInfo localize(Bundle bundle, BindingInfo bindingInfo, @Nullable Locale locale) { return bindingI18nService.createLocalizedBindingInfo(bundle, bindingInfo, locale); diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescription.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescription.java index c66572845..8765e9bde 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescription.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescription.java @@ -18,6 +18,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.registry.Identifiable; /** @@ -42,6 +44,7 @@ import org.openhab.core.common.registry.Identifiable; * @author Chris Jackson - Added parameter groups * @author Thomas Höfer - Added convenient operation to get config description parameters in a map */ +@NonNullByDefault public class ConfigDescription implements Identifiable { private final URI uri; @@ -90,8 +93,8 @@ public class ConfigDescription implements Identifiable { * @throws IllegalArgumentException if the URI is null or invalid */ @Deprecated - public ConfigDescription(URI uri, List parameters, - List groups) { + public ConfigDescription(@Nullable URI uri, @Nullable List parameters, + @Nullable List groups) { if (uri == null) { throw new IllegalArgumentException("The URI must not be null!"); } diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/AbstractXmlBasedProvider.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/AbstractXmlBasedProvider.java index 86ef99310..f20e7c6ee 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/AbstractXmlBasedProvider.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/AbstractXmlBasedProvider.java @@ -23,6 +23,8 @@ import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.registry.Identifiable; import org.openhab.core.i18n.LocalizedKey; @@ -36,9 +38,11 @@ import org.osgi.framework.Bundle; * @param the key type, e.g. ThingTypeUID, ChannelUID, URI,... * @param the object type, e.g. ThingType, ChannelType, ConfigDescription,... */ -public abstract class AbstractXmlBasedProvider> { +@NonNullByDefault +public abstract class AbstractXmlBasedProvider<@NonNull T_ID, @NonNull T_OBJECT extends Identifiable<@NonNull T_ID>> { private final Map> bundleObjectMap = new ConcurrentHashMap<>(); + private final Map localizedObjectCache = new ConcurrentHashMap<>(); /** @@ -72,7 +76,7 @@ public abstract class AbstractXmlBasedProvider objectList) { - if (objectList == null || objectList.isEmpty()) { + if (objectList.isEmpty()) { return; } List objects = acquireObjects(bundle); @@ -86,10 +90,7 @@ public abstract class AbstractXmlBasedProvider acquireObjects(Bundle bundle) { - if (bundle == null) { - return null; - } + private @Nullable List acquireObjects(Bundle bundle) { List objects = bundleObjectMap.get(bundle); if (objects == null) { objects = new CopyOnWriteArrayList<>(); @@ -141,9 +142,6 @@ public abstract class AbstractXmlBasedProvider objects = bundleObjectMap.remove(bundle); if (objects != null) { removeCachedEntries(objects); @@ -173,9 +171,12 @@ public abstract class AbstractXmlBasedProvider implements ConfigDescriptionProvider { diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionReader.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionReader.java index 657f37541..9e2c14bf9 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionReader.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionReader.java @@ -14,6 +14,7 @@ package org.openhab.core.config.xml.internal; import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionParameter; import org.openhab.core.config.core.ConfigDescriptionParameterGroup; @@ -42,6 +43,7 @@ import com.thoughtworks.xstream.XStream; * @author Alex Tugarev - Extended for options and filter criteria * @author Chris Jackson - Added configuration groups */ +@NonNullByDefault public class ConfigDescriptionReader extends XmlDocumentReader> { /** diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionXmlProvider.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionXmlProvider.java index 57118239f..aa9e5f81b 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionXmlProvider.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/internal/ConfigDescriptionXmlProvider.java @@ -14,6 +14,7 @@ package org.openhab.core.config.xml.internal; import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionProvider; import org.openhab.core.config.xml.AbstractXmlConfigDescriptionProvider; @@ -32,28 +33,21 @@ import org.osgi.framework.Bundle; * * @see ConfigDescriptionXmlProviderFactory */ +@NonNullByDefault public class ConfigDescriptionXmlProvider implements XmlDocumentProvider> { - private Bundle bundle; - private AbstractXmlConfigDescriptionProvider configDescriptionProvider; + private final Bundle bundle; + private final AbstractXmlConfigDescriptionProvider configDescriptionProvider; public ConfigDescriptionXmlProvider(Bundle bundle, AbstractXmlConfigDescriptionProvider configDescriptionProvider) throws IllegalArgumentException { - if (bundle == null) { - throw new IllegalArgumentException("The Bundle must not be null!"); - } - - if (configDescriptionProvider == null) { - throw new IllegalArgumentException("The XmlConfigDescriptionProvider must not be null!"); - } - this.bundle = bundle; this.configDescriptionProvider = configDescriptionProvider; } @Override public synchronized void addingObject(List configDescriptions) { - this.configDescriptionProvider.addAll(this.bundle, configDescriptions); + this.configDescriptionProvider.addAll(bundle, configDescriptions); } @Override diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentBundleTracker.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentBundleTracker.java index d27200a9f..1c0ecd108 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentBundleTracker.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentBundleTracker.java @@ -32,6 +32,9 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Supplier; import java.util.stream.Collectors; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.config.xml.util.XmlDocumentReader; import org.openhab.core.service.ReadyMarker; @@ -61,7 +64,8 @@ import org.slf4j.LoggerFactory; * * @param the result type of the conversion */ -public class XmlDocumentBundleTracker extends BundleTracker { +@NonNullByDefault +public class XmlDocumentBundleTracker<@NonNull T> extends BundleTracker { /** * Execute given method while take the lock and unlock before return. @@ -108,8 +112,7 @@ public class XmlDocumentBundleTracker extends BundleTracker { private final ReadWriteLock lockOpenState = new ReentrantReadWriteLock(); private OpenState openState = OpenState.CREATED; - @SuppressWarnings("rawtypes") - private BundleTracker relevantBundlesTracker; + private @Nullable BundleTracker relevantBundlesTracker; private final ReadyService readyService; /** @@ -129,22 +132,6 @@ public class XmlDocumentBundleTracker extends BundleTracker { String readyMarkerKey, ReadyService readyService) throws IllegalArgumentException { super(bundleContext, Bundle.ACTIVE, null); - if (bundleContext == null) { - throw new IllegalArgumentException("The BundleContext must not be null!"); - } - if ((xmlDirectory == null) || (xmlDirectory.isEmpty())) { - throw new IllegalArgumentException("The XML directory must neither be null, nor empty!"); - } - if (xmlDocumentTypeReader == null) { - throw new IllegalArgumentException("The XmlDocumentTypeReader must not be null!"); - } - if (xmlDocumentProviderFactory == null) { - throw new IllegalArgumentException("The XmlDocumentProviderFactory must not be null!"); - } - if (readyService == null) { - throw new IllegalArgumentException("The ReadyService must not be null!"); - } - this.readyMarkerKey = readyMarkerKey; this.xmlDirectory = xmlDirectory; this.xmlDocumentTypeReader = xmlDocumentTypeReader; @@ -161,10 +148,11 @@ public class XmlDocumentBundleTracker extends BundleTracker { } private Set getRelevantBundles() { - if (relevantBundlesTracker.getBundles() == null) { + BundleTracker bundleTracker = relevantBundlesTracker; + if (bundleTracker == null || bundleTracker.getBundles() == null) { return Collections.emptySet(); } - return Arrays.stream(relevantBundlesTracker.getBundles()).collect(Collectors.toSet()); + return (Set) Arrays.stream(bundleTracker.getBundles()).collect(Collectors.toSet()); } @Override @@ -184,7 +172,8 @@ public class XmlDocumentBundleTracker extends BundleTracker { relevantBundlesTracker = new BundleTracker(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, null) { @Override - public Object addingBundle(Bundle bundle, BundleEvent event) { + public @Nullable Object addingBundle(@NonNullByDefault({}) Bundle bundle, + @NonNullByDefault({}) BundleEvent event) { return withLock(lockOpenState.readLock(), () -> openState == OpenState.OPENED && isBundleRelevant(bundle) ? bundle : null); } @@ -216,11 +205,7 @@ public class XmlDocumentBundleTracker extends BundleTracker { queue.clear(); } - private XmlDocumentProvider acquireXmlDocumentProvider(Bundle bundle) { - if (bundle == null) { - return null; - } - + private @Nullable XmlDocumentProvider acquireXmlDocumentProvider(Bundle bundle) { XmlDocumentProvider xmlDocumentProvider = bundleDocumentProviderMap.get(bundle); if (xmlDocumentProvider == null) { xmlDocumentProvider = xmlDocumentProviderFactory.createDocumentProvider(bundle); @@ -232,9 +217,6 @@ public class XmlDocumentBundleTracker extends BundleTracker { } private void releaseXmlDocumentProvider(Bundle bundle) { - if (bundle == null) { - return; - } XmlDocumentProvider xmlDocumentProvider = bundleDocumentProviderMap.get(bundle); if (xmlDocumentProvider == null) { return; @@ -270,13 +252,15 @@ public class XmlDocumentBundleTracker extends BundleTracker { } @Override - public final synchronized Bundle addingBundle(Bundle bundle, BundleEvent event) { + public final synchronized Bundle addingBundle(@NonNullByDefault({}) Bundle bundle, + @NonNullByDefault({}) BundleEvent event) { addingBundle(bundle); return bundle; } @Override - public final synchronized void removedBundle(Bundle bundle, BundleEvent event, Bundle object) { + public final synchronized void removedBundle(@NonNullByDefault({}) Bundle bundle, + @NonNullByDefault({}) BundleEvent event, Bundle object) { logger.trace("Removing the XML related objects from module '{}'...", ReadyMarkerUtils.getIdentifier(bundle)); finishedBundles.remove(bundle); Future future = queue.remove(bundle); @@ -408,8 +392,11 @@ public class XmlDocumentBundleTracker extends BundleTracker { String xmlDocumentFile = xmlDocumentURL.getFile(); logger.debug("Reading the XML document '{}' in module '{}'...", xmlDocumentFile, moduleName); try { + @Nullable T object = xmlDocumentTypeReader.readFromXML(xmlDocumentURL); - addingObject(bundle, object); + if (object != null) { + addingObject(bundle, object); + } numberOfParsedXmlDocuments++; } catch (Exception ex) { // If we are not open, we can stop here. diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProvider.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProvider.java index b92d4ba62..e40c85355 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProvider.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProvider.java @@ -12,8 +12,8 @@ */ package org.openhab.core.config.xml.osgi; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.config.xml.internal.ConfigDescriptionReader; /** @@ -27,14 +27,14 @@ import org.openhab.core.config.xml.internal.ConfigDescriptionReader; * @param the result type of the conversion */ @NonNullByDefault -public interface XmlDocumentProvider { +public interface XmlDocumentProvider<@NonNull T> { /** * Adds a new result object from the XML processing for further processing. * - * @param object the result object to be processed (could be null) + * @param object the result object to be processed */ - void addingObject(@Nullable T object); + void addingObject(T object); /** * Signals that all available result objects from the XML processing of the diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProviderFactory.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProviderFactory.java index cbc115bae..65e0337af 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProviderFactory.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/osgi/XmlDocumentProviderFactory.java @@ -12,6 +12,7 @@ */ package org.openhab.core.config.xml.osgi; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.osgi.framework.Bundle; @@ -29,7 +30,7 @@ import org.osgi.framework.Bundle; * @see XmlDocumentProvider */ @NonNullByDefault -public interface XmlDocumentProviderFactory { +public interface XmlDocumentProviderFactory<@NonNull T> { /** * Creates an XML document provider for the specified module which is used to process diff --git a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/util/XmlDocumentReader.java b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/util/XmlDocumentReader.java index afce0158b..c8401391c 100644 --- a/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/util/XmlDocumentReader.java +++ b/bundles/org.openhab.core.config.xml/src/main/java/org/openhab/core/config/xml/util/XmlDocumentReader.java @@ -14,6 +14,10 @@ package org.openhab.core.config.xml.util; import java.net.URL; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.ConversionException; import com.thoughtworks.xstream.converters.Converter; @@ -29,21 +33,18 @@ import com.thoughtworks.xstream.io.xml.StaxDriver; * * @param the result type of the conversion */ -public abstract class XmlDocumentReader { +@NonNullByDefault +public abstract class XmlDocumentReader<@NonNull T> { - private XStream xstream; + private final XStream xstream = new XStream(new StaxDriver()); /** * The default constructor of this class initializes the {@code XStream} object, and calls * the abstract methods {@link #registerConverters()} and {@link #registerAliases()}. */ public XmlDocumentReader() { - StaxDriver driver = new StaxDriver(); - - this.xstream = new XStream(driver); - - registerConverters(this.xstream); - registerAliases(this.xstream); + registerConverters(xstream); + registerAliases(xstream); } /** @@ -52,7 +53,7 @@ public abstract class XmlDocumentReader { * @param classLoader the classloader to set (must not be null) */ public void setClassLoader(ClassLoader classLoader) { - this.xstream.setClassLoader(classLoader); + xstream.setClassLoader(classLoader); } /** @@ -80,12 +81,8 @@ public abstract class XmlDocumentReader { * @throws ConversionException if the specified document contains invalid content */ @SuppressWarnings("unchecked") - public T readFromXML(URL xmlURL) throws ConversionException { - if (xmlURL != null) { - return (T) this.xstream.fromXML(xmlURL); - } - - return null; + public @Nullable T readFromXML(URL xmlURL) throws ConversionException { + return (@Nullable T) xstream.fromXML(xmlURL); } } diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java index a498f1503..e0d50db8c 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java @@ -25,7 +25,6 @@ import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import org.eclipse.emf.common.util.EList; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.registry.AbstractProvider; @@ -512,8 +511,7 @@ public class GenericItemProvider extends AbstractProvider } @Override - public @Nullable StateDescriptionFragment getStateDescriptionFragment(@NonNull String itemName, - @Nullable Locale locale) { + public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) { return stateDescriptionFragments.get(itemName); } diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java index 966e882ff..f6e5c26fe 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingDescriptionReader.java @@ -14,6 +14,7 @@ package org.openhab.core.thing.xml.internal; import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionParameter; import org.openhab.core.config.core.ConfigDescriptionParameterGroup; @@ -48,6 +49,7 @@ import com.thoughtworks.xstream.XStream; * @author Chris Jackson - Added parameter groups and channel properties * @author Moritz Kammerer - Added triggers */ +@NonNullByDefault public class ThingDescriptionReader extends XmlDocumentReader> { /** diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingTypeXmlProvider.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingTypeXmlProvider.java index c9e412fa9..ae6538f39 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingTypeXmlProvider.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/ThingTypeXmlProvider.java @@ -15,6 +15,8 @@ package org.openhab.core.thing.xml.internal; import java.util.ArrayList; import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionProvider; import org.openhab.core.config.xml.AbstractXmlConfigDescriptionProvider; @@ -51,6 +53,7 @@ import com.thoughtworks.xstream.converters.ConversionException; * @author Michael Grammling - Initial contribution * @author Ivan Iliev - Added support for system wide channel types */ +@NonNullByDefault public class ThingTypeXmlProvider implements XmlDocumentProvider> { private final Logger logger = LoggerFactory.getLogger(ThingTypeXmlProvider.class); @@ -70,18 +73,6 @@ public class ThingTypeXmlProvider implements XmlDocumentProvider> { public ThingTypeXmlProvider(Bundle bundle, AbstractXmlConfigDescriptionProvider configDescriptionProvider, XmlThingTypeProvider thingTypeProvider, XmlChannelTypeProvider channelTypeProvider, XmlChannelGroupTypeProvider channelGroupTypeProvider) throws IllegalArgumentException { - if (bundle == null) { - throw new IllegalArgumentException("The Bundle must not be null!"); - } - - if (configDescriptionProvider == null) { - throw new IllegalArgumentException("The XmlConfigDescriptionProvider must not be null!"); - } - - if (thingTypeProvider == null) { - throw new IllegalArgumentException("The XmlThingTypeProvider must not be null!"); - } - this.bundle = bundle; this.configDescriptionProvider = configDescriptionProvider; this.thingTypeProvider = thingTypeProvider; @@ -91,27 +82,25 @@ public class ThingTypeXmlProvider implements XmlDocumentProvider> { @Override public synchronized void addingObject(List types) { - if (types != null) { - for (Object type : types) { - if (type instanceof ThingTypeXmlResult) { - ThingTypeXmlResult typeResult = (ThingTypeXmlResult) type; - addConfigDescription(typeResult.getConfigDescription()); - thingTypeRefs.add(typeResult); - } else if (type instanceof ChannelGroupTypeXmlResult) { - ChannelGroupTypeXmlResult typeResult = (ChannelGroupTypeXmlResult) type; - channelGroupTypeRefs.add(typeResult); - } else if (type instanceof ChannelTypeXmlResult) { - ChannelTypeXmlResult typeResult = (ChannelTypeXmlResult) type; - channelTypeRefs.add(typeResult); - addConfigDescription(typeResult.getConfigDescription()); - } else { - throw new ConversionException("Unknown data type for '" + type + "'!"); - } + for (Object type : types) { + if (type instanceof ThingTypeXmlResult) { + ThingTypeXmlResult typeResult = (ThingTypeXmlResult) type; + addConfigDescription(typeResult.getConfigDescription()); + thingTypeRefs.add(typeResult); + } else if (type instanceof ChannelGroupTypeXmlResult) { + ChannelGroupTypeXmlResult typeResult = (ChannelGroupTypeXmlResult) type; + channelGroupTypeRefs.add(typeResult); + } else if (type instanceof ChannelTypeXmlResult) { + ChannelTypeXmlResult typeResult = (ChannelTypeXmlResult) type; + channelTypeRefs.add(typeResult); + addConfigDescription(typeResult.getConfigDescription()); + } else { + throw new ConversionException("Unknown data type for '" + type + "'!"); } } } - private void addConfigDescription(ConfigDescription configDescription) { + private void addConfigDescription(@Nullable ConfigDescription configDescription) { if (configDescription != null) { try { configDescriptionProvider.add(bundle, configDescription); diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelGroupTypeProvider.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelGroupTypeProvider.java index 6ccaab2a6..d6bbff2d4 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelGroupTypeProvider.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelGroupTypeProvider.java @@ -15,6 +15,7 @@ package org.openhab.core.thing.xml.internal; import java.util.Collection; import java.util.Locale; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.config.xml.AbstractXmlBasedProvider; import org.openhab.core.thing.UID; @@ -33,6 +34,7 @@ import org.osgi.service.component.annotations.Reference; * @author Simon Kaufmann - Initial contribution * @author Christoph Weitkamp - factored out common aspects into ThingTypeI18nLocalizationService */ +@NonNullByDefault @Component(property = { "esh.scope=core.xml.channelGroups" }) public class XmlChannelGroupTypeProvider extends AbstractXmlBasedProvider implements ChannelGroupTypeProvider { diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelTypeProvider.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelTypeProvider.java index ece168186..56d93e9a3 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelTypeProvider.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlChannelTypeProvider.java @@ -15,6 +15,7 @@ package org.openhab.core.thing.xml.internal; import java.util.Collection; import java.util.Locale; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.config.xml.AbstractXmlBasedProvider; import org.openhab.core.thing.UID; @@ -36,6 +37,7 @@ import org.osgi.service.component.annotations.Reference; * @author Henning Treu - QuantityType implementation * @author Christoph Weitkamp - factored out common aspects into ThingTypeI18nLocalizationService */ +@NonNullByDefault @Component(property = { "esh.scope=core.xml.channels" }) public class XmlChannelTypeProvider extends AbstractXmlBasedProvider implements ChannelTypeProvider { diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlThingTypeProvider.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlThingTypeProvider.java index afb04d358..28944c9e8 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlThingTypeProvider.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/openhab/core/thing/xml/internal/XmlThingTypeProvider.java @@ -18,6 +18,7 @@ import java.util.Locale; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.config.core.ConfigDescriptionProvider; @@ -54,6 +55,7 @@ import org.osgi.service.component.annotations.Reference; * @author Kai Kreuzer - fixed concurrency issues * @author Simon Kaufmann - factored out common aspects into {@link AbstractXmlBasedProvider} */ +@NonNullByDefault @Component(property = { "esh.scope=core.xml.thing" }) public class XmlThingTypeProvider extends AbstractXmlBasedProvider implements ThingTypeProvider, XmlDocumentProviderFactory> { @@ -62,9 +64,9 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider> thingTypeTracker; private final ReadyService readyService; private final ScheduledExecutorService scheduler = ThreadPoolManager @@ -72,10 +74,17 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider trackerJob; @Activate - public XmlThingTypeProvider(final @Reference ThingTypeI18nLocalizationService thingTypeI18nLocalizationService, - final @Reference ReadyService readyService) { - this.thingTypeI18nLocalizationService = thingTypeI18nLocalizationService; + public XmlThingTypeProvider( + final @Reference(target = "(esh.scope=core.xml.channelGroups)") ChannelGroupTypeProvider channelGroupTypeProvider, + final @Reference(target = "(esh.scope=core.xml.channels)") ChannelTypeProvider channelTypeProvider, + final @Reference(target = "(esh.scope=core.xml.thing)") ConfigDescriptionProvider configDescriptionProvider, + final @Reference ReadyService readyService, + final @Reference ThingTypeI18nLocalizationService thingTypeI18nLocalizationService) { + this.channelGroupTypeProvider = (XmlChannelGroupTypeProvider) channelGroupTypeProvider; + this.channelTypeProvider = (XmlChannelTypeProvider) channelTypeProvider; + this.configDescriptionProvider = (AbstractXmlConfigDescriptionProvider) configDescriptionProvider; this.readyService = readyService; + this.thingTypeI18nLocalizationService = thingTypeI18nLocalizationService; } @Activate @@ -112,33 +121,6 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider, @NonNull K, PE> +public abstract class AbstractManagedProvider<@NonNull E extends Identifiable, @NonNull K, @NonNull PE> extends AbstractProvider implements ManagedProvider { private volatile Storage storage; @@ -80,6 +80,7 @@ public abstract class AbstractManagedProvider<@NonNull E extends Identifiable } private @Nullable E getElement(String key) { + @Nullable PE persistableElement = storage.get(key); return persistableElement != null ? toElement(key, persistableElement) : null; } @@ -87,6 +88,7 @@ public abstract class AbstractManagedProvider<@NonNull E extends Identifiable @Override public @Nullable E remove(K key) { String keyAsString = keyToString(key); + @Nullable PE persistableElement = storage.remove(keyAsString); if (persistableElement != null) { @Nullable @@ -105,6 +107,7 @@ public abstract class AbstractManagedProvider<@NonNull E extends Identifiable public @Nullable E update(E element) { String key = getKeyAsString(element); if (storage.get(key) != null) { + @Nullable PE persistableElement = storage.put(key, toPersistableElement(element)); if (persistableElement != null) { @Nullable @@ -150,7 +153,7 @@ public abstract class AbstractManagedProvider<@NonNull E extends Identifiable * @param persistableElement persistable element * @return original element */ - protected abstract @Nullable E toElement(String key, @NonNull PE persistableElement); + protected abstract @Nullable E toElement(String key, PE persistableElement); /** * Converts the original element into an element that can be persisted. diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java index b638a41b2..9dc22cb61 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/AbstractRegistry.java @@ -199,7 +199,7 @@ public abstract class AbstractRegistry<@NonNull E extends Identifiable, @NonN } @Override - public Collection<@NonNull E> getAll() { + public Collection getAll() { elementReadLock.lock(); try { return new HashSet<>(elements); diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Identifiable.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Identifiable.java index 71f43f48c..0b14dc43b 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Identifiable.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Identifiable.java @@ -12,6 +12,7 @@ */ package org.openhab.core.common.registry; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -20,7 +21,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; * @author Markus Rathgeb - Initial contribution */ @NonNullByDefault -public interface Identifiable { +public interface Identifiable<@NonNull T> { /** * Get the unique identifier. diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Registry.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Registry.java index 1059fd79c..8d994c4ec 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Registry.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/registry/Registry.java @@ -15,6 +15,7 @@ package org.openhab.core.common.registry; import java.util.Collection; import java.util.stream.Stream; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -29,7 +30,7 @@ import org.eclipse.jdt.annotation.Nullable; * @param type of the elements in the registry */ @NonNullByDefault -public interface Registry, K> { +public interface Registry<@NonNull E extends Identifiable, @NonNull K> { /** * Adds a {@link RegistryChangeListener} to the registry. diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/service/ReadyMarkerUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/service/ReadyMarkerUtils.java index 5069a8a0f..99660aebd 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/service/ReadyMarkerUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/service/ReadyMarkerUtils.java @@ -14,6 +14,7 @@ package org.openhab.core.service; import java.util.Arrays; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.osgi.framework.Bundle; /** @@ -21,6 +22,7 @@ import org.osgi.framework.Bundle; * * @author Markus Rathgeb - Initial contribution */ +@NonNullByDefault public class ReadyMarkerUtils { /** diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/factory/ManagedThingProviderOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/factory/ManagedThingProviderOSGiTest.java index 3135c22fd..da45000a0 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/factory/ManagedThingProviderOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/factory/ManagedThingProviderOSGiTest.java @@ -63,7 +63,7 @@ public class ManagedThingProviderOSGiTest extends JavaOSGiTest { managedThingProvider.getAll().forEach(t -> managedThingProvider.remove(t.getUID())); } - private void registerThingsChangeListener(ProviderChangeListener<@NonNull Thing> thingChangeListener) { + private void registerThingsChangeListener(ProviderChangeListener thingChangeListener) { unregisterCurrentThingsChangeListener(); this.thingChangeListener = thingChangeListener; managedThingProvider.addProviderChangeListener(this.thingChangeListener);