Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some non null annotations #4486

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -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!");
}
Expand All @@ -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<URL> resourceFiles = this.bundle.findEntries(this.path, this.filePattern, true);

List<URL> allResources = new LinkedList<>();
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +33,7 @@
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@NonNullByDefault
public class WrappedScheduledExecutorService extends ScheduledThreadPoolExecutor {

final Logger logger = LoggerFactory.getLogger(WrappedScheduledExecutorService.class);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand All @@ -50,7 +53,7 @@ public class LanguageResourceBundleManager {
private ClassLoader resourceClassLoader;
private List<String> 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!");
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -135,7 +138,7 @@ private List<String> 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();

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,9 +25,11 @@
* @author Henning Treu - Initial contribution
*/
@Component(service = BundleResolver.class)
@NonNullByDefault
public class BundleResolverImpl implements BundleResolver {

@Override
holgerfriedrich marked this conversation as resolved.
Show resolved Hide resolved
@Nullable
public Bundle resolveBundle(Class<?> clazz) {
return FrameworkUtil.getBundle(clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.core.items;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.registry.Identifiable;

/**
* A listener to be informed before entities are added respectively after they are removed.
*
* @author Simon Kaufmann - Initial contribution
*/
@NonNullByDefault
public interface RegistryHook<E extends Identifiable<?>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wborn @kaikreuzer Do you know about the use case for this Hook? This is only used in registries, which just forward the calls. addRegistryHook - which seems to be the only method for adding hooks - seems never being called. I could not trace it down to a single use in our core repo, no use in webui, and only 2 uses in add-ons test. However, it is a public interface to register for add/remove of elements in the registries.

It was introduced in ESH times and seems no longer be in use, at least not how it was before.

Shall I create a PR to deprecate this functions in the registries and this class?


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package org.openhab.core.util;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.Bundle;

/**
* Resolve bundle specific information from the framework.
*
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public interface BundleResolver {

/**
holgerfriedrich marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -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);
}
Loading