mbien commented on code in PR #9545:
URL: https://github.com/apache/netbeans/pull/9545#discussion_r3738821387


##########
java/java.freeform/src/org/netbeans/modules/java/freeform/Classpaths.java:
##########
@@ -104,6 +109,11 @@ final class Classpaths implements ClassPathProvider, 
AntProjectListener, Propert
     
     private static final ErrorManager err = 
ErrorManager.getDefault().getInstance(Classpaths.class.getName());
 
+    /** Recomputes wildcard classpaths off the file-event thread; see {@code 
wildcardListener}. */

Review Comment:
   you can use `///` comments for javadoc one-liners if you want. The formatter 
would break the line otherwise. Modules can be bumped to JDK 21 level.



##########
java/java.freeform/test/unit/src/org/netbeans/modules/java/freeform/ClasspathsTest.java:
##########
@@ -548,4 +551,114 @@ public void propertyChange(PropertyChangeEvent e) {
         assertTrue(cpe2.includes("whatever/"));
     }
 
+    /** A "dir/*.jar" entry expands to every matching JAR in the directory 
(#116185). */
+    public void testWildcardClasspath() throws Exception {
+        clearWorkDir();
+        File d = getWorkDir();
+        AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, 
"prj", null);
+        Project p = 
ProjectManager.getDefault().findProject(helper.getProjectDirectory());
+        FileObject src = FileUtil.createFolder(new File(d, "src"));
+        File libDir = new File(d, "lib");
+        assertTrue("created lib dir", libDir.mkdirs());
+        File jarA = createJar(new File(libDir, "a.jar"));
+        File jarB = createJar(new File(libDir, "b.jar"));
+        createJar(new File(libDir, "readme.zip")); // does not match *.jar
+        configureCompileClasspath(helper, p, "src", "lib/*.jar");
+
+        ClassPath cp = ClassPath.getClassPath(src, ClassPath.COMPILE);
+        assertNotNull("have COMPILE classpath for src/", cp);
+        SortedSet<String> expected = new TreeSet<String>();
+        expected.add(jarRoot(jarA));
+        expected.add(jarRoot(jarB));
+        assertEquals("wildcard expanded to the matching jars", expected, 
urlsOfCp(cp));
+    }
+
+    /** A bare "dir/*" entry expands to the archives only, skipping other 
files. */
+    public void testWildcardClasspathIncludesOnlyArchives() throws Exception {
+        clearWorkDir();
+        File d = getWorkDir();
+        AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, 
"prj", null);
+        Project p = 
ProjectManager.getDefault().findProject(helper.getProjectDirectory());
+        FileObject src = FileUtil.createFolder(new File(d, "src"));
+        File libDir = new File(d, "lib");
+        assertTrue("created lib dir", libDir.mkdirs());
+        File jarA = createJar(new File(libDir, "a.jar"));
+        writeText(new File(libDir, "notes.txt"), "not an archive"); // must be 
skipped
+        configureCompileClasspath(helper, p, "src", "lib/*");
+
+        ClassPath cp = ClassPath.getClassPath(src, ClassPath.COMPILE);
+        assertNotNull("have COMPILE classpath for src/", cp);
+        assertEquals("only the archive was included",
+                Collections.singleton(jarRoot(jarA)), urlsOfCp(cp));
+    }
+
+    /** JARs produced after the project is opened are picked up without 
editing project.xml. */
+    @RandomlyFails // depends on asynchronous filesystem-event delivery + 
coalescing

Review Comment:
   how bad is it? `@RandomlyFails` is discouraged for new tests since CI won't 
run it - and if it doesn't run it will age.



##########
java/java.freeform/src/org/netbeans/modules/java/freeform/Classpaths.java:
##########
@@ -430,26 +451,90 @@ private List<URL> createClasspath(
         final String[] path = PropertyUtils.tokenizePath(cpEval);
         final List<URL> res = new ArrayList<>();
         for (String pathElement : path) {
-            res.addAll(translate.apply(createClasspathEntry(pathElement)));
+            for (URL entry : createClasspathEntries(pathElement, watchedDirs)) 
{
+                res.addAll(translate.apply(entry));
+            }
         }
         return res;
     }
