Make the module dependency on java.logging optional

Currently, FlatLaf has the following module dependencies:

$ jdeps --list-deps --multi-release 9 flatlaf-1.0.jar
   java.base
   java.desktop
   java.logging

This commit makes the java.logging dependency optional and hides logging behind a facade that falls back to printing to stderr if the java.logging module is not available.

To test, create a reduced JRE with a command like

jdk-15/bin/jlink.exe --module-path jdk-15/jmods --add-modules java.desktop --add-modules java.instrument --output jre-15-desktop-only

(adding java.instrument, so the FlatLafDemo main class can be started from IntelliJ IDEA)
This commit is contained in:
Ingo Kegel
2021-03-05 16:44:08 +01:00
parent e5a116a0d4
commit 343451de65
10 changed files with 81 additions and 47 deletions

View File

@@ -38,8 +38,6 @@ import java.util.Properties;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
@@ -74,7 +72,6 @@ import com.formdev.flatlaf.util.UIScale;
public abstract class FlatLaf
extends BasicLookAndFeel
{
static final Logger LOG = Logger.getLogger( FlatLaf.class.getName() );
private static final String DESKTOPFONTHINTS = "awt.font.desktophints";
private static List<Object> customDefaultsSources;
@@ -103,7 +100,7 @@ public abstract class FlatLaf
UIManager.setLookAndFeel( newLookAndFeel );
return true;
} catch( Exception ex ) {
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize look and feel '" + newLookAndFeel.getClass().getName() + "'.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to initialize look and feel '" + newLookAndFeel.getClass().getName() + "'.", ex );
return false;
}
}
@@ -341,7 +338,7 @@ public abstract class FlatLaf
} else
aquaLaf = (BasicLookAndFeel) Class.forName( aquaLafClassName ).newInstance();
} catch( Exception ex ) {
LOG.log( Level.SEVERE, "FlatLaf: Failed to initialize Aqua look and feel '" + aquaLafClassName + "'.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to initialize Aqua look and feel '" + aquaLafClassName + "'.", ex );
throw new IllegalStateException();
}
@@ -577,7 +574,7 @@ public abstract class FlatLaf
.invoke( null, true );
defaults.put( key, value );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( null, ex );
throw new RuntimeException( ex );
}
}
@@ -684,7 +681,7 @@ public abstract class FlatLaf
// update UI
updateUI();
} catch( UnsupportedLookAndFeelException ex ) {
LOG.log( Level.SEVERE, "FlatLaf: Failed to reinitialize look and feel '" + lookAndFeel.getClass().getName() + "'.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to reinitialize look and feel '" + lookAndFeel.getClass().getName() + "'.", ex );
}
} );
}

View File

@@ -30,7 +30,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.swing.UIDefaults;
import javax.swing.plaf.ColorUIResource;
import com.formdev.flatlaf.json.Json;
@@ -76,7 +75,7 @@ public class IntelliJTheme
try {
return FlatLaf.install( createLaf( in ) );
} catch( Exception ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to load IntelliJ theme", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to load IntelliJ theme", ex );
return false;
}
}
@@ -324,7 +323,7 @@ public class IntelliJTheme
try {
uiValue = UIDefaultsLoader.parseValue( key, valueStr );
} catch( RuntimeException ex ) {
UIDefaultsLoader.logParseError( Level.CONFIG, key, valueStr, ex );
UIDefaultsLoader.logParseError( key, valueStr, ex, false );
return; // ignore invalid value
}
}

View File

