Skip to content

Commit

Permalink
Add more null annotations to XML processing classes (openhab#2775)
Browse files Browse the repository at this point in the history
This adds null annotations to many XML processing classes and a few others.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored Mar 10, 2022
1 parent c7aec15 commit e6ddecc
Show file tree
Hide file tree
Showing 44 changed files with 506 additions and 383 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.binding.BindingInfo;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.xml.util.ConverterAttributeMapValidator;
Expand All @@ -38,18 +40,19 @@
* @author Michael Grammling - Initial contribution
* @author Andre Fuechsel - Made author tag optional
*/
@NonNullByDefault
public class BindingInfoConverter extends GenericUnmarshaller<BindingInfoXmlResult> {

private ConverterAttributeMapValidator attributeMapValidator;

public BindingInfoConverter() {
super(BindingInfoXmlResult.class);

this.attributeMapValidator = new ConverterAttributeMapValidator(
attributeMapValidator = new ConverterAttributeMapValidator(
new String[][] { { "id", "true" }, { "schemaLocation", "false" } });
}

private URI readConfigDescriptionURI(NodeIterator nodeIterator) throws ConversionException {
private @Nullable URI readConfigDescriptionURI(NodeIterator nodeIterator) throws ConversionException {
String uriText = nodeIterator.nextAttribute("config-description-ref", "uri", false);

if (uriText != null) {
Expand All @@ -64,7 +67,7 @@ private URI readConfigDescriptionURI(NodeIterator nodeIterator) throws Conversio
return null;
}

private ConfigDescription readConfigDescription(NodeIterator nodeIterator) {
private @Nullable ConfigDescription readConfigDescription(NodeIterator nodeIterator) {
Object nextNode = nodeIterator.next();

if (nextNode != null) {
Expand All @@ -79,17 +82,11 @@ private ConfigDescription readConfigDescription(NodeIterator nodeIterator) {
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
BindingInfoXmlResult bindingInfoXmlResult = null;
BindingInfo bindingInfo = null;

public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
// read attributes
Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
Map<String, String> attributes = attributeMapValidator.readValidatedAttributes(reader);

String id = attributes.get("id");
if (id == null) {
throw new ConversionException("Binding id attribute is null");
}
String id = requireNonEmpty(attributes.get("id"), "Binding id attribute is null or empty");

// set automatically extracted URI for a possible 'config-description' section
context.put("config-description.uri", "binding:" + id);
Expand All @@ -98,7 +95,8 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
List<?> nodes = (List<?>) context.convertAnother(context, List.class);
NodeIterator nodeIterator = new NodeIterator(nodes);

String name = (String) nodeIterator.nextValue("name", true);
String name = requireNonEmpty((String) nodeIterator.nextValue("name", true),
"Binding name attribute is null or empty");
String description = (String) nodeIterator.nextValue("description", false);
String author = (String) nodeIterator.nextValue("author", false);
String serviceId = (String) nodeIterator.nextValue("service-id", false);
Expand All @@ -115,9 +113,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
nodeIterator.assertEndOfType();

// create object
bindingInfo = new BindingInfo(id, name, description, author, serviceId, configDescriptionURI);
bindingInfoXmlResult = new BindingInfoXmlResult(bindingInfo, configDescription);

return bindingInfoXmlResult;
BindingInfo bindingInfo = new BindingInfo(id, name, description, author, serviceId, configDescriptionURI);
return new BindingInfoXmlResult(bindingInfo, configDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public class BindingInfoXmlResult {
private BindingInfo bindingInfo;
private @Nullable ConfigDescription configDescription;

public BindingInfoXmlResult(BindingInfo bindingInfo, @Nullable ConfigDescription configDescription)
throws IllegalArgumentException {
if (bindingInfo == null) {
throw new IllegalArgumentException("The BindingInfo must not be null!");
}

public BindingInfoXmlResult(BindingInfo bindingInfo, @Nullable ConfigDescription configDescription) {
this.bindingInfo = bindingInfo;
this.configDescription = configDescription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import java.util.List;
import java.util.Map;

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.ConfigDescriptionBuilder;
import org.openhab.core.config.core.ConfigDescriptionParameter;
import org.openhab.core.config.core.ConfigDescriptionParameterGroup;
import org.openhab.core.config.xml.util.ConverterAssertion;
import org.openhab.core.config.xml.util.ConverterAttributeMapValidator;
import org.openhab.core.config.xml.util.GenericUnmarshaller;
import org.openhab.core.config.xml.util.NodeIterator;
Expand All @@ -42,6 +43,7 @@
* @author Michael Grammling - Initial contribution
* @author Chris Jackson - Added configuration groups
*/
@NonNullByDefault
public class ConfigDescriptionConverter extends GenericUnmarshaller<ConfigDescription> {

private ConverterAttributeMapValidator attributeMapValidator;
Expand All @@ -53,7 +55,7 @@ public ConfigDescriptionConverter() {
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
// read attributes
Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
String uriText = attributes.get("uri");
Expand Down Expand Up @@ -95,7 +97,9 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
}
}

ConverterAssertion.assertEndOfType(reader);
if (reader.hasMoreChildren()) {
throw new ConversionException("The document is invalid, it contains unsupported data!");
}

return ConfigDescriptionBuilder.create(uri).withParameters(configDescriptionParams)
.withParameterGroups(configDescriptionGroups).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.ConfigDescriptionParameter;
import org.openhab.core.config.core.ConfigDescriptionParameter.Type;
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
Expand Down Expand Up @@ -45,6 +47,7 @@
* parameters.
* @author Thomas Höfer - Added unit
*/
@NonNullByDefault
public class ConfigDescriptionParameterConverter extends GenericUnmarshaller<ConfigDescriptionParameter> {

private ConverterAttributeMapValidator attributeMapValidator;
Expand All @@ -58,15 +61,15 @@ public ConfigDescriptionParameterConverter() {
{ "multiple", "false" }, { "groupName", "false" }, { "unit", "false" } });
}

private Type toType(String xmlType) {
private @Nullable Type toType(@Nullable String xmlType) {
if (xmlType != null) {
return Type.valueOf(xmlType.toUpperCase());
}

return null;
}

private BigDecimal toNumber(String value) {
private @Nullable BigDecimal toNumber(@Nullable String value) {
try {
if (value != null) {
return new BigDecimal(value);
Expand All @@ -77,19 +80,19 @@ private BigDecimal toNumber(String value) {
return null;
}

private Boolean toBoolean(String val) {
private @Nullable Boolean toBoolean(@Nullable String val) {
if (val == null) {
return null;
}
return Boolean.valueOf(val);
}

private Boolean falseIfNull(Boolean b) {
return (b != null) ? b : false;
private Boolean falseIfNull(@Nullable Boolean b) {
return b != null ? b : false;
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
ConfigDescriptionParameter configDescriptionParam = null;

// read attributes
Expand Down Expand Up @@ -142,7 +145,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
return configDescriptionParam;
}

private List<ParameterOption> readParameterOptions(Object rawNodeValueList) {
private @Nullable List<ParameterOption> readParameterOptions(@Nullable Object rawNodeValueList) {
if (rawNodeValueList instanceof List<?>) {
List<?> list = (List<?>) rawNodeValueList;
List<ParameterOption> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.config.xml;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.ConfigDescriptionParameterGroup;
import org.openhab.core.config.core.ConfigDescriptionParameterGroupBuilder;
import org.openhab.core.config.xml.util.ConverterValueMap;
Expand All @@ -27,14 +29,15 @@
*
* @author Chris Jackson - Initial contribution
*/
@NonNullByDefault
public class ConfigDescriptionParameterGroupConverter extends GenericUnmarshaller<ConfigDescriptionParameterGroup> {

public ConfigDescriptionParameterGroupConverter() {
super(ConfigDescriptionParameterGroup.class);
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext marshallingContext) {
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext marshallingContext) {
String name = reader.getAttribute("name");

// Read values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.config.xml;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.FilterCriteria;
import org.openhab.core.config.xml.util.GenericUnmarshaller;

Expand All @@ -24,14 +26,15 @@
*
* @author Alex Tugarev - Initial contribution
*/
@NonNullByDefault
public class FilterCriteriaConverter extends GenericUnmarshaller<FilterCriteria> {

public FilterCriteriaConverter() {
super(FilterCriteria.class);
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
public @Nullable Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String name = reader.getAttribute("name");
String criteria = reader.getValue();
return new FilterCriteria(name, criteria);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

Expand All @@ -24,9 +27,10 @@
*
* @author Michael Grammling - Initial contribution
*/
@NonNullByDefault
public class ConverterAttributeMapValidator {

private Map<String, Boolean> validationMaskTemplate;
private @Nullable Map<String, Boolean> validationMaskTemplate;

/**
* Creates a new instance of this class with the specified parameter.
Expand All @@ -45,13 +49,14 @@ public class ConverterAttributeMapValidator {
*
* @param validationMaskTemplate the two-dimensional key-required list (could be null or empty)
*/
public ConverterAttributeMapValidator(String[][] validationMaskTemplate) {
public ConverterAttributeMapValidator(String @Nullable [][] validationMaskTemplate) {
if (validationMaskTemplate != null) {
this.validationMaskTemplate = new HashMap<>(validationMaskTemplate.length);
Map<String, Boolean> template = new HashMap<>(validationMaskTemplate.length);

for (String[] validationProperty : validationMaskTemplate) {
this.validationMaskTemplate.put(validationProperty[0], Boolean.parseBoolean(validationProperty[1]));
template.put(validationProperty[0], Boolean.parseBoolean(validationProperty[1]));
}
this.validationMaskTemplate = template;
}
}

Expand All @@ -66,7 +71,7 @@ public ConverterAttributeMapValidator(String[][] validationMaskTemplate) {
*
* @param validationMaskTemplate the key-required map (could be null or empty)
*/
public ConverterAttributeMapValidator(Map<String, Boolean> validationMaskTemplate) {
public ConverterAttributeMapValidator(@Nullable Map<String, Boolean> validationMaskTemplate) {
this.validationMaskTemplate = validationMaskTemplate;
}

Expand All @@ -79,7 +84,7 @@ public ConverterAttributeMapValidator(Map<String, Boolean> validationMaskTemplat
* @throws ConversionException if the validation check fails
*/
public Map<String, String> readValidatedAttributes(HierarchicalStreamReader reader) throws ConversionException {
return readValidatedAttributes(reader, this.validationMaskTemplate);
return readValidatedAttributes(reader, validationMaskTemplate);
}

/**
Expand All @@ -95,7 +100,7 @@ public Map<String, String> readValidatedAttributes(HierarchicalStreamReader read
* @throws ConversionException if the validation check fails
*/
public static Map<String, String> readValidatedAttributes(HierarchicalStreamReader reader,
Map<String, Boolean> validationMaskTemplate) throws ConversionException {
@Nullable Map<String, Boolean> validationMaskTemplate) throws ConversionException {
Map<String, String> attributeMap = new HashMap<>(reader.getAttributeCount());

Map<String, Boolean> validationMask = null;
Expand Down
Loading

0 comments on commit e6ddecc

Please sign in to comment.