Add and fix more null annotations (#1421)

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2020-04-15 01:44:37 +02:00 committed by GitHub
parent b770bb1b4a
commit 6c85b1bccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 143 additions and 181 deletions

View File

@ -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<? extends @NonNull Trigger> triggers, @NonNull List<? extends @NonNull Condition> conditions,
@NonNull List<? extends @NonNull Action> actions) {
public static void validateConnections(ModuleTypeRegistry mtRegistry, List<? extends @NonNull Trigger> triggers,
List<? extends @NonNull Condition> conditions, List<? extends @NonNull Action> 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<? extends @NonNull Trigger> triggers, @NonNull List<? extends @NonNull Action> actions) {
List<? extends @NonNull Trigger> triggers, List<? extends @NonNull Action> 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<? extends @NonNull Trigger> triggers, @NonNull List<? extends @NonNull Action> actions) {
List<? extends @NonNull Trigger> triggers, List<? extends @NonNull Action> actions) {
Map<String, Action> 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<? extends @NonNull Trigger> triggers) {
private static void validateConditionConnections(ModuleTypeRegistry mtRegistry, Condition condition,
List<? extends @NonNull Trigger> 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<? extends @NonNull Trigger> triggers) {
List<? extends @NonNull Trigger> triggers) {
Map<String, Trigger> 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);
}

View File

@ -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<BindingInfoXmlResult> {
/**

View File

@ -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<BindingInfoXm
}
@Override
public synchronized void addingObject(@Nullable BindingInfoXmlResult bindingInfoXmlResult) {
if (bindingInfoXmlResult != null) {
ConfigDescription configDescription = bindingInfoXmlResult.getConfigDescription();
public synchronized void addingObject(BindingInfoXmlResult bindingInfoXmlResult) {
ConfigDescription configDescription = bindingInfoXmlResult.getConfigDescription();
if (configDescription != null) {
try {
configDescriptionProvider.add(bundle, configDescription);
} catch (Exception ex) {
logger.error("Could not register ConfigDescription!", ex);
}
if (configDescription != null) {
try {
configDescriptionProvider.add(bundle, configDescription);
} catch (Exception ex) {
logger.error("Could not register ConfigDescription!", ex);
}
bindingInfoProvider.add(bundle, bindingInfoXmlResult.getBindingInfo());
}
bindingInfoProvider.add(bundle, bindingInfoXmlResult.getBindingInfo());
}
@Override

View File

@ -18,6 +18,7 @@ import java.util.Set;
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.binding.BindingInfo;
import org.openhab.core.binding.BindingInfoProvider;
@ -48,6 +49,7 @@ import org.osgi.service.component.annotations.Reference;
* @author Michael Grammling - Refactoring: Provider/Registry pattern is used, added locale support
* @author Simon Kaufmann - factored out common aspects into {@link AbstractXmlBasedProvider}
*/
@NonNullByDefault
@Component
public class XmlBindingInfoProvider extends AbstractXmlBasedProvider<String, BindingInfo>
implements BindingInfoProvider, XmlDocumentProviderFactory<BindingInfoXmlResult> {
@ -65,8 +67,10 @@ public class XmlBindingInfoProvider extends AbstractXmlBasedProvider<String, Bin
@Activate
public XmlBindingInfoProvider(final @Reference BindingI18nLocalizationService bindingI18nService,
final @Reference(target = "(esh.scope=core.xml.binding)") ConfigDescriptionProvider configDescriptionProvider,
final @Reference ReadyService readyService) {
this.bindingI18nService = bindingI18nService;
this.configDescriptionProvider = (AbstractXmlConfigDescriptionProvider) configDescriptionProvider;
this.readyService = readyService;
}
@ -104,15 +108,6 @@ public class XmlBindingInfoProvider extends AbstractXmlBasedProvider<String, Bin
return new HashSet<>(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);

View File

@ -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<URI> {
private final URI uri;
@ -90,8 +93,8 @@ public class ConfigDescription implements Identifiable<URI> {
* @throws IllegalArgumentException if the URI is null or invalid
*/
@Deprecated
public ConfigDescription(URI uri, List<ConfigDescriptionParameter> parameters,
List<ConfigDescriptionParameterGroup> groups) {
public ConfigDescription(@Nullable URI uri, @Nullable List<ConfigDescriptionParameter> parameters,
@Nullable List<ConfigDescriptionParameterGroup> groups) {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null!");
}

View File

@ -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 <T_ID> the key type, e.g. ThingTypeUID, ChannelUID, URI,...
* @param <T_OBJECT> the object type, e.g. ThingType, ChannelType, ConfigDescription,...
*/
public abstract class AbstractXmlBasedProvider<T_ID, T_OBJECT extends Identifiable<T_ID>> {
@NonNullByDefault
public abstract class AbstractXmlBasedProvider<@NonNull T_ID, @NonNull T_OBJECT extends Identifiable<@NonNull T_ID>> {
private final Map<Bundle, List<T_OBJECT>> bundleObjectMap = new ConcurrentHashMap<>();
private final Map<LocalizedKey, T_OBJECT> localizedObjectCache = new ConcurrentHashMap<>();
/**
@ -72,7 +76,7 @@ public abstract class AbstractXmlBasedProvider<T_ID, T_OBJECT extends Identifiab
* @param objectList the objects to be added
*/
public final synchronized void addAll(Bundle bundle, Collection<T_OBJECT> objectList) {
if (objectList == null || objectList.isEmpty()) {
if (objectList.isEmpty()) {
return;
}
List<T_OBJECT> objects = acquireObjects(bundle);
@ -86,10 +90,7 @@ public abstract class AbstractXmlBasedProvider<T_ID, T_OBJECT extends Identifiab
}
}
private List<T_OBJECT> acquireObjects(Bundle bundle) {
if (bundle == null) {
return null;
}
private @Nullable List<T_OBJECT> acquireObjects(Bundle bundle) {
List<T_OBJECT> objects = bundleObjectMap.get(bundle);
if (objects == null) {
objects = new CopyOnWriteArrayList<>();
@ -141,9 +142,6 @@ public abstract class AbstractXmlBasedProvider<T_ID, T_OBJECT extends Identifiab
* @param bundle the module for which all associated Thing types to be removed
*/
public final synchronized void removeAll(Bundle bundle) {
if (bundle == null) {
return;
}
List<T_OBJECT> objects = bundleObjectMap.remove(bundle);
if (objects != null) {
removeCachedEntries(objects);
@ -173,9 +171,12 @@ public abstract class AbstractXmlBasedProvider<T_ID, T_OBJECT extends Identifiab
return cacheEntry;
}
@Nullable
final T_OBJECT localizedObject = localize(bundle, object, locale);
if (localizedObject != null) {
localizedObjectCache.put(localizedKey, localizedObject);
@NonNull
T_OBJECT nonNullLocalizedObject = (@NonNull T_OBJECT) localizedObject;
localizedObjectCache.put(localizedKey, nonNullLocalizedObject);
return localizedObject;
} else {
return object;

View File

@ -16,6 +16,7 @@ import java.net.URI;
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.core.ConfigDescription;
import org.openhab.core.config.core.ConfigDescriptionProvider;
@ -38,6 +39,7 @@ import org.osgi.framework.Bundle;
* @author Markus Rathgeb - Use ConfigI18nLocalizerService
* @author Simon Kaufmann - factored out common aspects into {@link AbstractXmlBasedProvider}
*/
@NonNullByDefault
public abstract class AbstractXmlConfigDescriptionProvider extends AbstractXmlBasedProvider<URI, ConfigDescription>
implements ConfigDescriptionProvider {

View File

@ -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<List<ConfigDescription>> {
/**

View File

@ -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<List<ConfigDescription>> {
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<ConfigDescription> configDescriptions) {
this.configDescriptionProvider.addAll(this.bundle, configDescriptions);
this.configDescriptionProvider.addAll(bundle, configDescriptions);
}
@Override

View File

@ -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 <T> the result type of the conversion
*/
public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
@NonNullByDefault
public class XmlDocumentBundleTracker<@NonNull T> extends BundleTracker<Bundle> {
/**
* Execute given method while take the lock and unlock before return.
@ -108,8 +112,7 @@ public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
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<T> extends BundleTracker<Bundle> {
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<T> extends BundleTracker<Bundle> {
}
private Set<Bundle> 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<Bundle>) Arrays.stream(bundleTracker.getBundles()).collect(Collectors.toSet());
}
@Override
@ -184,7 +172,8 @@ public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
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<T> extends BundleTracker<Bundle> {
queue.clear();
}
private XmlDocumentProvider<T> acquireXmlDocumentProvider(Bundle bundle) {
if (bundle == null) {
return null;
}
private @Nullable XmlDocumentProvider<T> acquireXmlDocumentProvider(Bundle bundle) {
XmlDocumentProvider<T> xmlDocumentProvider = bundleDocumentProviderMap.get(bundle);
if (xmlDocumentProvider == null) {
xmlDocumentProvider = xmlDocumentProviderFactory.createDocumentProvider(bundle);
@ -232,9 +217,6 @@ public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
}
private void releaseXmlDocumentProvider(Bundle bundle) {
if (bundle == null) {
return;
}
XmlDocumentProvider<T> xmlDocumentProvider = bundleDocumentProviderMap.get(bundle);
if (xmlDocumentProvider == null) {
return;
@ -270,13 +252,15 @@ public class XmlDocumentBundleTracker<T> extends BundleTracker<Bundle> {
}
@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<T> extends BundleTracker<Bundle> {
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.

View File

@ -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 <T> the result type of the conversion
*/
@NonNullByDefault
public interface XmlDocumentProvider<T> {
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

View File

@ -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<T> {
public interface XmlDocumentProviderFactory<@NonNull T> {
/**
* Creates an XML document provider for the specified module which is used to process

View File

@ -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 <T> the result type of the conversion
*/
public abstract class XmlDocumentReader<T> {
@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<T> {
* @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<T> {
* @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);
}
}

View File

@ -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<Item>
}
@Override
public @Nullable StateDescriptionFragment getStateDescriptionFragment(@NonNull String itemName,
@Nullable Locale locale) {
public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) {
return stateDescriptionFragments.get(itemName);
}

View File

@ -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<List<?>> {
/**

View File

@ -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<List<?>> {
private final Logger logger = LoggerFactory.getLogger(ThingTypeXmlProvider.class);
@ -70,18 +73,6 @@ public class ThingTypeXmlProvider implements XmlDocumentProvider<List<?>> {
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<List<?>> {
@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);

View File

@ -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<UID, ChannelGroupType>
implements ChannelGroupTypeProvider {

View File

@ -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<UID, ChannelType> implements ChannelTypeProvider {

View File

@ -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<UID, ThingType>
implements ThingTypeProvider, XmlDocumentProviderFactory<List<?>> {
@ -62,9 +64,9 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider<UID, ThingTyp
public static final String READY_MARKER = "esh.xmlThingTypes";
private final ThingTypeI18nLocalizationService thingTypeI18nLocalizationService;
private XmlChannelTypeProvider channelTypeProvider;
private XmlChannelGroupTypeProvider channelGroupTypeProvider;
private AbstractXmlConfigDescriptionProvider configDescriptionProvider;
private final XmlChannelTypeProvider channelTypeProvider;
private final XmlChannelGroupTypeProvider channelGroupTypeProvider;
private final AbstractXmlConfigDescriptionProvider configDescriptionProvider;
private @Nullable XmlDocumentBundleTracker<List<?>> thingTypeTracker;
private final ReadyService readyService;
private final ScheduledExecutorService scheduler = ThreadPoolManager
@ -72,10 +74,17 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider<UID, ThingTyp
private @Nullable Future<?> 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<UID, ThingTyp
return getAll(locale);
}
@Reference(target = "(esh.scope=core.xml.thing)")
public void setConfigDescriptionProvider(ConfigDescriptionProvider configDescriptionProvider) {
this.configDescriptionProvider = (AbstractXmlConfigDescriptionProvider) configDescriptionProvider;
}
public void unsetConfigDescriptionProvider(ConfigDescriptionProvider configDescriptionProvider) {
this.configDescriptionProvider = null;
}
@Reference(target = "(esh.scope=core.xml.channels)")
public void setChannelTypeProvider(ChannelTypeProvider channelTypeProvider) {
this.channelTypeProvider = (XmlChannelTypeProvider) channelTypeProvider;
}
public void unsetChannelTypeProvider(ChannelTypeProvider channelTypeProvider) {
this.channelTypeProvider = null;
}
@Reference(target = "(esh.scope=core.xml.channelGroups)")
public void setChannelGroupTypeProvider(ChannelGroupTypeProvider channelGroupTypeProvider) {
this.channelGroupTypeProvider = (XmlChannelGroupTypeProvider) channelGroupTypeProvider;
}
public void unsetChannelGroupTypeProvider(ChannelGroupTypeProvider channelGroupTypeProvider) {
this.channelGroupTypeProvider = null;
}
@Override
protected @Nullable ThingType localize(Bundle bundle, ThingType thingType, @Nullable Locale locale) {
return thingTypeI18nLocalizationService.createLocalizedThingType(bundle, thingType, locale);

View File

@ -44,7 +44,7 @@ import org.slf4j.LoggerFactory;
* type of the persistable element
*/
@NonNullByDefault
public abstract class AbstractManagedProvider<@NonNull E extends Identifiable<K>, @NonNull K, PE>
public abstract class AbstractManagedProvider<@NonNull E extends Identifiable<K>, @NonNull K, @NonNull PE>
extends AbstractProvider<E> implements ManagedProvider<E, K> {
private volatile Storage<PE> storage;
@ -80,6 +80,7 @@ public abstract class AbstractManagedProvider<@NonNull E extends Identifiable<K>
}
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<K>
@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<K>
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<K>
* @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.

View File

@ -199,7 +199,7 @@ public abstract class AbstractRegistry<@NonNull E extends Identifiable<K>, @NonN
}
@Override
public Collection<@NonNull E> getAll() {
public Collection<E> getAll() {
elementReadLock.lock();
try {
return new HashSet<>(elements);

View File

@ -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<T> {
public interface Identifiable<@NonNull T> {
/**
* Get the unique identifier.

View File

@ -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 <E> type of the elements in the registry
*/
@NonNullByDefault
public interface Registry<E extends Identifiable<K>, K> {
public interface Registry<@NonNull E extends Identifiable<K>, @NonNull K> {
/**
* Adds a {@link RegistryChangeListener} to the registry.

View File

@ -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 {
/**

View File

@ -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<Thing> thingChangeListener) {
unregisterCurrentThingsChangeListener();
this.thingChangeListener = thingChangeListener;
managedThingProvider.addProviderChangeListener(this.thingChangeListener);