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' } } 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..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 @@ -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; @@ -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); @@ -274,7 +276,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..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 @@ -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; @@ -138,17 +140,19 @@ 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) { 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)); 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); @@ -240,7 +244,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 +261,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/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..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 @@ -69,14 +69,14 @@ 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 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 diff --git a/versions.props b/versions.props index 21dc92f7..a004dcd7 100644 --- a/versions.props +++ b/versions.props @@ -1,7 +1,8 @@ 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 +com.palantir.safe-logging:* = 1.13.0 junit:junit = 4.13 org.assertj:* = 3.16.0 org.checkerframework:checker-qual = 2.5.3