From 87d99195aabf9962b1f8ee9ebb496a661f42eea6 Mon Sep 17 00:00:00 2001
From: Diego Berrueta
Date: Mon, 7 Jan 2019 21:25:56 +1100
Subject: [PATCH] Add nullability annotations to main API and extension points
---
core/pom.xml | 4 ++++
.../generator/ComponentizedGenerator.java | 3 +++
.../junit/quickcheck/generator/Gen.java | 4 +++-
.../generator/GenerationStatus.java | 5 +++++
.../junit/quickcheck/generator/Generator.java | 19 +++++++++++++------
.../quickcheck/generator/Generators.java | 12 ++++++++++++
.../junit/quickcheck/generator/Shrink.java | 5 ++++-
.../generator/GeneratorRepository.java | 12 ++++++++++++
.../quickcheck/random/SourceOfRandomness.java | 5 +++++
pom.xml | 5 +++++
10 files changed, 66 insertions(+), 8 deletions(-)
diff --git a/core/pom.xml b/core/pom.xml
index a22775b45..162c48b83 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -42,6 +42,10 @@
org.slf4j
slf4j-api
+
+ com.google.code.findbugs
+ jsr305
+
org.hamcrest
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/ComponentizedGenerator.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/ComponentizedGenerator.java
index cc992dd09..68257c9e7 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/ComponentizedGenerator.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/ComponentizedGenerator.java
@@ -31,6 +31,8 @@ a copy of this software and associated documentation files (the
import org.javaruntype.type.TypeParameter;
+import javax.annotation.Nonnull;
+
import static java.util.Collections.*;
import static com.pholser.junit.quickcheck.internal.Reflection.*;
@@ -105,6 +107,7 @@ protected ComponentizedGenerator(Class type) {
/**
* @return this generator's component generators
*/
+ @Nonnull
protected List> componentGenerators() {
return unmodifiableList(components);
}
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/Gen.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/Gen.java
index 3971d0ca9..2073a2c2d 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/Gen.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/Gen.java
@@ -37,6 +37,8 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.internal.Weighted;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
+import javax.annotation.Nonnull;
+
import static java.util.stream.Collectors.*;
/**
@@ -57,7 +59,7 @@ public interface Gen {
* number of elements.
* @return the generated value
*/
- T generate(SourceOfRandomness random, GenerationStatus status);
+ T generate(@Nonnull SourceOfRandomness random, @Nonnull GenerationStatus status);
/**
* Gives a generation strategy that produces a random value by having this
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/GenerationStatus.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/GenerationStatus.java
index 1a5a7f647..cecc7995d 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/GenerationStatus.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/GenerationStatus.java
@@ -25,6 +25,8 @@ a copy of this software and associated documentation files (the
package com.pholser.junit.quickcheck.generator;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Optional;
@@ -65,6 +67,7 @@ default void semiAttempt() {
* @param value the associated value
* @return self, so that calls to this method can be chained
*/
+ @Nonnull
GenerationStatus setValue(Key key, T value);
/**
@@ -74,6 +77,7 @@ default void semiAttempt() {
* @param key key to look up
* @return the (optional) associated value
*/
+ @Nonnull
Optional valueOf(Key key);
/**
@@ -96,6 +100,7 @@ public Key(String name, Class type) {
this.type = type;
}
+ @Nullable
public T cast(Object o) {
return type.cast(o);
}
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/Generator.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/Generator.java
index cae3e591a..659db3791 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/Generator.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/Generator.java
@@ -42,6 +42,8 @@ a copy of this software and associated documentation files (the
import org.javaruntype.type.Types;
import org.javaruntype.type.WildcardTypeParameter;
+import javax.annotation.Nonnull;
+
import static java.math.BigDecimal.*;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
@@ -82,6 +84,7 @@ protected Generator(List> types) {
* @return class tokens for the types of property parameters this generator
* is applicable to
*/
+ @Nonnull
public List> types() {
return unmodifiableList(types);
}
@@ -105,7 +108,8 @@ public boolean canRegisterAsType(Class> type) {
* participate} in shrinking the given value, and if so, they
* {@linkplain #doShrink(SourceOfRandomness, Object) produce shrinks}.
*/
- @Override public final List shrink(SourceOfRandomness random, Object larger) {
+ @Nonnull
+ @Override public final List shrink(@Nonnull SourceOfRandomness random, Object larger) {
if (!canShrink(larger)) {
throw new IllegalStateException(
getClass() + " not capable of shrinking " + larger);
@@ -141,7 +145,8 @@ public boolean canShrink(Object larger) {
* @param larger the larger object
* @return objects that are "smaller" than the larger object
*/
- public List doShrink(SourceOfRandomness random, T larger) {
+ @Nonnull
+ public List doShrink(@Nonnull SourceOfRandomness random, T larger) {
return emptyList();
}
@@ -159,6 +164,7 @@ public List doShrink(SourceOfRandomness random, T larger) {
* @param value the value to assess
* @return a measure of the given value's magnitude
*/
+ @Nonnull
public BigDecimal magnitude(Object value) {
return ONE;
}
@@ -214,7 +220,7 @@ public int numberOfNeededComponents() {
*
* @param newComponents component generators to add
*/
- public void addComponentGenerators(List> newComponents) {
+ public void addComponentGenerators(@Nonnull List> newComponents) {
// do nothing by default
}
@@ -224,7 +230,7 @@ public void addComponentGenerators(List> newComponents) {
* for property parameters that have the given type parameters in their
* signatures
*/
- public boolean canGenerateForParametersOfTypes(List> typeParameters) {
+ public boolean canGenerateForParametersOfTypes(@Nonnull List> typeParameters) {
return true;
}
@@ -254,14 +260,14 @@ public boolean canGenerateForParametersOfTypes(List> typeParame
* "understand" one of the generation configuration annotations on
* the annotated type
*/
- public void configure(AnnotatedType annotatedType) {
+ public void configure(@Nonnull AnnotatedType annotatedType) {
configureStrict(collectConfigurationAnnotations(annotatedType));
}
/**
* @param element an annotated program element
*/
- public void configure(AnnotatedElement element) {
+ public void configure(@Nonnull AnnotatedElement element) {
configureLenient(collectConfigurationAnnotations(element));
}
@@ -282,6 +288,7 @@ public void provide(Generators provided) {
*
* @return a copy of the receiver
*/
+ @Nonnull
@SuppressWarnings("unchecked")
public Generator copy() {
return (Generator) instantiate(getClass());
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/Generators.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/Generators.java
index 78581819b..79546ee5b 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/Generators.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/Generators.java
@@ -30,6 +30,8 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
+import javax.annotation.Nonnull;
+
/**
* An access point for available generators.
*/
@@ -47,6 +49,7 @@ public interface Generators {
* @param rest other (related) types of generated values
* @return generator that can produce values of the given types
*/
+ @Nonnull
Generator oneOf(
Class extends T> first,
Class extends T>... rest);
@@ -63,6 +66,7 @@ Generator oneOf(
* @param rest other generators
* @return generator that can produce values using the given generators
*/
+ @Nonnull
Generator oneOf(
Generator extends T> first,
Generator extends T>... rest);
@@ -79,6 +83,7 @@ Generator oneOf(
* @param fieldName name of a field
* @return generator that can produce values of the field's type
*/
+ @Nonnull
Generator> field(Class> type, String fieldName);
/**
@@ -97,6 +102,7 @@ Generator oneOf(
* @param argumentTypes types of arguments to the constructor
* @return generator that can produce values using the constructor
*/
+ @Nonnull
Generator constructor(Class type, Class>... argumentTypes);
/**
@@ -114,6 +120,7 @@ Generator oneOf(
* @param type a type
* @return generator that can produce values of that type
*/
+ @Nonnull
Generator fieldsOf(Class type);
/**
@@ -126,6 +133,7 @@ Generator oneOf(
* @return generator that can produce values of that type
* @see ComponentizedGenerator
*/
+ @Nonnull
Generator type(Class type, Class>... componentTypes);
/**
@@ -139,6 +147,7 @@ Generator oneOf(
* @param parameter a reflected method parameter
* @return generator that can produce values of the parameter's type
*/
+ @Nonnull
Generator> parameter(Parameter parameter);
/**
@@ -152,6 +161,7 @@ Generator oneOf(
* @param field a reflected field
* @return generator that can produce values of the field's type
*/
+ @Nonnull
Generator> field(Field field);
/**
@@ -168,6 +178,7 @@ Generator oneOf(
* @return a generator for producing values
* @see ComponentizedGenerator
*/
+ @Nonnull
> T make(
Class genType,
Generator>... componentGenerators);
@@ -182,5 +193,6 @@ > T make(
* @return a generator access point that has the source of randomness
* available to it
*/
+ @Nonnull
Generators withRandom(SourceOfRandomness random);
}
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/generator/Shrink.java b/core/src/main/java/com/pholser/junit/quickcheck/generator/Shrink.java
index 04290944f..1cc64c5bb 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/generator/Shrink.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/generator/Shrink.java
@@ -29,6 +29,8 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
+import javax.annotation.Nonnull;
+
/**
* Represents a strategy for producing objects "smaller than" a given object.
*
@@ -43,5 +45,6 @@ public interface Shrink {
* @param larger the larger object
* @return objects that are "smaller" than the larger object
*/
- List shrink(SourceOfRandomness random, Object larger);
+ @Nonnull
+ List shrink(@Nonnull SourceOfRandomness random, Object larger);
}
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/GeneratorRepository.java b/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/GeneratorRepository.java
index b409dee1d..a79a59d1d 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/GeneratorRepository.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/internal/generator/GeneratorRepository.java
@@ -54,6 +54,8 @@ a copy of this software and associated documentation files (the
import org.javaruntype.type.TypeParameter;
import org.javaruntype.type.Types;
+import javax.annotation.Nonnull;
+
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
@@ -126,10 +128,12 @@ private void registerGeneratorForType(
forType.add(generator);
}
+ @Nonnull
@Override public Generator> field(Class> type, String fieldName) {
return field(findField(type, fieldName));
}
+ @Nonnull
@Override public Generator constructor(
Class type,
Class>... argumentTypes) {
@@ -142,6 +146,7 @@ private void registerGeneratorForType(
return ctor;
}
+ @Nonnull
@Override public Generator fieldsOf(Class type) {
Fields fields = new Fields<>(type);
@@ -151,6 +156,7 @@ private void registerGeneratorForType(
return fields;
}
+ @Nonnull
@SuppressWarnings("unchecked")
@Override public Generator type(Class type, Class>... componentTypes) {
Generator generator =
@@ -160,6 +166,7 @@ private void registerGeneratorForType(
return generator;
}
+ @Nonnull
@Override public Generator> parameter(Parameter parameter) {
return produceGenerator(
new ParameterTypeContext(
@@ -169,6 +176,7 @@ private void registerGeneratorForType(
).annotate(parameter));
}
+ @Nonnull
@Override public Generator> field(Field field) {
return produceGenerator(
new ParameterTypeContext(
@@ -178,6 +186,7 @@ private void registerGeneratorForType(
).annotate(field));
}
+ @Nonnull
@SafeVarargs
@SuppressWarnings("unchecked")
@Override public final Generator oneOf(
@@ -191,6 +200,7 @@ private void registerGeneratorForType(
.toArray(Generator[]::new));
}
+ @Nonnull
@SafeVarargs
@SuppressWarnings("unchecked")
@Override public final Generator oneOf(
@@ -208,6 +218,7 @@ private void registerGeneratorForType(
return (Generator) new CompositeGenerator(weightings);
}
+ @Nonnull
@Override public final > T make(
Class genType,
Generator>... components) {
@@ -220,6 +231,7 @@ private void registerGeneratorForType(
return generator;
}
+ @Nonnull
@Override public final Generators withRandom(SourceOfRandomness other) {
return new GeneratorRepository(other, this.generators);
}
diff --git a/core/src/main/java/com/pholser/junit/quickcheck/random/SourceOfRandomness.java b/core/src/main/java/com/pholser/junit/quickcheck/random/SourceOfRandomness.java
index d245215f2..6dcdcfc16 100644
--- a/core/src/main/java/com/pholser/junit/quickcheck/random/SourceOfRandomness.java
+++ b/core/src/main/java/com/pholser/junit/quickcheck/random/SourceOfRandomness.java
@@ -38,6 +38,8 @@ a copy of this software and associated documentation files (the
import com.pholser.junit.quickcheck.internal.Ranges;
+import javax.annotation.Nonnull;
+
import static java.util.concurrent.TimeUnit.*;
import static com.pholser.junit.quickcheck.internal.Ranges.*;
@@ -297,6 +299,7 @@ public short nextShort(short min, short max) {
* @return a random {@code BigInteger}
* @see BigInteger#BigInteger(int, java.util.Random)
*/
+ @Nonnull
public BigInteger nextBigInteger(int numberOfBits) {
return new BigInteger(numberOfBits, delegate);
}
@@ -309,6 +312,7 @@ public BigInteger nextBigInteger(int numberOfBits) {
* @param max upper bound of the desired interval
* @return a random value
*/
+ @Nonnull
public Instant nextInstant(Instant min, Instant max) {
int comparison = checkRange(Ranges.Type.STRING, min, max);
if (comparison == 0)
@@ -331,6 +335,7 @@ public Instant nextInstant(Instant min, Instant max) {
* @param max upper bound of the desired interval
* @return a random value
*/
+ @Nonnull
public Duration nextDuration(Duration min, Duration max) {
int comparison = checkRange(Ranges.Type.STRING, min, max);
if (comparison == 0)
diff --git a/pom.xml b/pom.xml
index 155555bca..0c301f0b2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -149,6 +149,11 @@
logback-classic
1.2.3
+
+ com.google.code.findbugs
+ jsr305
+ 3.0.2
+