From 816a5b89c5ddb8e295b8e8e46075da3cb694b2a7 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 1 Jun 2015 13:10:49 +1000 Subject: Fix up static analysis issues (FindBugs and Intellij IDEA analysis) --- .../java/au/id/zancanaro/javacheck/Generator.java | 4 +--- .../java/au/id/zancanaro/javacheck/Generators.java | 8 +++---- .../java/au/id/zancanaro/javacheck/Iterators.java | 17 ++++++++------- .../java/au/id/zancanaro/javacheck/RoseTree.java | 11 +++++----- .../au/id/zancanaro/javacheck/ShrinkResult.java | 4 +++- .../id/zancanaro/javacheck/junit/Properties.java | 24 ++++++++++----------- .../zancanaro/javacheck/junit/PropertyError.java | 25 +++++++++------------- 7 files changed, 44 insertions(+), 49 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 076f7c2..2d2e556 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -40,8 +40,6 @@ public interface Generator { } default Generator flatMap(Function> action) { - return (random, size) -> { - return RoseTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); - }; + return (random, size) -> RoseTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); } } diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index 08b9989..bf1b1b3 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -15,13 +15,13 @@ public final class Generators { return (random, size) -> makeGenerator.apply(size).generate(random, size); } - public static Generator suchThat(Generator gen, Predicate pred) { + public static Generator suchThat(Generator gen, Predicate predicate) { return (random, size) -> { RoseTree result = gen.generate(random, size); - if (pred.test(result.getValue())) { - return result.filter(pred); + if (predicate.test(result.getValue())) { + return result.filter(predicate); } else { - return suchThat(gen, pred).generate(random, size); + return suchThat(gen, predicate).generate(random, size); } }; } diff --git a/src/main/java/au/id/zancanaro/javacheck/Iterators.java b/src/main/java/au/id/zancanaro/javacheck/Iterators.java index 347a927..33652b2 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Iterators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Iterators.java @@ -3,6 +3,7 @@ package au.id.zancanaro.javacheck; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import java.util.function.Function; import java.util.function.Predicate; @@ -11,7 +12,7 @@ public final class Iterators { } public static RangeIterator rangeIterator(int countTo, Function fn) { - return new RangeIterator(countTo, fn); + return new RangeIterator<>(countTo, fn); } private static class RangeIterator implements Iterator { @@ -39,9 +40,9 @@ public final class Iterators { return new FlattenIterator<>(iterators); } public static class FlattenIterator implements Iterator { - private Iterator current; + private final Iterator> iterators; - private Iterator> iterators; + private Iterator current; public FlattenIterator(Iterator> iterators) { this.current = Iterators.emptyIterator(); @@ -105,7 +106,7 @@ public final class Iterators { } @Override public T next() { - return null; + throw new NoSuchElementException("Empty iterators contain no elements"); } } @@ -134,8 +135,8 @@ public final class Iterators { } - public static FilteringIterator filteringIterator(Predicate pred, Iterator iterator) { - return new FilteringIterator<>(pred, iterator); + public static FilteringIterator filteringIterator(Predicate predicate, Iterator iterator) { + return new FilteringIterator<>(predicate, iterator); } private static class FilteringIterator implements Iterator { @@ -143,8 +144,8 @@ public final class Iterators { private final Iterator iterator; private List nextValue; - public FilteringIterator(Predicate pred, Iterator iterator) { - this.predicate = pred; + public FilteringIterator(Predicate predicate, Iterator iterator) { + this.predicate = predicate; this.iterator = iterator; this.nextValue = null; } diff --git a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java index 86952da..5a54d6f 100644 --- a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java @@ -36,8 +36,9 @@ public class RoseTree { } private static Iterator[]> permutations(RoseTree[] trees) { - return Iterators.flatten(Iterators.rangeIterator(trees.length, index -> - Iterators.mappingIterator(child -> { + return Iterators.flatten( + Iterators.rangeIterator(trees.length, + index -> Iterators.mappingIterator(child -> { @SuppressWarnings("unchecked") RoseTree[] result = (RoseTree[]) new RoseTree[trees.length]; for (int i = 0; i < trees.length; ++i) { @@ -45,14 +46,14 @@ public class RoseTree { } return result; }, trees[index].getChildren()) - )); + )); } public static RoseTree zip(Function, R> fn, RoseTree[] trees) { @SuppressWarnings("unchecked") List heads = new ArrayList(trees.length); - for (int i = 0; i < trees.length; ++i) { - heads.add(trees[i].getValue()); + for (RoseTree tree : trees) { + heads.add(tree.getValue()); } return new RoseTree<>( fn.apply(heads), diff --git a/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java index e1463f0..8776f4e 100644 --- a/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java @@ -1,11 +1,13 @@ package au.id.zancanaro.javacheck; +import java.util.Arrays; + public class ShrinkResult { public final Object[] args; public final Throwable thrown; public ShrinkResult(Object[] args, Throwable thrown) { - this.args = args; + this.args = Arrays.copyOf(args, args.length); this.thrown = thrown; } } 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 0507413..64bd9ab 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -16,12 +16,11 @@ import org.junit.runners.model.TestClass; import java.lang.reflect.*; import java.util.*; - public class Properties extends BlockJUnit4ClassRunner { private final Map> generators = new HashMap<>(); - public Properties(Class klass) throws InitializationError { - super(klass); + public Properties(Class classObject) throws InitializationError { + super(classObject); } @Override @@ -43,11 +42,11 @@ public class Properties extends BlockJUnit4ClassRunner { if (!(type instanceof ParameterizedType)) { continue; } - ParameterizedType ptype = (ParameterizedType) type; - if (!(ptype.getRawType() instanceof Class)) { + ParameterizedType parameterizedType = (ParameterizedType) type; + if (!(parameterizedType.getRawType() instanceof Class)) { continue; } - Class c = (Class) ptype.getRawType(); + Class c = (Class) parameterizedType.getRawType(); if (c != Generator.class) { continue; } @@ -61,7 +60,7 @@ public class Properties extends BlockJUnit4ClassRunner { error = true; } if (!error) { - result.add(ptype.getActualTypeArguments()[0]); + result.add(parameterizedType.getActualTypeArguments()[0]); } } return result; @@ -105,16 +104,16 @@ public class Properties extends BlockJUnit4ClassRunner { if (!(type instanceof ParameterizedType)) { continue; } - ParameterizedType ptype = (ParameterizedType) type; - if (!(ptype.getRawType() instanceof Class)) { + ParameterizedType parameterizedType = (ParameterizedType) type; + if (!(parameterizedType.getRawType() instanceof Class)) { continue; } - Class c = (Class) ptype.getRawType(); + Class c = (Class) parameterizedType.getRawType(); if (c != Generator.class) { continue; } try { - Type target = ptype.getActualTypeArguments()[0]; + Type target = parameterizedType.getActualTypeArguments()[0]; @SuppressWarnings("unchecked") Generator generator = (Generator) field.get(null); generators.put(target, generator); @@ -122,7 +121,7 @@ public class Properties extends BlockJUnit4ClassRunner { generators.put(rawTypes.get(target), generator); } } catch (IllegalAccessException ex) { - + throw new RuntimeException(ex); } } } @@ -213,7 +212,6 @@ public class Properties extends BlockJUnit4ClassRunner { if (assumptionsViolated++ == 50) { throw new Error("Violated 50 assumptions in a row: failing test"); } - ; } catch (Throwable ex) { // tree.print(new OutputStreamWriter(System.out), Arrays::toString); throw new PropertyError(method.getName(), seed, shrink(tree, ex)); diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java index 138c47f..2cd1325 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java @@ -3,35 +3,30 @@ package au.id.zancanaro.javacheck.junit; import au.id.zancanaro.javacheck.ShrinkResult; import java.util.Arrays; -import java.util.Collection; import java.util.Iterator; public class PropertyError extends AssertionError { public PropertyError(String methodName, long seed, ShrinkResult shrunk) { super(shrunk.thrown.getMessage() == null ? - String.format("%s(%s)\n\tSeed: %s", - methodName, join(", ", shrunk.args), + String.format("%s(%s)%n\tSeed: %s", + methodName, joinArgs(shrunk.args), seed): - String.format("%s(%s)\n\tSeed: %s\n%s", - methodName, join(", ", shrunk.args), + String.format("%s(%s)%n\tSeed: %s%n%s", + methodName, joinArgs(shrunk.args), seed, shrunk.thrown.getMessage())); initCause(shrunk.thrown); } - public static String join(String delimiter, Object... params) { - return join(delimiter, Arrays.asList(params)); - } - - public static String join(String delimiter, Collection values) { + public static String joinArgs(Object... params) { StringBuilder sb = new StringBuilder(); - Iterator iter = values.iterator(); - while (iter.hasNext()) { - Object next = iter.next(); + Iterator iterator = Arrays.asList(params).iterator(); + while (iterator.hasNext()) { + Object next = iterator.next(); sb.append(stringValueOf(next)); - if (iter.hasNext()) { - sb.append(delimiter); + if (iterator.hasNext()) { + sb.append(", "); } } return sb.toString(); -- cgit v1.2.3