You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/01/05 11:11:23 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-9878: GroovyClassLoader fileReallyExists super inefficient (closes #1454)

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new ca7e269  GROOVY-9878: GroovyClassLoader fileReallyExists super inefficient (closes #1454)
ca7e269 is described below

commit ca7e269af6b51c7d4ae216902f0ff1ef889d3b04
Author: David Estes <da...@gmail.com>
AuthorDate: Mon Jan 4 10:47:31 2021 -0500

    GROOVY-9878: GroovyClassLoader fileReallyExists super inefficient (closes #1454)
    
    fileReallyExists was doing a case sensitive test by listing all files in the directory. this is heavy on the filesystem API and can be done with simply using getCanonicalPath comparison.
---
 src/main/java/groovy/lang/GroovyClassLoader.java | 29 ++++++++++++++++--------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java b/src/main/java/groovy/lang/GroovyClassLoader.java
index 8cc7d6e..ccdb562 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -1031,22 +1031,31 @@ public class GroovyClassLoader extends URLClassLoader {
         try {
             /* fix for GROOVY-5809 */
             path = new File(ret.toURI());
-        } catch(URISyntaxException e) {
+        } catch (URISyntaxException e) {
             path = new File(decodeFileName(ret.getFile()));
         }
         path = path.getParentFile();
-        if (path.exists() && path.isDirectory()) {
-            File file = new File(path, fileWithoutPackage);
-            if (file.exists()) {
-                // file.exists() might be case insensitive. Let's do
-                // case sensitive match for the filename
-                File parent = file.getParentFile();
-                for (String child : parent.list()) {
-                    if (child.equals(fileWithoutPackage)) return file;
+
+        File file = new File(path, fileWithoutPackage);
+        if (file.exists()) {
+            // file.exists() might be case insensitive.
+            // Let's do case sensitive match for the filename
+            try {
+                String caseSensitiveName = file.getCanonicalPath();
+                int index = caseSensitiveName.lastIndexOf(File.separator);
+                if (index != -1) {
+                    caseSensitiveName = caseSensitiveName.substring(index + 1);
+                }
+                if (fileWithoutPackage.equals(caseSensitiveName)) {
+                    return file;
                 }
+            } catch (IOException ignore) {
+                // assume doesn't really exist if we can't read the file
             }
+
         }
-        //file does not exist!
+
+        // file does not exist!
         return null;
     }