From 6546881559e4bbcfe2b220199a9f3cd4196d1960 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 9 Jun 2015 23:58:41 +1000 Subject: A few changes, as suggested by static analysis --- .../java/au/id/zancanaro/javacheck/Generator.java | 2 +- .../java/au/id/zancanaro/javacheck/Generators.java | 4 ++-- .../java/au/id/zancanaro/javacheck/ShrinkTree.java | 20 +++++++++++--------- .../au/id/zancanaro/javacheck/junit/Properties.java | 13 +++++-------- .../au/id/zancanaro/javacheck/object/CharType.java | 2 +- .../javacheck/object/GeneratorProvider.java | 5 +++-- .../id/zancanaro/javacheck/state/CommandValue.java | 4 ++-- .../zancanaro/javacheck/state/GeneratedCommand.java | 10 +++------- 8 files changed, 28 insertions(+), 32 deletions(-) (limited to 'src/main/java/au') 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 { ShrinkTree[] result = (ShrinkTree[]) new ShrinkTree[generators.length]; int index = 0; for (Generator 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 Generator oneOf(Generator... 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 boolShrinkStrategy() { - return value -> (value ? Stream.of(false) : Stream.empty()); + return value -> (value ? Stream.of(Boolean.FALSE) : Stream.empty()); } public static Generator 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 { return heads; } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "SuspiciousUninitializedArray"}) public static Stream[]> promoteChildren(ShrinkTree[] trees) { return IntStream.range(0, trees.length) .mapToObj(index -> trees[index].getChildren().map(child -> @@ -65,10 +65,11 @@ public class ShrinkTree { public static Stream[]> removeChildren(ShrinkTree[] 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 Stream[]> removeAndPromoteChildren(ShrinkTree[] trees) { @@ -90,14 +91,15 @@ public class ShrinkTree { () -> this.getChildren().map(tree -> tree.map(f))); } - public ShrinkTree flatMap(Function> f) { + public ShrinkTree flatMap(Function> f) { return ShrinkTree.join(this.map(f)); } public ShrinkTree filter(Predicate 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 { private static Stream> strategyStream(final T value, final ShrinkStrategy strategy) { return strategy.shrink(value) - .map(v -> new ShrinkTree(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 @@ -26,11 +26,6 @@ public class Properties extends BlockJUnit4ClassRunner { provider = GeneratorProvider.DEFAULT_PROVIDER.withDataSources(classObject); } - @Override - protected void collectInitializationErrors(List errors) { - super.collectInitializationErrors(errors); - } - @Override protected void validateConstructor(List 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>> trees = failed.getChildren().iterator(); while (trees.hasNext()) { ShrinkTree> tree = trees.next(); - if (seenArgs.add(Arrays.asList(tree.getValue()))) { + List 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 { @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 { 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 -- cgit v1.2.3