diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/Activator.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/Activator.java index 1522f85b5a0..5d60030c46c 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/Activator.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/Activator.java @@ -12,6 +12,8 @@ */ package org.openhab.core; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.osgi.annotation.bundle.Header; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -25,17 +27,18 @@ * @author Jan N. Klug - Initial contribution */ @Header(name = Constants.BUNDLE_ACTIVATOR, value = "${@class}") +@NonNullByDefault public final class Activator implements BundleActivator { private final Logger logger = LoggerFactory.getLogger(Activator.class); @Override - public void start(BundleContext bc) throws Exception { + public void start(@Nullable BundleContext bc) throws Exception { logger.info("Starting openHAB {} ({})", OpenHAB.getVersion(), OpenHAB.buildString()); } @Override - public void stop(BundleContext context) throws Exception { + public void stop(@Nullable BundleContext context) throws Exception { // do nothing } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/osgi/ResourceBundleClassLoader.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/osgi/ResourceBundleClassLoader.java index 2053417a3db..066ced1e0cb 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/common/osgi/ResourceBundleClassLoader.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/common/osgi/ResourceBundleClassLoader.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Properties; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.osgi.framework.Bundle; /** @@ -38,6 +40,7 @@ * @author Martin Herbst - UTF-8 replaced by ISO-8859-1 to follow Java standards * */ +@NonNullByDefault public class ResourceBundleClassLoader extends ClassLoader { private Bundle bundle; @@ -56,7 +59,8 @@ public class ResourceBundleClassLoader extends ClassLoader { * considered. * @throws IllegalArgumentException if the bundle is null */ - public ResourceBundleClassLoader(Bundle bundle, String path, String filePattern) throws IllegalArgumentException { + public ResourceBundleClassLoader(@Nullable Bundle bundle, @Nullable String path, @Nullable String filePattern) + throws IllegalArgumentException { if (bundle == null) { throw new IllegalArgumentException("The bundle must not be null!"); } @@ -67,7 +71,7 @@ public ResourceBundleClassLoader(Bundle bundle, String path, String filePattern) } @Override - public URL getResource(String name) { + public @Nullable URL getResource(String name) { Enumeration resourceFiles = this.bundle.findEntries(this.path, this.filePattern, true); List allResources = new LinkedList<>(); @@ -106,7 +110,7 @@ public URL getResource(String name) { } @Override - public InputStream getResourceAsStream(String name) { + public @Nullable InputStream getResourceAsStream(String name) { URL resourceURL = getResource(name); if (resourceURL != null) { try (InputStream resourceStream = resourceURL.openStream()) { diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/WrappedScheduledExecutorService.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/WrappedScheduledExecutorService.java index 56e065a0703..24578a86361 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/WrappedScheduledExecutorService.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/common/WrappedScheduledExecutorService.java @@ -18,6 +18,8 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,6 +33,7 @@ * * @author Hilbrand Bouwkamp - Initial contribution */ +@NonNullByDefault public class WrappedScheduledExecutorService extends ScheduledThreadPoolExecutor { final Logger logger = LoggerFactory.getLogger(WrappedScheduledExecutorService.class); @@ -40,7 +43,7 @@ public WrappedScheduledExecutorService(int corePoolSize, ThreadFactory threadFac } @Override - protected void afterExecute(Runnable r, Throwable t) { + protected void afterExecute(@Nullable Runnable r, @Nullable Throwable t) { super.afterExecute(r, t); Throwable actualThrowable = t; if (actualThrowable == null && r instanceof Future f) { diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/LanguageResourceBundleManager.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/LanguageResourceBundleManager.java index fa8995213db..b696b312621 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/LanguageResourceBundleManager.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/i18n/LanguageResourceBundleManager.java @@ -21,6 +21,8 @@ import java.util.ResourceBundle; import java.util.ResourceBundle.Control; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.osgi.ResourceBundleClassLoader; import org.openhab.core.i18n.LocaleProvider; import org.osgi.framework.Bundle; @@ -37,6 +39,7 @@ * @author Michael Grammling - Initial contribution * @author Markus Rathgeb - Add locale provider support */ +@NonNullByDefault public class LanguageResourceBundleManager { /** The directory within the bundle where the resource files are searched. */ @@ -50,7 +53,7 @@ public class LanguageResourceBundleManager { private ClassLoader resourceClassLoader; private List resourceNames; - public LanguageResourceBundleManager(LocaleProvider localeProvider, Bundle bundle) { + public LanguageResourceBundleManager(LocaleProvider localeProvider, @Nullable Bundle bundle) { if (bundle == null) { throw new IllegalArgumentException("The Bundle must not be null!"); } @@ -80,7 +83,7 @@ public void clearCache() { * @param resource the resource to check (could be null or empty) * @return true if the specified resource is managed by this instance, otherwise false */ - public boolean containsResource(String resource) { + public boolean containsResource(@Nullable String resource) { if (resource != null) { return this.resourceNames.contains(resource); } @@ -135,7 +138,7 @@ private List determineResourceNames() { * * @return the translated text, or null if the key could not be translated */ - public String getText(String resource, String key, Locale locale) { + public @Nullable String getText(@Nullable String resource, @Nullable String key, @Nullable Locale locale) { if ((key != null) && (!key.isEmpty())) { Locale effectiveLocale = locale != null ? locale : localeProvider.getLocale(); @@ -167,27 +170,29 @@ public String getText(String resource, String key, Locale locale) { * * @return the translated text, or null if the key could not be translated */ - public String getText(String key, Locale locale) { + public @Nullable String getText(@Nullable String key, @Nullable Locale locale) { return getText(null, key, locale); } - private String getTranslatedText(String resourceName, String key, Locale locale) { - try { - // Modify the search order so that the following applies: - // 1.) baseName + "_" + language + "_" + country - // 2.) baseName + "_" + language - // 3.) baseName - // 4.) null -> leads to a default text - // Not using the default fallback strategy helps that not the default locale - // search order is applied between 2.) and 3.). - ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName, locale, this.resourceClassLoader, - Control.getNoFallbackControl(Control.FORMAT_PROPERTIES)); - - return resourceBundle.getString(key); - } catch (Exception ex) { - // nothing to do + private @Nullable String getTranslatedText(@Nullable String resourceName, @Nullable String key, + @Nullable Locale locale) { + if (resourceName != null && locale != null && key != null && !key.isEmpty()) { + try { + // Modify the search order so that the following applies: + // 1.) baseName + "_" + language + "_" + country + // 2.) baseName + "_" + language + // 3.) baseName + // 4.) null -> leads to a default text + // Not using the default fallback strategy helps that not the default locale + // search order is applied between 2.) and 3.). + ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName, locale, this.resourceClassLoader, + Control.getNoFallbackControl(Control.FORMAT_PROPERTIES)); + + return resourceBundle.getString(key); + } catch (NullPointerException ex) { + // nothing to do + } } - return null; } } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/service/BundleResolverImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/service/BundleResolverImpl.java index 0a3d4277d7c..2808ee05907 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/service/BundleResolverImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/service/BundleResolverImpl.java @@ -12,6 +12,8 @@ */ package org.openhab.core.internal.service; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.util.BundleResolver; import org.osgi.framework.Bundle; import org.osgi.framework.FrameworkUtil; @@ -23,9 +25,11 @@ * @author Henning Treu - Initial contribution */ @Component(service = BundleResolver.class) +@NonNullByDefault public class BundleResolverImpl implements BundleResolver { @Override + @Nullable public Bundle resolveBundle(Class clazz) { return FrameworkUtil.getBundle(clazz); } diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/RegistryHook.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/RegistryHook.java index 2ca809a0691..84311fbf63d 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/items/RegistryHook.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/items/RegistryHook.java @@ -12,6 +12,7 @@ */ package org.openhab.core.items; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.common.registry.Identifiable; /** @@ -19,6 +20,7 @@ * * @author Simon Kaufmann - Initial contribution */ +@NonNullByDefault public interface RegistryHook> { /** diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/util/BundleResolver.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/util/BundleResolver.java index 6586c1b10fb..ec882d5413d 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/util/BundleResolver.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/util/BundleResolver.java @@ -12,6 +12,8 @@ */ package org.openhab.core.util; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.osgi.framework.Bundle; /** @@ -19,6 +21,7 @@ * * @author Henning Treu - Initial contribution */ +@NonNullByDefault public interface BundleResolver { /** @@ -27,5 +30,6 @@ public interface BundleResolver { * @param clazz the {@link Class} to resolve the bundle for. * @return the bundle associated with the given {@link Class}. */ + @Nullable Bundle resolveBundle(Class clazz); }