Skip to content

Commit 4d6a11f

Browse files
authored
Add more null annotatons (openhab#260)
Adds null annotations to: * Default UI * Basic UI * HomeBuilder * REST Docs * Tiles Signed-off-by: Wouter Born <[email protected]>
1 parent 764925b commit 4d6a11f

File tree

33 files changed

+328
-647
lines changed

33 files changed

+328
-647
lines changed

bundles/org.openhab.ui.basic/bnd.bnd

-2
This file was deleted.

bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/BasicUITile.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
*/
1313
package org.openhab.ui.basic.internal;
1414

15+
import org.eclipse.jdt.annotation.NonNullByDefault;
16+
import org.eclipse.jdt.annotation.Nullable;
1517
import org.openhab.core.ui.tiles.Tile;
1618
import org.osgi.service.component.annotations.Component;
1719

1820
/**
1921
* The tile for the Basic UI
2022
*
21-
* @author Kai Kreuzer
22-
* @author Yannick Schaus - remove dependency to dashboard
23-
*
23+
* @author Kai Kreuzer - Initial contribution
24+
* @author Yannick Schaus - Remove dependency to dashboard
2425
*/
2526
@Component
27+
@NonNullByDefault
2628
public class BasicUITile implements Tile {
2729

2830
@Override
@@ -36,7 +38,7 @@ public String getUrl() {
3638
}
3739

3840
@Override
39-
public String getOverlay() {
41+
public @Nullable String getOverlay() {
4042
return null;
4143
}
4244

bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/WebAppActivator.java

-46
This file was deleted.

bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/WebAppConfig.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class WebAppConfig {
3636
private String defaultSitemap = DEFAULT_SITEMAP;
3737
private String theme = DEFAULT_THEME;
3838

39-
private List<String> cssClassList = new ArrayList<String>();
39+
private List<String> cssClassList = new ArrayList<>();
4040

4141
private static final Map<String, String> CSS_CLASSES;
4242
private static final Map<String, Boolean> CSS_DEFAULT_VALUES;
@@ -46,12 +46,12 @@ public class WebAppConfig {
4646
private static final String CONFIG_CAPITALIZE = "capitalizeValues";
4747

4848
static {
49-
CSS_CLASSES = new HashMap<String, String>();
49+
CSS_CLASSES = new HashMap<>();
5050
CSS_CLASSES.put(CONFIG_ENABLE_ICONS, "ui-icons-enabled");
5151
CSS_CLASSES.put(CONFIG_CONDENSED_LAYOUT, "ui-layout-condensed");
5252
CSS_CLASSES.put(CONFIG_CAPITALIZE, "ui-capitalize-values");
5353

54-
CSS_DEFAULT_VALUES = new HashMap<String, Boolean>();
54+
CSS_DEFAULT_VALUES = new HashMap<>();
5555
CSS_DEFAULT_VALUES.put(CONFIG_ENABLE_ICONS, true);
5656
CSS_DEFAULT_VALUES.put(CONFIG_CONDENSED_LAYOUT, false);
5757
CSS_DEFAULT_VALUES.put(CONFIG_CAPITALIZE, false);

bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/AbstractWidgetRenderer.java

+44-57
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@
2424
import org.apache.commons.io.IOUtils;
2525
import org.apache.commons.lang.StringEscapeUtils;
2626
import org.apache.commons.lang.StringUtils;
27+
import org.eclipse.jdt.annotation.NonNullByDefault;
28+
import org.eclipse.jdt.annotation.Nullable;
2729
import org.openhab.core.i18n.I18nUtil;
2830
import org.openhab.core.i18n.LocaleProvider;
2931
import org.openhab.core.i18n.TranslationProvider;
3032
import org.openhab.core.library.types.QuantityType;
3133
import org.openhab.core.model.sitemap.sitemap.Widget;
3234
import org.openhab.core.types.State;
3335
import org.openhab.core.ui.items.ItemUIRegistry;
34-
import org.openhab.ui.basic.internal.WebAppActivator;
3536
import org.openhab.ui.basic.internal.WebAppConfig;
3637
import org.openhab.ui.basic.render.RenderException;
3738
import org.openhab.ui.basic.render.WidgetRenderer;
3839
import org.osgi.framework.BundleContext;
40+
import org.osgi.service.component.annotations.Activate;
41+
import org.osgi.service.component.annotations.Reference;
3942
import org.slf4j.Logger;
4043
import org.slf4j.LoggerFactory;
4144

@@ -46,21 +49,29 @@
4649
*
4750
* @author Kai Kreuzer - Initial contribution and API
4851
* @author Vlad Ivanov - BasicUI changes
49-
*
5052
*/
53+
@NonNullByDefault
5154
public abstract class AbstractWidgetRenderer implements WidgetRenderer {
5255

5356
private final Logger logger = LoggerFactory.getLogger(AbstractWidgetRenderer.class);
5457

5558
public static final String ICON_TYPE = "svg";
5659

57-
protected ItemUIRegistry itemUIRegistry;
58-
protected TranslationProvider i18nProvider;
59-
protected LocaleProvider localeProvider;
60+
private final BundleContext bundleContext;
61+
protected final TranslationProvider i18nProvider;
62+
protected final ItemUIRegistry itemUIRegistry;
63+
protected final LocaleProvider localeProvider;
6064

61-
protected WebAppConfig config;
65+
protected WebAppConfig config = new WebAppConfig();
6266

63-
private BundleContext bundleContext;
67+
@Activate
68+
public AbstractWidgetRenderer(final BundleContext bundleContext, final @Reference TranslationProvider i18nProvider,
69+
final @Reference ItemUIRegistry itemUIRegistry, final @Reference LocaleProvider localeProvider) {
70+
this.bundleContext = bundleContext;
71+
this.i18nProvider = i18nProvider;
72+
this.itemUIRegistry = itemUIRegistry;
73+
this.localeProvider = localeProvider;
74+
}
6475

6576
/* the file extension of the snippets */
6677
protected static final String SNIPPET_EXT = ".html";
@@ -69,44 +80,12 @@ public abstract class AbstractWidgetRenderer implements WidgetRenderer {
6980
protected static final String SNIPPET_LOCATION = "snippets/";
7081

7182
/* a local cache so we do not have to read the snippets over and over again from the bundle */
72-
protected static final Map<String, String> SNIPPET_CACHE = new HashMap<String, String>();
73-
74-
protected void setItemUIRegistry(ItemUIRegistry itemUIRegistry) {
75-
this.itemUIRegistry = itemUIRegistry;
76-
}
77-
78-
protected void unsetItemUIRegistry(ItemUIRegistry itemUIRegistry) {
79-
this.itemUIRegistry = null;
80-
}
83+
protected static final Map<String, String> SNIPPET_CACHE = new HashMap<>(30);
8184

8285
public ItemUIRegistry getItemUIRegistry() {
8386
return itemUIRegistry;
8487
}
8588

86-
protected void setLocaleProvider(LocaleProvider localeProvider) {
87-
this.localeProvider = localeProvider;
88-
}
89-
90-
protected void unsetLocaleProvider(final LocaleProvider localeProvider) {
91-
this.localeProvider = null;
92-
}
93-
94-
protected void setTranslationProvider(TranslationProvider i18nProvider) {
95-
this.i18nProvider = i18nProvider;
96-
}
97-
98-
protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
99-
this.i18nProvider = null;
100-
}
101-
102-
protected void activate(BundleContext context) {
103-
this.bundleContext = context;
104-
}
105-
106-
protected void deactivate(BundleContext context) {
107-
this.bundleContext = null;
108-
}
109-
11089
/**
11190
* Replace some common values in the widget template
11291
*
@@ -123,12 +102,12 @@ protected String preprocessSnippet(String originalSnippet, Widget w) {
123102
String text = itemUIRegistry.getLabel(w);
124103
snippet = StringUtils.replace(snippet, "%label%", getLabel(text));
125104
snippet = StringUtils.replace(snippet, "%value%", getValue(text));
126-
snippet = StringUtils.replace(snippet, "%has_value%", new Boolean(hasValue(text)).toString());
105+
snippet = StringUtils.replace(snippet, "%has_value%", Boolean.valueOf(hasValue(text)).toString());
127106
snippet = StringUtils.replace(snippet, "%visibility_class%",
128107
itemUIRegistry.getVisiblity(w) ? "" : "mdl-form__row--hidden");
129108

130109
String state = getState(w);
131-
snippet = StringUtils.replace(snippet, "%state%", state == null ? "" : escapeURL(state));
110+
snippet = StringUtils.replace(snippet, "%state%", escapeURL(state));
132111

133112
String category = getCategory(w);
134113
snippet = StringUtils.replace(snippet, "%category%", escapeURL(category));
@@ -148,7 +127,7 @@ protected synchronized String getSnippet(String elementType) throws RenderExcept
148127
String snippet = SNIPPET_CACHE.get(lowerTypeElementType);
149128
if (snippet == null) {
150129
String snippetLocation = SNIPPET_LOCATION + lowerTypeElementType + SNIPPET_EXT;
151-
URL entry = WebAppActivator.getContext().getBundle().getEntry(snippetLocation);
130+
URL entry = bundleContext.getBundle().getEntry(snippetLocation);
152131
if (entry != null) {
153132
try {
154133
snippet = IOUtils.toString(entry.openStream());
@@ -179,7 +158,11 @@ public String getLabel(Widget w) {
179158
* @param text the text containing the label and an optional value around []
180159
* @return the label extracted from the text
181160
*/
182-
protected String getLabel(String text) {
161+
protected String getLabel(@Nullable String text) {
162+
if (text == null) {
163+
return "";
164+
}
165+
183166
int index = text.indexOf('[');
184167

185168
if (index != -1) {
@@ -205,7 +188,11 @@ public String getValue(Widget w) {
205188
* @param text the text containing the label and an optional value around []
206189
* @return the value extracted from the text or "" if not present
207190
*/
208-
protected String getValue(String text) {
191+
protected String getValue(@Nullable String text) {
192+
if (text == null) {
193+
return "";
194+
}
195+
209196
int index = text.indexOf('[');
210197

211198
if (index != -1) {
@@ -231,8 +218,8 @@ public boolean hasValue(Widget w) {
231218
* @param text the text containing the label and an optional value around []
232219
* @return true if the text contains a value
233220
*/
234-
protected boolean hasValue(String text) {
235-
return (text.indexOf('[') != -1);
221+
protected boolean hasValue(@Nullable String text) {
222+
return text != null && text.indexOf('[') != -1;
236223
}
237224

238225
/**
@@ -242,9 +229,9 @@ protected boolean hasValue(String text) {
242229
* @param string The string that has to be escaped
243230
* @return The escaped string
244231
*/
245-
protected String escapeURL(String string) {
232+
protected String escapeURL(@Nullable String string) {
246233
if (string == null) {
247-
return null;
234+
return "";
248235
}
249236

250237
try {
@@ -287,7 +274,7 @@ protected String processColor(Widget w, String originalSnippet) {
287274
return snippet;
288275
}
289276

290-
protected String getCategory(Widget w) {
277+
protected @Nullable String getCategory(Widget w) {
291278
return itemUIRegistry.getCategory(w);
292279
}
293280

@@ -300,7 +287,7 @@ protected String getState(Widget w) {
300287
}
301288
}
302289

303-
protected String escapeHtml(String s) {
290+
protected String escapeHtml(@Nullable String s) {
304291
return StringEscapeUtils.escapeHtml(s);
305292
}
306293

@@ -309,24 +296,24 @@ public void setConfig(WebAppConfig config) {
309296
this.config = config;
310297
}
311298

312-
protected String localizeText(String key) {
299+
protected @Nullable String localizeText(String key) {
313300
String result = "";
314301
if (I18nUtil.isConstant(key)) {
315-
result = this.i18nProvider.getText(this.bundleContext.getBundle(), I18nUtil.stripConstant(key), "",
316-
this.localeProvider.getLocale());
302+
result = i18nProvider.getText(bundleContext.getBundle(), I18nUtil.stripConstant(key), "",
303+
localeProvider.getLocale());
317304
}
318305
return result;
319306
}
320307

321-
protected String getUnitForWidget(Widget widget) {
308+
protected @Nullable String getUnitForWidget(Widget widget) {
322309
return itemUIRegistry.getUnitForWidget(widget);
323310
}
324311

325-
protected State convertStateToLabelUnit(QuantityType<?> state, String label) {
312+
protected @Nullable State convertStateToLabelUnit(QuantityType<?> state, String label) {
326313
return itemUIRegistry.convertStateToLabelUnit(state, label);
327314
}
328315

329-
protected boolean isValidURL(String url) {
316+
protected boolean isValidURL(@Nullable String url) {
330317
if (url != null && !url.isEmpty()) {
331318
try {
332319
return new URL(url).toURI() != null ? true : false;

0 commit comments

Comments
 (0)