Skip to content

Commit 7d924a8

Browse files
pditommasopaulk-asert
authored andcommitted
GROOVY-11996: Provide a groovy.truth.file.exists.enabled system property which when false reverts to Groovy 4 behavior
Restore the pre-Groovy 5 truthy semantics for `java.io.File` and `java.nio.file.Path` when the system property `-Dgroovy.io.fileLegacyTruthy=true` is set: any non-null reference is truthy, regardless of whether the underlying file exists. The default behavior introduced in 5.0 (delegating to `File.exists()` / `Files.exists()`) is preserved when the property is absent or false.
1 parent f97ae1b commit 7d924a8

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import groovy.transform.stc.PickFirstResolver;
3232
import groovy.transform.stc.SimpleType;
3333
import groovy.util.CharsetToolkit;
34+
import org.apache.groovy.util.SystemUtil;
3435
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
3536

3637
import java.io.BufferedInputStream;
@@ -153,15 +154,30 @@ public void doCall(Object[] args) {
153154

154155
/**
155156
* Coerce the file to a {@code boolean} value.
157+
* <p>
158+
* By default this returns whether the file exists. Set the system property
159+
* {@code groovy.truth.file.exists.enabled} to {@code false} to restore the
160+
* pre-Groovy-5 behavior where any non-{@code null} {@code File} is truthy
161+
* regardless of whether the underlying file exists.
156162
*
157163
* @param file a {@code File}
158164
* @return {@code true} if the file exists, {@code false} otherwise
159165
* @since 5.0.0
160166
*/
161167
public static boolean asBoolean(final File file) {
168+
if (!FILE_EXISTS_ENABLED) return file != null;
162169
return file.exists();
163170
}
164171

172+
/**
173+
* When {@code true} (the default), coercing a {@link File} or {@link java.nio.file.Path}
174+
* to a {@code boolean} returns whether the underlying file exists. Set the system property
175+
* {@code groovy.truth.file.exists.enabled} to {@code false} to restore the pre-Groovy-5
176+
* behavior where any non-{@code null} reference is truthy.
177+
*/
178+
public static final boolean FILE_EXISTS_ENABLED = Boolean.parseBoolean(
179+
SystemUtil.getSystemPropertySafe("groovy.truth.file.exists.enabled", "true"));
180+
165181
/**
166182
* Create an object output stream for this file.
167183
*

subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.codehaus.groovy.runtime.FormatHelper;
3636
import org.codehaus.groovy.runtime.IOGroovyMethods;
3737
import org.codehaus.groovy.runtime.InvokerHelper;
38+
import org.codehaus.groovy.runtime.ResourceGroovyMethods;
3839
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
3940

4041
import java.io.BufferedInputStream;
@@ -147,13 +148,19 @@ public static String getBaseName(Path self) {
147148

148149
/**
149150
* Coerce the path to a {@code boolean} value.
151+
* <p>
152+
* By default this returns whether the file at the path exists. Set the
153+
* system property {@code groovy.truth.file.exists.enabled} to {@code false}
154+
* to restore the pre-Groovy-5 behavior where any non-{@code null} {@code Path}
155+
* is truthy regardless of whether the underlying file exists.
150156
*
151157
* @param path a {@code Path} object
152158
* @return {@code true} if the file at the path exists, {@code false} otherwise
153159
* @see Files#exists(Path, LinkOption...)
154160
* @since 5.0.0
155161
*/
156162
public static boolean asBoolean(final Path path) {
163+
if (!ResourceGroovyMethods.FILE_EXISTS_ENABLED) return path != null;
157164
return Files.exists(path);
158165
}
159166

0 commit comments

Comments
 (0)