Skip to content

Commit

Permalink
Adjusted nullness annotations to conform to expected return values.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Oct 30, 2024
1 parent ac4cd0f commit cc99272
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class SimpleModuleLoaderStrategy
@NonNull
private static final IModuleBindingGenerator COMPILATION_DISABLED_GENERATOR = module -> {
throw new UnsupportedOperationException(
"Dynamic compilation of Metaschema modules is not enabled by default. Configure a different IModuleBindingGenerator with the IModuleLoaderStrategy used with the IBindignContext.");
"Dynamic compilation of Metaschema modules is not enabled by default." +
" Configure a different IModuleBindingGenerator with the IModuleLoaderStrategy" +
" used with the IBindignContext.");
};

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ protected ObjectMapper getObjectMapper() {
@SuppressWarnings("unchecked")
@NonNull
public <T> T readObject(@NonNull IBoundDefinitionModelComplex definition) throws IOException {
return (T) definition.readItem(null, this);
T value = (T) definition.readItem(null, this);
if (value == null) {
throw new IOException(String.format("Failed to read object '%s'%s.",
definition.getDefinitionQName(),
JsonUtil.generateLocationMessage(getReader())));
}
return value;
}

@SuppressWarnings({ "unchecked" })
Expand Down Expand Up @@ -225,7 +231,7 @@ public <T> T readObjectRoot(
// Instance readers
// ================

@NonNull
@Nullable
private Object readInstance(
@NonNull IBoundProperty<?> instance,
@NonNull IBoundObject parent) throws IOException {
Expand Down Expand Up @@ -319,14 +325,14 @@ public Object readItemFieldValue(IBoundObject parentItem, IBoundFieldValue field
return checkMissingFieldValue(readScalarItem(fieldValue));
}

@NonNull
@Nullable
private Object checkMissingFieldValue(Object value) throws IOException {
if (value == null && LOGGER.isWarnEnabled()) {
LOGGER.atWarn().log("Missing property value{}",
JsonUtil.generateLocationMessage(getReader()));
}
// TODO: change nullness annotations to be @Nullable
return ObjectUtils.notNull(value);
return value;
}

@Override
Expand Down Expand Up @@ -391,7 +397,9 @@ private IBoundObject readFieldObject(
(def, parent, problem) -> {
IBoundFieldValue fieldValue = definition.getFieldValue();
Object item = readItemFieldValue(parent, fieldValue);
fieldValue.setValue(parent, item);
if (item != null) {
fieldValue.setValue(parent, item);
}
},
actualProblemHandler);

Expand Down Expand Up @@ -690,10 +698,10 @@ public boolean handleUnknownProperty(
JsonUtil.assertAndAdvance(parser, JsonToken.FIELD_NAME);

IBoundFieldValue fieldValue = ((IBoundDefinitionModelFieldComplex) definition).getFieldValue();
Object value = readItemFieldValue(
ObjectUtils.notNull(parentItem),
fieldValue);
fieldValue.setValue(ObjectUtils.notNull(parentItem), value);
Object value = readItemFieldValue(ObjectUtils.notNull(parentItem), fieldValue);
if (value != null) {
fieldValue.setValue(ObjectUtils.notNull(parentItem), value);
}

retval = foundJsonValueKey = true;
}
Expand Down Expand Up @@ -758,7 +766,12 @@ public Map<String, ITEM> readMap() throws IOException {
// a map item will always start with a FIELD_NAME, since this represents the key
JsonUtil.assertCurrent(parser, JsonToken.FIELD_NAME);

// get the object, since it must have a JSON key
ITEM item = readItem();
if (item == null) {
throw new IOException(String.format("Null object encountered'%s.",
JsonUtil.generateLocationMessage(parser)));
}

// lookup the key
IBoundInstanceFlag jsonKey = instance.getItemJsonKey(item);
Expand All @@ -785,7 +798,6 @@ public Map<String, ITEM> readMap() throws IOException {
}

@Override
@NonNull
public ITEM readItem() throws IOException {
IBoundInstanceModel<ITEM> instance = getCollectionInfo().getInstance();
return instance.readItem(getParentObject(), MetaschemaJsonReader.this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ public <CLASS> CLASS read(@NonNull IBoundDefinitionModelComplex definition) thro
}

ItemReadHandler handler = new ItemReadHandler(ObjectUtils.notNull(event.asStartElement()));
return ObjectUtils.asType(definition.readItem(null, handler));
Object value = definition.readItem(null, handler);
if (value == null) {
throw new IOException(String.format("Unable to read data%s",
XmlEventUtil.generateLocationMessage(reader.peek())));
}

return ObjectUtils.asType(value);
} catch (XMLStreamException ex) {
throw new IOException(ex);
}
Expand Down Expand Up @@ -476,7 +482,9 @@ private void handleFieldDefinitionBody(

// parse the value
Object value = fieldValue.readItem(item, this);
fieldValue.setValue(item, value);
if (value != null) {
fieldValue.setValue(item, value);
}
}

@Override
Expand All @@ -494,7 +502,7 @@ public Object readItemField(
XmlEventUtil.requireStartElement(getReader(), wrapper);
}

Object retval = checkMissingFieldValue(readScalarItem(instance));
Object retval = readScalarItem(instance);

if (wrapper != null) {
XmlEventUtil.skipWhitespace(getReader());
Expand Down Expand Up @@ -550,15 +558,13 @@ public Object readItemFieldValue(
return checkMissingFieldValue(readScalarItem(fieldValue));
}

@SuppressWarnings("null")
@NonNull
@Nullable
private Object checkMissingFieldValue(Object value) throws IOException {
if (value == null && LOGGER.isWarnEnabled()) {
StartElement start = getStartElement();
LOGGER.atWarn().log("Missing property value{}",
XmlEventUtil.generateLocationMessage(start));
}
// TODO: change nullness annotations to be @Nullable
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Object readItemFlag(
* @throws IOException
* if an error occurred while parsing
*/
@NonNull
@Nullable
Object readItemField(
@NonNull IBoundObject parent,
@NonNull IBoundInstanceModelFieldScalar instance) throws IOException;
Expand Down Expand Up @@ -115,7 +115,7 @@ IBoundObject readItemField(
* @throws IOException
* if an error occurred while parsing
*/
@NonNull
@Nullable
Object readItemFieldValue(
@NonNull IBoundObject parent,
@NonNull IBoundFieldValue fieldValue) throws IOException;
Expand Down Expand Up @@ -180,7 +180,7 @@ IBoundObject readItemAssembly(
* @throws IOException
* if an error occurred while parsing
*/
@NonNull
@Nullable
IBoundObject readChoiceGroupItem(
@NonNull IBoundObject parent,
@NonNull IBoundInstanceModelChoiceGroup instance) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IItemValueHandler<TYPE> {
* @throws IOException
* if an error occurred while parsing
*/
@NonNull
@Nullable
TYPE readItem(
@Nullable IBoundObject parent,
@NonNull IItemReadHandler handler) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ default ITEM readSingleton() throws IOException {
* @throws IOException
* if an error occurred while parsing the input
*/
@Nullable
ITEM readItem() throws IOException;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;

import edu.umd.cs.findbugs.annotations.NonNull;

class Issue206MetaschemaReaderTest {
@RegisterExtension
JUnit5Mockery context = new JUnit5Mockery();
Expand All @@ -60,6 +62,7 @@ void testIssue205Json() throws IOException {

try (InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
try (JsonParser parser = JsonFactoryFactory.instance().createParser(is)) {
assert parser != null;
MetaschemaJsonReader reader = new MetaschemaJsonReader(parser);

// assertThrows(IOException.class, () -> {
Expand All @@ -83,7 +86,7 @@ void testIssue205XmlNoValue() throws IOException, XMLStreamException {

XMLInputFactory factory = XMLInputFactory.newInstance();
try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
XMLEventReader2 eventReader = (XMLEventReader2) factory.createXMLEventReader(is);
XMLEventReader2 eventReader = ObjectUtils.notNull((XMLEventReader2) factory.createXMLEventReader(is));
MetaschemaXmlReader reader = new MetaschemaXmlReader(eventReader);

// assertThrows(IOException.class, () -> {
Expand All @@ -106,7 +109,7 @@ void testIssue205XmlEmptyValue() throws IOException, XMLStreamException {

XMLInputFactory factory = XMLInputFactory.newInstance();
try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
XMLEventReader2 eventReader = (XMLEventReader2) factory.createXMLEventReader(is);
XMLEventReader2 eventReader = ObjectUtils.requireNonNull((XMLEventReader2) factory.createXMLEventReader(is));
MetaschemaXmlReader reader = new MetaschemaXmlReader(eventReader);

TestField field = (TestField) reader.read(definition);
Expand All @@ -117,8 +120,12 @@ void testIssue205XmlEmptyValue() throws IOException, XMLStreamException {
@MetaschemaModule(fields = { TestField.class })
public static class TestModule
extends AbstractBoundModule {
@NonNull
private static final URI NAMESPACE = ObjectUtils.notNull(URI.create("http://example.com/"));

public TestModule(List<? extends IBoundModule> importedModules, IBindingContext bindingContext) {
public TestModule(
@NonNull List<? extends IBoundModule> importedModules,
@NonNull IBindingContext bindingContext) {
super(importedModules, bindingContext);
}

Expand All @@ -144,17 +151,18 @@ public String getShortName() {

@Override
public URI getXmlNamespace() {
return URI.create("http://example.com/");
return NAMESPACE;
}

@Override
public URI getJsonBaseUri() {
return URI.create("http://example.com/");
return NAMESPACE;
}
}

@MetaschemaField(name = "test-field", moduleClass = TestModule.class)
public static class TestField implements IBoundObject {
@SuppressWarnings("unused")
public TestField(IMetaschemaData data) {
// do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void testJsonReadField()
MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser);

// read the top-level definition
MultiFieldAssembly obj = (MultiFieldAssembly) parser.readObject(classBinding);
MultiFieldAssembly obj = ObjectUtils.requireNonNull((MultiFieldAssembly) parser.readObject(classBinding));

assertAll(
() -> assertEquals("field1value", obj.getField1()),
Expand Down Expand Up @@ -118,7 +118,7 @@ void testJsonReadMissingFieldValue()
MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser);

// read the top-level definition
MultiFieldAssembly obj = (MultiFieldAssembly) parser.readObject(classBinding);
MultiFieldAssembly obj = ObjectUtils.requireNonNull((MultiFieldAssembly) parser.readObject(classBinding));

assertAll(
() -> assertNull(obj.getField1()),
Expand All @@ -144,7 +144,7 @@ void testJsonReadFieldValueKey()
MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser);

// read the top-level definition
MultiFieldAssembly obj = (MultiFieldAssembly) parser.readObject(classBinding);
MultiFieldAssembly obj = ObjectUtils.requireNonNull((MultiFieldAssembly) parser.readObject(classBinding));

assertAll(
() -> assertEquals("theValue", obj.getField3().getValue()));
Expand All @@ -168,7 +168,7 @@ void testJsonReadFieldDefaultValueKey()
MetaschemaJsonReader parser = new MetaschemaJsonReader(jsonParser);

// read the top-level definition
MultiFieldAssembly obj = (MultiFieldAssembly) parser.readObject(classBinding);
MultiFieldAssembly obj = ObjectUtils.requireNonNull((MultiFieldAssembly) parser.readObject(classBinding));

assertAll(
() -> assertEquals("theValue", obj.getField4().getValue()));
Expand Down

0 comments on commit cc99272

Please sign in to comment.