From b0c8c8f0a3ebb48fe791a63a0a7fde6033aa81ae Mon Sep 17 00:00:00 2001 From: svc-excavator-bot Date: Mon, 20 Jan 2020 20:55:34 +0000 Subject: [PATCH 1/6] Excavator: Upgrades Baseline to the latest version --- .baseline/checkstyle/checkstyle.xml | 22 +++++++++++----------- build.gradle | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.baseline/checkstyle/checkstyle.xml b/.baseline/checkstyle/checkstyle.xml index 792229cf..cd19cae4 100644 --- a/.baseline/checkstyle/checkstyle.xml +++ b/.baseline/checkstyle/checkstyle.xml @@ -53,6 +53,9 @@ + + + @@ -84,11 +87,13 @@ + org.apache.commons.lang3.Validate.*, + org.assertj.core.api.Assertions.*, + org.mockito.Mockito.*"/> @@ -110,7 +115,6 @@ - @@ -369,10 +373,6 @@ - - - - @@ -422,6 +422,7 @@ + @@ -435,7 +436,7 @@ - + @@ -468,10 +469,9 @@ - + - diff --git a/build.gradle b/build.gradle index 206b7d37..cc776aa3 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ buildscript { classpath 'com.netflix.nebula:gradle-info-plugin:7.1.4' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5' classpath 'com.netflix.nebula:nebula-publishing-plugin:16.0.0' - classpath 'com.palantir.baseline:gradle-baseline-java:0.65.0' + classpath 'com.palantir.baseline:gradle-baseline-java:2.49.1' classpath 'com.palantir.gradle.gitversion:gradle-git-version:0.12.3' } } From 90ca781431945475721331a6bd7c24e16e076769 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 6 May 2020 23:37:51 +0100 Subject: [PATCH 2/6] versions --- versions.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/versions.props b/versions.props index 21dc92f7..c52c5f41 100644 --- a/versions.props +++ b/versions.props @@ -1,6 +1,6 @@ com.fasterxml.jackson.*:jackson-* = 2.11.0 com.google.code.findbugs:jsr305 = 3.0.2 -com.google.errorprone:error_prone_annotations = 2.3.4 +com.google.errorprone:* = 2.3.4 com.google.guava:guava = 27.0.1-jre junit:junit = 4.13 org.assertj:* = 3.16.0 From a1cd612a0788297376b6d771184b7eb886dc7629 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 6 May 2020 23:46:17 +0100 Subject: [PATCH 3/6] fix errorprone --- human-readable-types/build.gradle | 1 + .../HumanReadableByteCount.java | 18 +++-- .../HumanReadableDuration.java | 10 ++- .../humanreadabletypes/Preconditions.java | 81 ------------------- versions.props | 1 + 5 files changed, 18 insertions(+), 93 deletions(-) delete mode 100644 human-readable-types/src/main/java/com/palantir/humanreadabletypes/Preconditions.java diff --git a/human-readable-types/build.gradle b/human-readable-types/build.gradle index 90cd0fb5..83ead1d9 100644 --- a/human-readable-types/build.gradle +++ b/human-readable-types/build.gradle @@ -19,6 +19,7 @@ apply from: "$rootDir/gradle/publish-jar.gradle" dependencies { compile 'com.fasterxml.jackson.core:jackson-databind' compile 'com.fasterxml.jackson.datatype:jackson-datatype-guava' + implementation 'com.palantir.safe-logging:preconditions' testCompile 'junit:junit' testCompile 'org.assertj:assertj-core' diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java index cd67c484..470081a0 100644 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java +++ b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; +import com.palantir.logsafe.Preconditions; +import com.palantir.logsafe.SafeArg; import java.io.Serializable; import java.util.HashMap; import java.util.Locale; @@ -136,7 +138,7 @@ public static HumanReadableByteCount pebibytes(long size) { * @param byteCount the string representation of this byte string * @return the parsed {@link HumanReadableByteCount} * @throws IllegalArgumentException if the provided byte string is invalid - * @throws NumberFormatException if the provided size cannot be parsed + * @throws NumberFormatException if the provided size cannot be parsed */ @JsonCreator public static HumanReadableByteCount valueOf(String byteCount) { @@ -145,7 +147,7 @@ public static HumanReadableByteCount valueOf(String byteCount) { try { Matcher matcher = BYTE_COUNT_PATTERN.matcher(lower); - Preconditions.checkArgument(matcher.matches(), "Invalid byte string: %s", byteCount); + Preconditions.checkArgument(matcher.matches(), "Invalid byte string", SafeArg.of("byteCount", byteCount)); long size = Long.parseLong(matcher.group(1)); String suffix = matcher.group(2); @@ -155,7 +157,6 @@ public static HumanReadableByteCount valueOf(String byteCount) { } return new HumanReadableByteCount(size, suffix != null ? SUFFIXES.get(suffix) : ByteUnit.BYTE); - } catch (NumberFormatException e) { String byteError = "Size must be specified as bytes (b), " + "kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). " @@ -194,10 +195,10 @@ public long toBytes() { /** * Compares this byte string to the specified {@code HumanReadableByteCount}. *

