This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
It is quite common in project system code to have a java.io.File which represents either an archive or a folder (a classpath entry) and wish to get a URL representing this entry. You can use existing FileUtil methods, but it is slightly tricky; you need to check a few special conditions. This code is duplicated all over the place. Just grep the code base for File.toURI and you will find plenty of examples. See e.g. apisupport/project/src/org/netbeans/modules/apisupport/project/Util.java and the urlFor* methods. Suggest new static convenience methods to do these things right, in one place. Would mean going thru apireviews, adding the methods, and doing a search over the whole code base for usages of these idioms that could be replaced with the standard method. There is sometimes the reverse operation: getting a File from a URL which might be a file: or jar: URL. The thing which people writing this code usually forget is to check for a jar: URL whether there is anything after "!/"; if so, you generally *don't* want to convert it back to a *.jar File (because you will be losing the suffix). Might also want methods to convert classpaths between URL[] form (as used in ClassPath) and string form (as e.g. "/path/dir:/path/archive.jar"). I think this kind of operation is done in a few more places. Not sure where all this belongs. FileUtil? PropertyUtils in ant/project? ClassPathSupport in java/api?
I vote for the FileUtil, there are already the getArchiveRoot and the getArchiveFile methods.
Re. "convert classpaths between URL[] form and string form" - and/or convert between File (as used in -classpath) and URL. Examples: File("/tmp/dir") -> file:/tmp/dir/ File("/tmp/archive.jar") -> jar:file:/tmp/archive.jar!/ file:/tmp/dir/ -> File("/tmp/dir") jar:file:/tmp/archive.jar!/ -> File("/tmp/archive.jar") and the ones that people usually forget to check for, resulting in the occasional error: File("/tmp/nonexistent-dir") -> file:/tmp/nonexistent-dir/ [need final '/'] jar:file:/tmp/archive.jar -> null [missing JAR path; or assume "!/" anyway?] jar:file:/tmp/archive.jar!/subpath/ -> null [not a root] jar:jar:file:/tmp/archive.jar!/subjar.jar!/ -> null [nested not permitted] http://nowhere.net/ -> null [remote] jar:http://nowhere.net/archive.jar!/ -> null [remote] Converting String -> URL[] would require PropertyUtils.tokenizePath or its equivalent to correctly handle ':' as a drive letter marker on Windows rather than a path separator (assuming the method permitted either ':' or ';' on any platform, as Ant does).
There should be probably a method on the ClassPathSupport creating the classpath from the string: ClassPathSupport.fromJVMPath (String path); and a method on the ClassPath creating the string representation of the classpath: ClassPath.toJVMPath()
This is important. People don't know how to do it right and wind up doing it wrong. And similar but slightly different code is scattered all over the place. For the ClassPath -> String conversion I would suggest a mode parameter which would control behavior on untranslatable entries (e.g. jar:file:/tmp/src.zip!/src/): enum Mode { SKIP - such entries are silently dropped WARN - a log warning is issued FAIL - an IAE is thrown PRINT - the URL.toString is included (useful for debugging and unit tests) }
I am working on a patch for this, will submit soon.
Created attachment 58104 [details] Proposed patch
Please review. Patch contains API changes, and usage from modules I suspected needed the new APIs.
Both API (filesystems & java api) seems good.
+1 from me, api looks fine.
Also used it in java.freeform. #4c8b2e081042