From 07a57ff66f3fa0ffd8abea94e7c0f6ac4f025b5f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 26 Jan 2023 12:18:26 -0800 Subject: [PATCH] Rename the sentinel and move it to `Formatter`. --- lib/src/main/java/com/diffplug/spotless/Formatter.java | 5 ++++- lib/src/main/java/com/diffplug/spotless/FormatterFunc.java | 6 +++--- .../main/java/com/diffplug/spotless/FormatterStepImpl.java | 5 +---- .../src/main/java/com/diffplug/spotless/StepHarness.java | 2 +- .../src/test/java/com/diffplug/spotless/FormatterTest.java | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index 035452e29c..a55e8e6504 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -237,7 +237,7 @@ public String compute(String unix, File file) { unix = LineEnding.toUnix(formatted); } } catch (Throwable e) { - if (file == FormatterStepImpl.SENTINEL) { + if (file == NO_FILE_SENTINEL) { exceptionPolicy.handleError(e, step, ""); } else { // Path may be forged from a different FileSystem than Filesystem.default @@ -289,4 +289,7 @@ public void close() { } } } + + /** This Sentinel reference may be used to Formatter requires a File, while there is no actual File to format */ + public static final File NO_FILE_SENTINEL = new File("NO_FILE_SENTINEL"); } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 48a8e810ee..14b407a2eb 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -127,7 +127,7 @@ public String apply(String unix, File file) throws Exception { @Override public String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.NO_FILE_SENTINEL); } }; } @@ -156,7 +156,7 @@ default String apply(String unix, File file) throws Exception { @Override default String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.NO_FILE_SENTINEL); } } } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 3b1b5cf278..17b62e75c6 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -116,11 +116,8 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti } } - /**This Sentinel reference may be used where Formatter requires a File, while there is no actual File to format */ - public static final File SENTINEL = new File(""); - static void checkNotSentinel(File file) { - if (file == SENTINEL) { + if (file == Formatter.NO_FILE_SENTINEL) { throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile"); } } diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index 71e2a663b5..c611cc738a 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -88,7 +88,7 @@ public AbstractStringAssert testResourceExceptionMsg(String resourceBefore) { public AbstractStringAssert testExceptionMsg(String before) { try { - formatter.compute(LineEnding.toUnix(before), FormatterStepImpl.SENTINEL); + formatter.compute(LineEnding.toUnix(before), Formatter.NO_FILE_SENTINEL); throw new SecurityException("Expected exception"); } catch (Throwable e) { if (e instanceof SecurityException) { diff --git a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java index 76f11b3d15..314507bd8d 100644 --- a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java @@ -142,7 +142,7 @@ public void testExceptionWithSentinelNoFileOnDisk() throws Exception { .exceptionPolicy(exceptionPolicy) .build(); - formatter.compute("someFileContent", FormatterStepImpl.SENTINEL); + formatter.compute("someFileContent", Formatter.NO_FILE_SENTINEL); } // rootDir may be a path not from the default FileSystem