This is an automated email from the ASF dual-hosted git repository.
paulk-asert pushed a commit to branch GROOVY_5_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_5_0_X by this push:
new a1c006c974 GROOVY-11996: Provide a groovy.truth.file.exists.enabled
system property which when false reverts to Groovy 4 behavior
a1c006c974 is described below
commit a1c006c974a3aca7f3d8c37c607535849e388580
Author: Paul King <[email protected]>
AuthorDate: Wed May 6 11:16:59 2026 +1000
GROOVY-11996: Provide a groovy.truth.file.exists.enabled system property
which when false reverts to Groovy 4 behavior
---
.../codehaus/groovy/runtime/ResourceGroovyMethods.java | 16 ++++++++++++++++
.../org/apache/groovy/nio/extensions/NioExtensions.java | 7 +++++++
2 files changed, 23 insertions(+)
diff --git
a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
index bf5c07858b..0e1db2f8ee 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
@@ -31,6 +31,7 @@ import groovy.transform.stc.FromString;
import groovy.transform.stc.PickFirstResolver;
import groovy.transform.stc.SimpleType;
import groovy.util.CharsetToolkit;
+import org.apache.groovy.util.SystemUtil;
import org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
@@ -152,15 +153,30 @@ public class ResourceGroovyMethods extends
DefaultGroovyMethodsSupport {
/**
* Coerce the file to a {@code boolean} value.
+ * <p>
+ * By default this returns whether the file exists. Set the system property
+ * {@code groovy.truth.file.exists.enabled} to {@code false} to restore the
+ * pre-Groovy-5 behavior where any non-{@code null} {@code File} is truthy
+ * regardless of whether the underlying file exists.
*
* @param file a {@code File}
* @return {@code true} if the file exists, {@code false} otherwise
* @since 5.0.0
*/
public static boolean asBoolean(final File file) {
+ if (!FILE_EXISTS_ENABLED) return file != null;
return file.exists();
}
+ /**
+ * When {@code true} (the default), coercing a {@link File} or {@link
java.nio.file.Path}
+ * to a {@code boolean} returns whether the underlying file exists. Set
the system property
+ * {@code groovy.truth.file.exists.enabled} to {@code false} to restore
the pre-Groovy-5
+ * behavior where any non-{@code null} reference is truthy.
+ */
+ public static final boolean FILE_EXISTS_ENABLED = Boolean.parseBoolean(
+
SystemUtil.getSystemPropertySafe("groovy.truth.file.exists.enabled", "true"));
+
/**
* Create an object output stream for this file.
*
diff --git
a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
index 90f84b5927..07f27de53e 100644
---
a/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
+++
b/subprojects/groovy-nio/src/main/java/org/apache/groovy/nio/extensions/NioExtensions.java
@@ -36,6 +36,7 @@ import org.codehaus.groovy.runtime.FormatHelper;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker;
+import org.codehaus.groovy.runtime.ResourceGroovyMethods;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import java.io.BufferedInputStream;
@@ -143,6 +144,11 @@ public class NioExtensions extends
DefaultGroovyMethodsSupport {
/**
* Coerce the path to a {@code boolean} value.
+ * <p>
+ * By default this returns whether the file at the path exists. Set the
+ * system property {@code groovy.truth.file.exists.enabled} to {@code
false}
+ * to restore the pre-Groovy-5 behavior where any non-{@code null} {@code
Path}
+ * is truthy regardless of whether the underlying file exists.
*
* @param path a {@code Path} object
* @return {@code true} if the file at the path exists, {@code false}
otherwise
@@ -150,6 +156,7 @@ public class NioExtensions extends
DefaultGroovyMethodsSupport {
* @since 5.0.0
*/
public static boolean asBoolean(final Path path) {
+ if (!ResourceGroovyMethods.FILE_EXISTS_ENABLED) return path != null;
return Files.exists(path);
}