- * The comparison is based on the total number of bytes. - * It is "consistent with equals", as defined by {@link Comparable}. + * The comparison is based on the total number of bytes. It is "consistent with equals", as defined by {@link + * Comparable}. * - * @param otherByteCount the other byte string to compare to, not null + * @param otherByteCount the other byte string to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override @@ -213,7 +214,7 @@ public int compareTo(HumanReadableByteCount otherByteCount) { *

* The comparison is based on the total number of bytes. * - * @param otherByteCount the other byte string, null returns false + * @param otherByteCount the other byte string, null returns false * @return true if the other byte string is equal to this one */ @Override @@ -274,7 +275,8 @@ enum ByteUnit { } public long toBytes(long sizeValue) { - Preconditions.checkArgument(sizeValue >= 0, "Negative size value. Size must be positive: %s", sizeValue); + Preconditions.checkArgument(sizeValue >= 0, "Negative size value. Size must be positive", + SafeArg.of("size", sizeValue)); return sizeValue * multiplier; } diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java index 466fe5da..aee6fb33 100644 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java +++ b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java @@ -6,13 +6,15 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; +import com.palantir.logsafe.Preconditions; +import com.palantir.logsafe.SafeArg; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import java.io.Serializable; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.HashMap; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -143,7 +145,7 @@ public static HumanReadableDuration days(long count) { @JsonCreator public static HumanReadableDuration valueOf(String duration) { final Matcher matcher = DURATION_PATTERN.matcher(duration); - Preconditions.checkArgument(matcher.matches(), "Invalid duration: %s", duration); + Preconditions.checkArgument(matcher.matches(), "Invalid duration", SafeArg.of("duration", duration)); final long count = Long.parseLong(matcher.group(1)); final TimeUnit unit = SUFFIXES.get(matcher.group(2)); @@ -240,7 +242,7 @@ public Duration toJavaDuration() { * @return the converted unit, not null */ private static ChronoUnit chronoUnit(TimeUnit unit) { - Objects.requireNonNull(unit, "unit"); + Preconditions.checkNotNull(unit, "unit"); switch (unit) { case NANOSECONDS: return ChronoUnit.NANOS; @@ -257,7 +259,7 @@ private static ChronoUnit chronoUnit(TimeUnit unit) { case DAYS: return ChronoUnit.DAYS; } - throw new IllegalArgumentException("Unknown TimeUnit constant"); + throw new SafeIllegalArgumentException("Unknown TimeUnit constant"); } /** diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/Preconditions.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/Preconditions.java deleted file mode 100644 index 18b123a2..00000000 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/Preconditions.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. - */ - -package com.palantir.humanreadabletypes; - -/** See Guava's {@code Preconditions class}. */ -final class Preconditions { - private Preconditions() {} - - static void checkArgument(boolean expression, Object errorMessage) { - if (!expression) { - throw new IllegalArgumentException(String.valueOf(errorMessage)); - } - } - - static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { - if (!expression) { - throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs)); - } - } - - static void checkState(boolean expression, Object errorMessage) { - if (!expression) { - throw new IllegalStateException(String.valueOf(errorMessage)); - } - } - - static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { - if (!expression) { - throw new IllegalStateException(format(errorMessageTemplate, errorMessageArgs)); - } - } - - static T checkNotNull(T reference, Object errorMessage) { - if (reference == null) { - throw new NullPointerException(String.valueOf(errorMessage)); - } - return reference; - } - - static T checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) { - if (reference == null) { - // If either of these parameters is null, the right thing happens anyway - throw new NullPointerException(format(errorMessageTemplate, errorMessageArgs)); - } - return reference; - } - - private static String format(String templateString, Object... args) { - String template = String.valueOf(templateString); // null -> "null" - - // start substituting the arguments into the '%s' placeholders - StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); - int templateStart = 0; - int index = 0; - while (index < args.length) { - int placeholderStart = template.indexOf("%s", templateStart); - if (placeholderStart == -1) { - break; - } - builder.append(template.substring(templateStart, placeholderStart)); - builder.append(args[index++]); - templateStart = placeholderStart + 2; - } - builder.append(template.substring(templateStart)); - - // if we run out of placeholders, append the extra args in square braces - if (index < args.length) { - builder.append(" ["); - builder.append(args[index++]); - while (index < args.length) { - builder.append(", "); - builder.append(args[index++]); - } - builder.append(']'); - } - - return builder.toString(); - } -} diff --git a/versions.props b/versions.props index c52c5f41..a004dcd7 100644 --- a/versions.props +++ b/versions.props @@ -2,6 +2,7 @@ com.fasterxml.jackson.*:jackson-* = 2.11.0 com.google.code.findbugs:jsr305 = 3.0.2 com.google.errorprone:* = 2.3.4 com.google.guava:guava = 27.0.1-jre +com.palantir.safe-logging:* = 1.13.0 junit:junit = 4.13 org.assertj:* = 3.16.0 org.checkerframework:checker-qual = 2.5.3 From c751eca5dde23c90a7f394c605f98df16ffa334a Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 6 May 2020 23:48:27 +0100 Subject: [PATCH 4/6] fix tests --- .../humanreadabletypes/HumanReadableByteCountTests.java | 2 +- .../palantir/humanreadabletypes/HumanReadableDurationTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableByteCountTests.java b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableByteCountTests.java index a5245019..b56e5e6a 100644 --- a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableByteCountTests.java +++ b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableByteCountTests.java @@ -69,7 +69,7 @@ public void testParsePebiBytes() { public void testInvalidString() { assertThatThrownBy(() -> HumanReadableByteCount.valueOf("Ten bytes")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid byte string: Ten bytes"); + .hasMessage("Invalid byte string: {byteCount=Ten bytes}"); } @Test diff --git a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java index 3b09bee0..170f0889 100644 --- a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java +++ b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java @@ -69,7 +69,7 @@ public void testParseDays() { public void testInvalidPattern() { assertThatThrownBy(() -> HumanReadableDuration.valueOf("One hour")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid duration: One hour"); + .hasMessage("Invalid duration: {duration=One hour}"); } @Test From c61ea5c842977eb73a2afcbbc08b2a4fb9144d68 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 6 May 2020 23:50:18 +0100 Subject: [PATCH 5/6] exceptions --- .../HumanReadableDuration.java | 25 ++++++++++--------- .../HumanReadableDurationTests.java | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java index aee6fb33..01cb6640 100644 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java +++ b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java @@ -22,9 +22,9 @@ /** * A human-readable type for durations. *

- * This class allows for parsing strings representing durations into usable time quantities. Strings should match - * {@link HumanReadableDuration#DURATION_PATTERN} which represents the numeric duration value with a suffix representing - * the {@link TimeUnit} to use for this duration. Suffixes may be pluralized or not regardless of the actual numeric + * This class allows for parsing strings representing durations into usable time quantities. Strings should match {@link + * HumanReadableDuration#DURATION_PATTERN} which represents the numeric duration value with a suffix representing the + * {@link TimeUnit} to use for this duration. Suffixes may be pluralized or not regardless of the actual numeric * quantity. *

* All {@code equal}, {@code compareTo} and {@code hashCode} implementations assume normalized values, i.e. they work @@ -140,7 +140,7 @@ public static HumanReadableDuration days(long count) { * * @param duration the string HumanReadableDuration of this duration * @return the parsed {@link HumanReadableDuration} - * @throws IllegalArgumentException if the provided duration is invalid + * @throws SafeIllegalArgumentException if the provided duration is invalid */ @JsonCreator public static HumanReadableDuration valueOf(String duration) { @@ -150,7 +150,9 @@ public static HumanReadableDuration valueOf(String duration) { final long count = Long.parseLong(matcher.group(1)); final TimeUnit unit = SUFFIXES.get(matcher.group(2)); if (unit == null) { - throw new IllegalArgumentException("Invalid duration: " + duration + ". Wrong time unit"); + throw new SafeIllegalArgumentException( + "Invalid duration. Wrong time unit", + SafeArg.of("duration", duration)); } return new HumanReadableDuration(count, unit); @@ -236,10 +238,10 @@ public Duration toJavaDuration() { *

* This handles the seven units declared in {@code TimeUnit}. * - * @implNote This method can be removed in JDK9 - * @see JDK-8141452 * @param unit the unit to convert, not null * @return the converted unit, not null + * @implNote This method can be removed in JDK9 + * @see JDK-8141452 */ private static ChronoUnit chronoUnit(TimeUnit unit) { Preconditions.checkNotNull(unit, "unit"); @@ -265,10 +267,10 @@ private static ChronoUnit chronoUnit(TimeUnit unit) { /** * Compares this duration to the specified {@code HumanReadableDuration}. *

- * The comparison is based on the total length of the durations. - * It is "consistent with equals", as defined by {@link Comparable}. + * The comparison is based on the total length of the durations. It is "consistent with equals", as defined by + * {@link Comparable}. * - * @param otherDuration the other duration to compare to, not null + * @param otherDuration the other duration to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override @@ -285,7 +287,7 @@ public int compareTo(HumanReadableDuration otherDuration) { *

* The comparison is based on the total length of the durations. * - * @param otherDuration the other duration, null returns false + * @param otherDuration the other duration, null returns false * @return true if the other duration is equal to this one */ @Override @@ -301,7 +303,6 @@ public boolean equals(Object otherDuration) { return count == duration.count; } return toJavaDuration().equals(duration.toJavaDuration()); - } /** diff --git a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java index 170f0889..883a9e46 100644 --- a/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java +++ b/human-readable-types/src/test/java/com/palantir/humanreadabletypes/HumanReadableDurationTests.java @@ -76,7 +76,7 @@ public void testInvalidPattern() { public void testInvalidUnits() { assertThatThrownBy(() -> HumanReadableDuration.valueOf("10 weeks")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid duration: 10 weeks. Wrong time unit"); + .hasMessage("Invalid duration. Wrong time unit: {duration=10 weeks}"); } @Test From 97f20fed51d16dc38e82ba0e77de89b8a64d2a59 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Wed, 6 May 2020 23:51:17 +0100 Subject: [PATCH 6/6] types --- .../HumanReadableByteCount.java | 11 ++++++----- .../HumanReadableDuration.java | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java index 470081a0..7b3c2bb1 100644 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java +++ b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java @@ -138,7 +138,7 @@ public static HumanReadableByteCount pebibytes(long size) { * @param byteCount the string representation of this byte string * @return the parsed {@link HumanReadableByteCount} * @throws IllegalArgumentException if the provided byte string is invalid - * @throws NumberFormatException if the provided size cannot be parsed + * @throws NumberFormatException if the provided size cannot be parsed */ @JsonCreator public static HumanReadableByteCount valueOf(String byteCount) { @@ -157,6 +157,7 @@ public static HumanReadableByteCount valueOf(String byteCount) { } return new HumanReadableByteCount(size, suffix != null ? SUFFIXES.get(suffix) : ByteUnit.BYTE); + } catch (NumberFormatException e) { String byteError = "Size must be specified as bytes (b), " + "kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). " @@ -195,10 +196,10 @@ public long toBytes() { /** * Compares this byte string to the specified {@code HumanReadableByteCount}. *

- * The comparison is based on the total number of bytes. It is "consistent with equals", as defined by {@link - * Comparable}. + * The comparison is based on the total number of bytes. + * It is "consistent with equals", as defined by {@link Comparable}. * - * @param otherByteCount the other byte string to compare to, not null + * @param otherByteCount the other byte string to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override @@ -214,7 +215,7 @@ public int compareTo(HumanReadableByteCount otherByteCount) { *

* The comparison is based on the total number of bytes. * - * @param otherByteCount the other byte string, null returns false + * @param otherByteCount the other byte string, null returns false * @return true if the other byte string is equal to this one */ @Override diff --git a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java index 01cb6640..1d8045c3 100644 --- a/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java +++ b/human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java @@ -22,9 +22,9 @@ /** * A human-readable type for durations. *

- * This class allows for parsing strings representing durations into usable time quantities. Strings should match {@link - * HumanReadableDuration#DURATION_PATTERN} which represents the numeric duration value with a suffix representing the - * {@link TimeUnit} to use for this duration. Suffixes may be pluralized or not regardless of the actual numeric + * This class allows for parsing strings representing durations into usable time quantities. Strings should match + * {@link HumanReadableDuration#DURATION_PATTERN} which represents the numeric duration value with a suffix representing + * the {@link TimeUnit} to use for this duration. Suffixes may be pluralized or not regardless of the actual numeric * quantity. *

* All {@code equal}, {@code compareTo} and {@code hashCode} implementations assume normalized values, i.e. they work @@ -238,10 +238,10 @@ public Duration toJavaDuration() { *

* This handles the seven units declared in {@code TimeUnit}. * - * @param unit the unit to convert, not null - * @return the converted unit, not null * @implNote This method can be removed in JDK9 * @see JDK-8141452 + * @param unit the unit to convert, not null + * @return the converted unit, not null */ private static ChronoUnit chronoUnit(TimeUnit unit) { Preconditions.checkNotNull(unit, "unit"); @@ -267,10 +267,10 @@ private static ChronoUnit chronoUnit(TimeUnit unit) { /** * Compares this duration to the specified {@code HumanReadableDuration}. *

- * The comparison is based on the total length of the durations. It is "consistent with equals", as defined by - * {@link Comparable}. + * The comparison is based on the total length of the durations. + * It is "consistent with equals", as defined by {@link Comparable}. * - * @param otherDuration the other duration to compare to, not null + * @param otherDuration the other duration to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override @@ -287,7 +287,7 @@ public int compareTo(HumanReadableDuration otherDuration) { *

* The comparison is based on the total length of the durations. * - * @param otherDuration the other duration, null returns false + * @param otherDuration the other duration, null returns false * @return true if the other duration is equal to this one */ @Override @@ -303,6 +303,7 @@ public boolean equals(Object otherDuration) { return count == duration.count; } return toJavaDuration().equals(duration.toJavaDuration()); + } /**