support loading FlatLaf properties files from named Java modules without the need to open that package in module-info.java (issue #1026)

This commit is contained in:
Karl Tauber
2025-09-08 13:03:17 +02:00
parent 6f63982054
commit 2ac7234c32
9 changed files with 96 additions and 19 deletions

View File

@@ -911,8 +911,7 @@ public abstract class FlatLaf
* <p>
* Invoke this method before setting the look and feel.
* <p>
* If using Java modules, the package must be opened in {@code module-info.java}.
* Otherwise, use {@link #registerCustomDefaultsSource(URL)}.
* If using Java modules, it is not necessary to open the package in {@code module-info.java}.
*
* @param packageName a package name (e.g. "com.myapp.resources")
*/
@@ -959,9 +958,9 @@ public abstract class FlatLaf
* <p>
* See {@link #registerCustomDefaultsSource(String)} for details.
* <p>
* This method is useful if using Java modules and the package containing the properties files
* is not opened in {@code module-info.java}.
* E.g. {@code FlatLaf.registerCustomDefaultsSource( MyApp.class.getResource( "/com/myapp/themes/" ) )}.
* <p>
* If using Java modules, it is not necessary to open the package in {@code module-info.java}.
*
* @param packageUrl a package URL
* @since 2

View File

@@ -205,20 +205,37 @@ class UIDefaultsLoader
if( classLoader != null && !addonClassLoaders.contains( classLoader ) )
addonClassLoaders.add( classLoader );
packageName = packageName.replace( '.', '/' );
if( classLoader == null )
classLoader = FlatLaf.class.getClassLoader();
// Get package URL using ClassLoader.getResource(...) because this works
// also in named Java modules, even without opening the package in module-info.java.
// This extra step is necessary because ClassLoader.getResource("<package>/<file>.properties")
// does not work for named Java modules.
URL url = classLoader.getResource( packageName.replace( '.', '/' ) );
if( url == null ) {
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to find package '"
+ packageName + "' to load properties files.", null );
continue;
}
String packageUrl = url.toExternalForm();
if( !packageUrl.endsWith( "/" ) )
packageUrl = packageUrl.concat( "/" );
for( Class<?> lafClass : lafClasses ) {
String propertiesName = packageName + '/' + simpleClassName( lafClass ) + ".properties";
try( InputStream in = classLoader.getResourceAsStream( propertiesName ) ) {
if( in != null )
properties.load( in );
URL propertiesUrl = new URL( packageUrl + simpleClassName( lafClass ) + ".properties" );
try( InputStream in = propertiesUrl.openStream() ) {
properties.load( in );
} catch( FileNotFoundException ex ) {
// ignore
}
}
} else if( source instanceof URL ) {
// load from package URL
URL packageUrl = (URL) source;
String packageUrl = ((URL)source).toExternalForm();
if( !packageUrl.endsWith( "/" ) )
packageUrl = packageUrl.concat( "/" );
for( Class<?> lafClass : lafClasses ) {
URL propertiesUrl = new URL( packageUrl + simpleClassName( lafClass ) + ".properties" );