-    
+
+    /**
+     * Turn a single (already property-evaluated) classpath token into zero or
+     * more classpath root URLs. Ordinary tokens map to exactly one URL; a 
token
+     * whose last path component is a filename glob is expanded to the matching
+     * archives in the directory it names.
+     */
+    private List<URL> createClasspathEntries(String text, Set<File> 
watchedDirs) {
+        final int slash = Math.max(text.lastIndexOf('/'), 
text.lastIndexOf(File.separatorChar));
+        final String lastComponent = slash >= 0 ? text.substring(slash + 1) : 
text;
+        if (lastComponent.indexOf('*') < 0 && lastComponent.indexOf('?') < 0) {
+            return Collections.singletonList(createClasspathEntry(text));
+        }
+        final String prefix = slash >= 0 ? text.substring(0, slash) : ""; // 
NOI18N
+        final File dir = helper.resolveFile(prefix.isEmpty() ? "." : prefix); 
// NOI18N
+        if (watchedDirs != null) {
+            // Watch the directory even if it does not exist yet: a build may
+            // create it (and the matching JARs) after the project is opened.
+            watchedDirs.add(dir);
+        }
+        final File[] kids = dir.listFiles();
+        if (kids == null) {
+            return Collections.emptyList();
+        }
+        // Sort for a stable classpath order independent of directory listing 
order.
+        Arrays.sort(kids);
+        final Pattern pattern = wildcardToRegex(lastComponent);
+        final List<URL> res = new ArrayList<>();
+        for (File kid : kids) {
+            if (!kid.isFile() || !pattern.matcher(kid.getName()).matches()) {
+                continue;
+            }
+            final URL entry = FileUtil.urlForArchiveOrDir(kid);
+            // Only archives yield a URL ending in '/'; skip anything that is 
not
+            // a valid classpath root (matches the java launcher's JARs-only 
rule).
+            if (entry != null && entry.toExternalForm().endsWith("/")) { // 
NOI18N

Review Comment:
   `FileUtil.isArchiveFile(url)` doesn't work here?



##########
java/java.freeform/src/org/netbeans/modules/java/freeform/Classpaths.java:
##########
@@ -430,26 +451,90 @@ private List<URL> createClasspath(
         final String[] path = PropertyUtils.tokenizePath(cpEval);
         final List<URL> res = new ArrayList<>();
         for (String pathElement : path) {
-            res.addAll(translate.apply(createClasspathEntry(pathElement)));
+            for (URL entry : createClasspathEntries(pathElement, watchedDirs)) 
{
+                res.addAll(translate.apply(entry));
+            }
         }
         return res;
     }
-    
+
+    /**
+     * Turn a single (already property-evaluated) classpath token into zero or
+     * more classpath root URLs. Ordinary tokens map to exactly one URL; a 
token
+     * whose last path component is a filename glob is expanded to the matching
+     * archives in the directory it names.
+     */
+    private List<URL> createClasspathEntries(String text, Set<File> 
watchedDirs) {
+        final int slash = Math.max(text.lastIndexOf('/'), 
text.lastIndexOf(File.separatorChar));
+        final String lastComponent = slash >= 0 ? text.substring(slash + 1) : 
text;
+        if (lastComponent.indexOf('*') < 0 && lastComponent.indexOf('?') < 0) {
+            return Collections.singletonList(createClasspathEntry(text));
+        }
+        final String prefix = slash >= 0 ? text.substring(0, slash) : ""; // 
NOI18N
+        final File dir = helper.resolveFile(prefix.isEmpty() ? "." : prefix); 
// NOI18N
+        if (watchedDirs != null) {
+            // Watch the directory even if it does not exist yet: a build may
+            // create it (and the matching JARs) after the project is opened.
+            watchedDirs.add(dir);
+        }
+        final File[] kids = dir.listFiles();
+        if (kids == null) {
+            return Collections.emptyList();
+        }
+        // Sort for a stable classpath order independent of directory listing 
order.
+        Arrays.sort(kids);
+        final Pattern pattern = wildcardToRegex(lastComponent);
+        final List<URL> res = new ArrayList<>();
+        for (File kid : kids) {
+            if (!kid.isFile() || !pattern.matcher(kid.getName()).matches()) {
+                continue;
+            }
+            final URL entry = FileUtil.urlForArchiveOrDir(kid);
+            // Only archives yield a URL ending in '/'; skip anything that is 
not
+            // a valid classpath root (matches the java launcher's JARs-only 
rule).
+            if (entry != null && entry.toExternalForm().endsWith("/")) { // 
NOI18N
+                res.add(entry);
+            }
+        }
+        return res;
+    }
+
+    /**
+     * Translate a filename glob (<code>*</code> matches any run of characters,
+     * <code>?</code> matches a single character) into a case-insensitive 
regex.
+     */
+    private static Pattern wildcardToRegex(String glob) {
+        final StringBuilder sb = new StringBuilder(glob.length() + 8);
+        for (int i = 0; i < glob.length(); i++) {
+            final char c = glob.charAt(i);
+            switch (c) {
+                case '*': sb.append(".*"); break; // NOI18N
+                case '?': sb.append('.'); break;
+                default:
+                    if ("\\.[]{}()+-^$|".indexOf(c) >= 0) { // NOI18N
+                        sb.append('\\');
+                    }
+                    sb.append(c);
+            }

Review Comment:
   if you want you could set `javac.release=21` and use the arrow syntax.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to