@@ -28,7 +28,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import com.formdev.flatlaf.util.StringUtils;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;
@@ -172,7 +171,7 @@ class LinuxFontPolicy
if( "1".equals( strs.get( 5 ) ) )
style |= Font.ITALIC;
} catch( RuntimeException ex ) {
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to parse 'font=" + generalFont + "'.", ex );
LoggingFacade.logConfig( "FlatLaf: Failed to parse 'font=" + generalFont + "'.", ex );
}
}
@@ -186,7 +185,7 @@ class LinuxFontPolicy
if( dpi < 50 )
dpi = 50;
} catch( NumberFormatException ex ) {
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to parse 'forceFontDPI=" + forceFontDPI + "'.", ex );
LoggingFacade.logConfig( "FlatLaf: Failed to parse 'forceFontDPI=" + forceFontDPI + "'.", ex );
}
}
@@ -225,7 +224,7 @@ class LinuxFontPolicy
while( (line = reader.readLine()) != null )
lines.add( line );
} catch( IOException ex ) {
FlatLaf.LOG.log( Level.CONFIG, "FlatLaf: Failed to read '" + filename + "'.", ex );
LoggingFacade.logConfig( "FlatLaf: Failed to read '" + filename + "'.", ex );
}
return lines;
}

View File

@@ -0,0 +1,46 @@
package com.formdev.flatlaf;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingFacade
{
private static final Logger LOG = createLogger();
private static Logger createLogger() {
try {
return Logger.getLogger( FlatLaf.class.getName() );
} catch( Throwable e ) {
// Module java.logging is not present
return null;
}
}
public static void logSevere( Throwable t ) {
logSevere( null, t );
}
public static void logSevere( String message ) {
logSevere( message, null );
}
public static void logSevere( String message, Throwable t ) {
if( LOG != null ) {
LOG.log( Level.SEVERE, message, t );
} else {
System.err.println( message );
t.printStackTrace();
}
}
public static void logConfig( String message, Throwable t ) {
if( LOG != null ) {
LOG.log( Level.CONFIG, message, t );
} else {
if (Boolean.getBoolean( "flatLaf.logConfig" )) {
System.err.println( message );
t.printStackTrace();
}
}
}
}

View File

@@ -33,7 +33,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.function.Function;
import java.util.logging.Level;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIDefaults.ActiveValue;
@@ -243,16 +242,21 @@ class UIDefaultsLoader
try {
defaults.put( key, parseValue( key, value, null, resolver, addonClassLoaders ) );
} catch( RuntimeException ex ) {
logParseError( Level.SEVERE, key, value, ex );
logParseError( key, value, ex, true );
}
}
} catch( IOException ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to load properties files.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to load properties files.", ex );
}
}
static void logParseError( Level level, String key, String value, RuntimeException ex ) {
FlatLaf.LOG.log( level, "FlatLaf: Failed to parse: '" + key + '=' + value + '\'', ex );
static void logParseError( String key, String value, RuntimeException ex, boolean severe ) {
String message = "FlatLaf: Failed to parse: '" + key + '=' + value + '\'';
if (severe) {
LoggingFacade.logSevere( message, ex );
} else {
LoggingFacade.logConfig( message, ex );
}
}
static String resolveValue( String value, Function<String, String> propertiesGetter ) {
@@ -440,7 +444,7 @@ class UIDefaultsLoader
try {
return findClass( value, addonClassLoaders ).newInstance();
} catch( InstantiationException | IllegalAccessException | ClassNotFoundException ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to instantiate '" + value + "'.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to instantiate '" + value + "'.", ex );
return null;
}
};
@@ -451,7 +455,7 @@ class UIDefaultsLoader
try {
return findClass( value, addonClassLoaders );
} catch( ClassNotFoundException ex ) {
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: Failed to find class '" + value + "'.", ex );
LoggingFacade.logSevere( "FlatLaf: Failed to find class '" + value + "'.", ex );
return null;
}
};
@@ -928,7 +932,7 @@ class UIDefaultsLoader
Object value = UIManager.get( uiKey );
if( value == null && !optional )
FlatLaf.LOG.log( Level.SEVERE, "FlatLaf: '" + uiKey + "' not found in UI defaults." );
LoggingFacade.logSevere( "FlatLaf: '" + uiKey + "' not found in UI defaults." );
return value;
}
}

View File

