From c3d43a1ce71f145639a52cdc79463d9aae40577d Mon Sep 17 00:00:00 2001 From: Davy Landman Date: Mon, 2 Sep 2024 11:21:00 +0200 Subject: [PATCH] Fixed a few missing null annotations (#269) * Fixed a few missing null annotations * Fixed a few missing null annotations --- .../vallang/IWithKeywordParameters.java | 4 +- .../AbstractDefaultWithKeywordParameters.java | 4 +- .../usethesource/vallang/ValueProvider.java | 37 ++++++++++++------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/usethesource/vallang/IWithKeywordParameters.java b/src/main/java/io/usethesource/vallang/IWithKeywordParameters.java index 29c12e0c9..1dc772c5c 100644 --- a/src/main/java/io/usethesource/vallang/IWithKeywordParameters.java +++ b/src/main/java/io/usethesource/vallang/IWithKeywordParameters.java @@ -14,7 +14,7 @@ import java.util.Map; import java.util.Set; - +import org.checkerframework.checker.nullness.qual.Nullable; import io.usethesource.vallang.exceptions.FactTypeUseException; public interface IWithKeywordParameters { @@ -25,7 +25,7 @@ public interface IWithKeywordParameters { * @param label identifies the parameter * @return a value if the parameter has a value on this node or null otherwise */ - public IValue getParameter(String label); + public @Nullable IValue getParameter(String label); /** * Set the value of an parameter diff --git a/src/main/java/io/usethesource/vallang/impl/fields/AbstractDefaultWithKeywordParameters.java b/src/main/java/io/usethesource/vallang/impl/fields/AbstractDefaultWithKeywordParameters.java index 0ad3ff308..210f0c308 100644 --- a/src/main/java/io/usethesource/vallang/impl/fields/AbstractDefaultWithKeywordParameters.java +++ b/src/main/java/io/usethesource/vallang/impl/fields/AbstractDefaultWithKeywordParameters.java @@ -66,7 +66,7 @@ public String toString() { } @Override - public IValue getParameter(String label) { + public @Nullable IValue getParameter(String label) { return parameters.get(label); } @@ -139,7 +139,7 @@ public boolean equals(@Nullable Object other) { // TODO: there should be a faster way for this for (String key : parameters.keySet()) { - if (!getParameter(key).equals(o.getParameter(key))) { + if (!parameters.get(key).equals(o.getParameter(key))) { return false; } } diff --git a/src/test/java/io/usethesource/vallang/ValueProvider.java b/src/test/java/io/usethesource/vallang/ValueProvider.java index 56f9a7562..fa556058a 100644 --- a/src/test/java/io/usethesource/vallang/ValueProvider.java +++ b/src/test/java/io/usethesource/vallang/ValueProvider.java @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Random; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -259,15 +260,16 @@ private static long hashSeed(String string) { * @param cls the class type of the parameter to generate an input for * @return a random object which is assignable to cls */ - private Object argument(IValueFactory vf, TypeStore ts, Class cls, ExpectedType expected, GivenValue givenValue, TypeConfig typeConfig, int depth, int width) { + private Object argument(IValueFactory vf, TypeStore ts, Class cls, @Nullable ExpectedType expected, GivenValue givenValue, TypeConfig typeConfig, int depth, int width) { if (givenValue != null) { try { if (expected != null) { - return new StandardTextReader().read(vf, ts, readType(ts, expected), new StringReader(givenValue.value())); - } - else { - return new StandardTextReader().read(vf, new StringReader(givenValue.value())); + Type type = readType(ts, expected); + if (type != null) { + return new StandardTextReader().read(vf, ts, type, new StringReader(givenValue.value())); + } } + return new StandardTextReader().read(vf, new StringReader(givenValue.value())); } catch (FactTypeUseException | IOException e) { System.err.println("[WARNING] failed to parse given value: " + givenValue.value()); } @@ -281,12 +283,13 @@ else if (cls.isAssignableFrom(TypeStore.class)) { } else if (cls.isAssignableFrom(Type.class)) { if (expected != null) { - return readType(ts, expected); - } - else { - RandomTypesConfig rtc = configureRandomTypes(typeConfig, depth); - return TypeFactory.getInstance().randomType(ts, rtc); + Type result = readType(ts, expected); + if (result != null) { + return result; + } } + RandomTypesConfig rtc = configureRandomTypes(typeConfig, depth); + return TypeFactory.getInstance().randomType(ts, rtc); } else if (cls.isAssignableFrom(TypeFactory.class)) { return TypeFactory.getInstance(); @@ -336,7 +339,7 @@ private RandomTypesConfig configureRandomTypes(TypeConfig typeConfig, int depth) * @param noAnnotations * @return an instance assignable to `cl` */ - private IValue generateValue(IValueFactory vf, TypeStore ts, Class cl, ExpectedType expected, int depth, int width) { + private IValue generateValue(IValueFactory vf, TypeStore ts, Class cl, @Nullable ExpectedType expected, int depth, int width) { Type expectedType = tf.voidType(); @@ -345,8 +348,14 @@ private IValue generateValue(IValueFactory vf, TypeStore ts, Class(), depth, width)); } - private static Type readType(TypeStore ts, ExpectedType expected) { + private static @Nullable Type readType(TypeStore ts, ExpectedType expected) { try { return tf.fromString(ts, new StringReader(expected.value())); } catch (IOException e) {