Skip to content

Commit

Permalink
Use @FormatString and @FormatMethod annotations.
Browse files Browse the repository at this point in the history
This introduces a compile-time-only dependency on errorprone
annotations.

Also fixes a bug identified by errorprone when these annotations were
added.
  • Loading branch information
sjamesr committed Dec 11, 2020
1 parent ef0abec commit 296b011
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -29,6 +29,8 @@ dependencies {
testCompile 'junit:junit:4.12'
testCompile 'com.google.truth:truth:0.24'

compile 'com.google.errorprone:error_prone_annotations:2.4.0'

// Cobertura requires slf4j at runtime
testRuntime 'org.slf4j:slf4j-api:1.7.10'

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/au/com/southsky/jfreesane/Preconditions.java
@@ -1,9 +1,13 @@
package au.com.southsky.jfreesane;

import com.google.errorprone.annotations.FormatMethod;
import com.google.errorprone.annotations.FormatString;

final class Preconditions {
private Preconditions() {}

static void checkState(boolean state, String message, Object... args) {
@FormatMethod
static void checkState(boolean state, @FormatString String message, Object... args) {
if (!state) {
throw new IllegalStateException(String.format(message, args));
}
Expand All @@ -27,7 +31,8 @@ static void checkArgument(boolean arg) {
}
}

static void checkArgument(boolean arg, String message, Object... args) {
@FormatMethod
static void checkArgument(boolean arg, @FormatString String message, Object... args) {
if (!arg) {
throw new IllegalArgumentException(String.format(message, args));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/au/com/southsky/jfreesane/SaneOption.java
Expand Up @@ -491,7 +491,7 @@ public void setButtonValue() throws IOException, SaneException {
*/
public double setFixedValue(double value) throws IOException, SaneException {
Preconditions.checkArgument(
value >= -32768 && value <= 32767.9999, "value %d is out of range", value);
value >= -32768 && value <= 32767.9999, "value %f is out of range", value);
SaneWord wordValue = SaneWord.forFixedPrecision(value);
ControlOptionResult result = writeOption(wordValue);
Preconditions.checkState(
Expand Down

0 comments on commit 296b011

Please sign in to comment.