@@ -30,8 +30,6 @@ import java.awt.event.HierarchyListener;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
@@ -40,6 +38,7 @@ import javax.swing.UIManager;
import javax.swing.plaf.BorderUIResource;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatSystemProperties;
import com.formdev.flatlaf.LoggingFacade;
import com.formdev.flatlaf.util.HiDPIUtils;
import com.formdev.flatlaf.util.SystemInfo;
@@ -155,7 +154,7 @@ public class JBRCustomDecorations
try {
return (Boolean) Window_hasCustomDecoration.invoke( window );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( null, ex );
return false;
}
}
@@ -167,7 +166,7 @@ public class JBRCustomDecorations
try {
Window_setHasCustomDecoration.invoke( window );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( null, ex );
}
}
@@ -181,7 +180,7 @@ public class JBRCustomDecorations
WWindowPeer_setCustomDecorationHitTestSpots.invoke( peer, hitTestSpots );
WWindowPeer_setCustomDecorationTitleBarHeight.invoke( peer, titleBarHeight );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( null, ex );
}
}

View File

@@ -21,10 +21,8 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.LoggingFacade;
/**
* Provides Java version compatibility methods.
@@ -58,7 +56,7 @@ public class JavaCompatibility
? new Class[] { JComponent.class, Graphics2D.class, String.class, int.class, float.class, float.class }
: new Class[] { JComponent.class, Graphics.class, String.class, int.class, int.class, int.class } );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( ex );
throw new RuntimeException( ex );
}
}
@@ -70,7 +68,7 @@ public class JavaCompatibility
else
drawStringUnderlineCharAtMethod.invoke( null, c, g, text, underlinedIndex, x, y );
} catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( ex );
throw new RuntimeException( ex );
}
}
@@ -94,7 +92,7 @@ public class JavaCompatibility
: "clipStringIfNecessary",
new Class[] { JComponent.class, FontMetrics.class, String.class, int.class } );
} catch( Exception ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( ex );
throw new RuntimeException( ex );
}
}
@@ -103,7 +101,7 @@ public class JavaCompatibility
try {
return (String) getClippedStringMethod.invoke( null, c, fm, string, availTextWidth );
} catch( IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) {
Logger.getLogger( FlatLaf.class.getName() ).log( Level.SEVERE, null, ex );
LoggingFacade.logSevere( ex );
throw new RuntimeException( ex );
}
}

View File

@@ -19,7 +19,7 @@
*/
module com.formdev.flatlaf {
requires java.desktop;
requires java.logging;
requires static java.logging;
exports com.formdev.flatlaf;
exports com.formdev.flatlaf.icons;

View File

@@ -17,25 +17,21 @@
package com.formdev.flatlaf.intellijthemes;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.LoggingFacade;
/**
* @author Karl Tauber
*/
class Utils
{
static final Logger LOG = Logger.getLogger( FlatLaf.class.getName() );
static IntelliJTheme loadTheme( String name ) {
try {
return new IntelliJTheme( Utils.class.getResourceAsStream(
"/com/formdev/flatlaf/intellijthemes/themes/" + name ) );
} catch( IOException ex ) {
String msg = "FlatLaf: Failed to load IntelliJ theme '" + name + "'";
LOG.log( Level.SEVERE, msg, ex );
LoggingFacade.logSevere( msg, ex );
throw new RuntimeException( msg, ex );
}
}

View File

@@ -17,25 +17,21 @@
package com.formdev.flatlaf.intellijthemes.materialthemeuilite;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.LoggingFacade;
/**
* @author Karl Tauber
*/
class Utils
{
static final Logger LOG = Logger.getLogger( FlatLaf.class.getName() );
static IntelliJTheme loadTheme( String name ) {
try {
return new IntelliJTheme( Utils.class.getResourceAsStream(
"/com/formdev/flatlaf/intellijthemes/themes/material-theme-ui-lite/" + name ) );
} catch( IOException ex ) {
String msg = "FlatLaf: Failed to load IntelliJ theme '" + name + "'";
LOG.log( Level.SEVERE, msg, ex );
LoggingFacade.logSevere( msg, ex );
throw new RuntimeException( msg, ex );
}
}