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

Fixed a few missing null annotations #269

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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 @@ -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<T extends IValue> {
Expand All @@ -25,7 +25,7 @@ public interface IWithKeywordParameters<T extends IValue> {
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String toString() {
}

@Override
public IValue getParameter(String label) {
public @Nullable IValue getParameter(String label) {
return parameters.get(label);
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
37 changes: 23 additions & 14 deletions src/test/java/io/usethesource/vallang/ValueProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<? extends IValue> cl, ExpectedType expected, int depth, int width) {
private IValue generateValue(IValueFactory vf, TypeStore ts, Class<? extends IValue> cl, @Nullable ExpectedType expected, int depth, int width) {
Type expectedType = tf.voidType();


Expand All @@ -345,8 +348,14 @@ private IValue generateValue(IValueFactory vf, TypeStore ts, Class<? extends IVa
int i = 0;
while (expectedType.isBottom() && i++ < 1000) {
if (expected != null) {
expectedType = readType(ts, expected);
break;
Type read = readType(ts, expected);
if (read == null) {
expected = null;
}
else {
expectedType = read;
break;
}
}
else {
expectedType = types
Expand All @@ -364,7 +373,7 @@ private IValue generateValue(IValueFactory vf, TypeStore ts, Class<? extends IVa
return (previous = expectedType.randomValue(rnd, vf, ts, new HashMap<>(), 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) {
Expand Down
Loading