This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 7d924a8048ec5b27c0da95bb8e3f3907995ad44f
Author: Paolo Di Tommaso <[email protected]>
AuthorDate: Mon May 4 17:32:06 2026 +0200

    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.
---
 .../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 d046ebd57a..f367e32f58 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.typehandling.DefaultTypeTransformation;
 
 import java.io.BufferedInputStream;
@@ -153,15 +154,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 39d6efbf06..589b1acc02 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
@@ -35,6 +35,7 @@ import 
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport;
 import org.codehaus.groovy.runtime.FormatHelper;
 import org.codehaus.groovy.runtime.IOGroovyMethods;
 import org.codehaus.groovy.runtime.InvokerHelper;
+import org.codehaus.groovy.runtime.ResourceGroovyMethods;
 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 import java.io.BufferedInputStream;
@@ -147,6 +148,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
@@ -154,6 +160,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);
     }
 

Reply via email to