diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 23:58:41 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-09 23:58:41 +1000 |
commit | 6546881559e4bbcfe2b220199a9f3cd4196d1960 (patch) | |
tree | 16244beed46eff76b65d1e5304cae4591b7db7ee /src/main | |
parent | 40961d4950c40643d5d71721a7e024e3951323ce (diff) |
A few changes, as suggested by static analysis
Diffstat (limited to 'src/main')
8 files changed, 28 insertions, 32 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index ec7a16e..8642e8b 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -71,7 +71,7 @@ public interface Generator<T> { ShrinkTree<T>[] result = (ShrinkTree<T>[]) new ShrinkTree[generators.length]; int index = 0; for (Generator<? extends T> generator : generators) { - result[index++] = generator.generate(random, size).map(x -> (T) x); + result[index++] = generator.generate(random, size).map(Function.identity()); } return ShrinkTree.combine(result, ShrinkTree::promoteChildren); }; diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index 63d4ece..4c8e02e 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -41,7 +41,7 @@ public final class Generators { @SafeVarargs public static <T> Generator<T> oneOf(Generator<? extends T>... gens) { - return integer(0, gens.length).flatMap(index -> gens[index].map(x -> (T) x)); + return integer(0, gens.length).flatMap(index -> gens[index].map(Function.identity())); } @SafeVarargs @@ -60,7 +60,7 @@ public final class Generators { } private static ShrinkStrategy<Boolean> boolShrinkStrategy() { - return value -> (value ? Stream.of(false) : Stream.empty()); + return value -> (value ? Stream.of(Boolean.FALSE) : Stream.empty()); } public static Generator<Long> longInteger(long lower, long upper) { diff --git a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java index 4f4eb62..6e21f27 100644 --- a/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java @@ -52,7 +52,7 @@ public class ShrinkTree<T> { return heads; } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "SuspiciousUninitializedArray"}) public static <T> Stream<ShrinkTree<T>[]> promoteChildren(ShrinkTree<? super T>[] trees) { return IntStream.range(0, trees.length) .mapToObj(index -> trees[index].getChildren().map(child -> @@ -65,10 +65,11 @@ public class ShrinkTree<T> { public static <T> Stream<ShrinkTree<T>[]> removeChildren(ShrinkTree<? super T>[] trees) { return IntStream.range(0, trees.length) - .mapToObj(index -> IntStream.range(0, trees.length) - .filter(i -> i != index) - .mapToObj(i -> trees[i]) - .toArray(ShrinkTree[]::new)); + .mapToObj(index -> + IntStream.range(0, trees.length) + .filter(i -> i != index) + .mapToObj(i -> trees[i]) + .toArray(ShrinkTree[]::new)); } public static <T> Stream<ShrinkTree<T>[]> removeAndPromoteChildren(ShrinkTree<? super T>[] trees) { @@ -90,14 +91,15 @@ public class ShrinkTree<T> { () -> this.getChildren().map(tree -> tree.map(f))); } - public <R> ShrinkTree<R> flatMap(Function<?super T, ? extends ShrinkTree<R>> f) { + public <R> ShrinkTree<R> flatMap(Function<? super T, ? extends ShrinkTree<R>> f) { return ShrinkTree.join(this.map(f)); } public ShrinkTree<T> filter(Predicate<? super T> predicate) { - if (predicate.test(this.getValue())) { + T value = this.getValue(); + if (predicate.test(value)) { return new ShrinkTree<>( - this.getValue(), + value, () -> this.getChildren() .filter(tree -> predicate.test(tree.getValue())) .map(tree -> tree.filter(predicate))); @@ -112,7 +114,7 @@ public class ShrinkTree<T> { private static <T> Stream<ShrinkTree<T>> strategyStream(final T value, final ShrinkStrategy<T> strategy) { return strategy.shrink(value) - .map(v -> new ShrinkTree<T>(v, () -> strategyStream(v, strategy))); + .map(v -> new ShrinkTree<>(v, () -> strategyStream(v, strategy))); } public void print(Writer output) throws IOException { diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index a7bf19b..c428edc 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -27,11 +27,6 @@ public class Properties extends BlockJUnit4ClassRunner { } @Override - protected void collectInitializationErrors(List<Throwable> errors) { - super.collectInitializationErrors(errors); - } - - @Override protected void validateConstructor(List<Throwable> errors) { validateOnlyOneConstructor(errors); } @@ -109,7 +104,7 @@ public class Properties extends BlockJUnit4ClassRunner { } catch (AssumptionViolatedException ex) { numTests++; if (assumptionsViolated++ == 50) { - throw new Error("Violated 50 assumptions in a row: failing test"); + throw new Error("Violated 50 assumptions in a row: failing test", ex); } } catch (Throwable ex) { throw new PropertyError(method.getName(), seed, shrink(tree, ex)); @@ -130,9 +125,11 @@ public class Properties extends BlockJUnit4ClassRunner { Iterator<ShrinkTree<List<Object>>> trees = failed.getChildren().iterator(); while (trees.hasNext()) { ShrinkTree<List<Object>> tree = trees.next(); - if (seenArgs.add(Arrays.asList(tree.getValue()))) { + List<Object> value = tree.getValue(); + boolean haveSeen = seenArgs.add(Arrays.asList(value)); + if (haveSeen) { try { - runTest(tree.getValue().toArray()); + runTest(value.toArray()); } catch (AssumptionViolatedException ex) { // ignore, because it's not useful } catch (Throwable ex) { diff --git a/src/main/java/au/id/zancanaro/javacheck/object/CharType.java b/src/main/java/au/id/zancanaro/javacheck/object/CharType.java index aa3049e..b53d211 100644 --- a/src/main/java/au/id/zancanaro/javacheck/object/CharType.java +++ b/src/main/java/au/id/zancanaro/javacheck/object/CharType.java @@ -10,6 +10,6 @@ import java.lang.annotation.Target; public @interface CharType { public static enum TYPE { ALL, ASCII, ALPHA, ALPHA_NUMERIC - }; + } TYPE value(); } diff --git a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java index 67769b6..24b73d5 100644 --- a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java +++ b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java @@ -83,9 +83,10 @@ public interface GeneratorProvider { provider)); } else if (type == Map.class) { TypeVariable<?>[] params = Map.class.getTypeParameters(); + TypeVariable<?> keyType = params[0], valueType = params[1]; return mapOf( - provider.getGenerator(params[0], new Annotation[0], provider), - provider.getGenerator(params[1], new Annotation[0], provider)); + provider.getGenerator(keyType, new Annotation[0], provider), + provider.getGenerator(valueType, new Annotation[0], provider)); } else if (type instanceof Class) { return ofType((Class<?>) type, provider); } else if (type instanceof ParameterizedType) { diff --git a/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java b/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java index 1ca51a9..d2f3419 100644 --- a/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java +++ b/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java @@ -46,8 +46,8 @@ public class CommandValue<T> { @SuppressWarnings("unchecked") public T get() { - if (values.containsKey(getId())) { - return (T) values.get(getId()); + if (values.containsKey(id)) { + return (T) values.get(id); } else { throw new NoSuchElementException("Concrete values cannot be supplied prior to being calculated"); } diff --git a/src/main/java/au/id/zancanaro/javacheck/state/GeneratedCommand.java b/src/main/java/au/id/zancanaro/javacheck/state/GeneratedCommand.java index 23f306b..92a90f9 100644 --- a/src/main/java/au/id/zancanaro/javacheck/state/GeneratedCommand.java +++ b/src/main/java/au/id/zancanaro/javacheck/state/GeneratedCommand.java @@ -30,19 +30,15 @@ public class GeneratedCommand<S, A, R> { GeneratedCommand that = (GeneratedCommand) o; - if (args != null ? !args.equals(that.args) : that.args != null) - return false; - if (command != null ? !command.equals(that.command) : that.command != null) - return false; + return !(args != null ? !args.equals(that.args) : that.args != null) + && !(command != null ? !command.equals(that.command) : that.command != null); - return true; } @Override public int hashCode() { int result = command != null ? command.hashCode() : 0; - result = 31 * result + (args != null ? args.hashCode() : 0); - return result; + return 31 * result + (args != null ? args.hashCode() : 0); } @Override |