Skip to content

Commit

Permalink
See #17858: Remove old workarounds for javabugs and update Utils#getL…
Browse files Browse the repository at this point in the history
…atestVersion

* JDK-8180379: Fixed in Java 9+
* JDK-8251377: Fixed in Java 11.0.14 (2021-09-02)
* JDK-8262085: Fixed in Java 11.0.17 (2022-07-19)

git-svn-id: https://josm.openstreetmap.de/svn/trunk@19109 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
taylor.smock committed Jun 17, 2024
1 parent 25780f5 commit 2c95b45
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 120 deletions.
57 changes: 0 additions & 57 deletions src/org/openstreetmap/josm/gui/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.openstreetmap.josm.tools.Utils.getSystemProperty;

import java.awt.AWTError;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
Expand All @@ -18,7 +17,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.Authenticator;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand All @@ -41,7 +39,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -164,9 +161,7 @@
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.OsmUrlToBounds;
import org.openstreetmap.josm.tools.PlatformHook.NativeOsCallback;
import org.openstreetmap.josm.tools.PlatformHookWindows;
import org.openstreetmap.josm.tools.PlatformManager;
import org.openstreetmap.josm.tools.ReflectionUtils;
import org.openstreetmap.josm.tools.Shortcut;
import org.openstreetmap.josm.tools.Utils;
import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
Expand Down Expand Up @@ -1068,19 +1063,6 @@ private static void updateSystemProperties() {
}
Utils.updateSystemProperty("http.agent", Version.getInstance().getAgentString());
Utils.updateSystemProperty("user.language", Config.getPref().get("language"));
// Workaround to fix a Java bug. This ugly hack comes from Sun bug database: https://bugs.openjdk.java.net/browse/JDK-6292739
// Force AWT toolkit to update its internal preferences (fix #6345).
// Does not work anymore with Java 9, to remove with Java 9 migration
if (Utils.getJavaVersion() < 9 && !GraphicsEnvironment.isHeadless()) {
try {
Field field = Toolkit.class.getDeclaredField("resources");
ReflectionUtils.setObjectsAccessible(field);
field.set(null, ResourceBundle.getBundle("sun.awt.resources.awt"));
} catch (ReflectiveOperationException | RuntimeException e) { // NOPMD
// Catch RuntimeException in order to catch InaccessibleObjectException, new in Java 9
Logging.log(Logging.LEVEL_WARN, null, e);
}
}
// Possibility to disable SNI (not by default) in case of misconfigured https servers
// See #9875 + http://stackoverflow.com/a/14884941/2257172
// then https://josm.openstreetmap.de/ticket/12152#comment:5 for details
Expand Down Expand Up @@ -1115,44 +1097,6 @@ public static void setupNadGridSources() {
*/
static void applyLaFWorkarounds() {
final String laf = UIManager.getLookAndFeel().getID();
final int javaVersion = Utils.getJavaVersion();
// Workaround for JDK-8180379: crash on Windows 10 1703 with Windows L&F and java < 8u141 / 9+172
// To remove during Java 9 migration
if (getSystemProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows 10") &&
PlatformManager.getPlatform().getDefaultStyle().equals(LafPreference.LAF.get())) {
try {
String build = PlatformHookWindows.getCurrentBuild();
if (build != null) {
final int currentBuild = Integer.parseInt(build);
final int javaUpdate = Utils.getJavaUpdate();
final int javaBuild = Utils.getJavaBuild();
// See https://technet.microsoft.com/en-us/windows/release-info.aspx
if (currentBuild >= 15_063 && ((javaVersion == 8 && javaUpdate < 141)
|| (javaVersion == 9 && javaUpdate == 0 && javaBuild < 173))) {
// Workaround from https://bugs.openjdk.java.net/browse/JDK-8179014
UIManager.put("FileChooser.useSystemExtensionHiding", Boolean.FALSE);
}
}
} catch (NumberFormatException | ReflectiveOperationException | JosmRuntimeException e) {
Logging.error(e);
} catch (ExceptionInInitializerError e) {
Logging.log(Logging.LEVEL_ERROR, null, e);
}
} else if (PlatformManager.isPlatformOsx() && javaVersion < 17) {
// Workaround for JDK-8251377: JTabPanel active tab is unreadable in Big Sur, see #20075, see #20821
// os.version will return 10.16, or 11.0 depending on environment variable
// https://twitter.com/BriceDutheil/status/1330926649269956612
final String macOSVersion = getSystemProperty("os.version");
if ((laf.contains("Mac") || laf.contains("Aqua"))
&& (macOSVersion.startsWith("10.16") || macOSVersion.startsWith("11"))) {
UIManager.put("TabbedPane.foreground", Color.BLACK);
}
}
// Workaround for JDK-8262085
if ("Metal".equals(laf) && javaVersion >= 11 && javaVersion < 17) {
UIManager.put("ToolTipUI", JosmMetalToolTipUI.class.getCanonicalName());
}

// See #20850. The upstream bug (JDK-6396936) is unlikely to ever be fixed due to potential compatibility
// issues. This affects Windows LaF only (includes Windows Classic, a sub-LaF of Windows LaF).
if ("Windows".equals(laf) && "Monospaced".equals(UIManager.getFont("TextArea.font").getFamily())) {
Expand Down Expand Up @@ -1270,7 +1214,6 @@ private static void setupTextAntiAliasing() {
// On Linux and running on Java 9+, enable text anti aliasing
// if not yet enabled and if neither running on Gnome or KDE desktop
if (PlatformManager.isPlatformUnixoid()
&& Utils.getJavaVersion() >= 9
&& UIManager.getLookAndFeelDefaults().get(RenderingHints.KEY_TEXT_ANTIALIASING) == null
&& System.getProperty("awt.useSystemAAFontSettings") == null
&& Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/Antialias") == null
Expand Down
15 changes: 1 addition & 14 deletions src/org/openstreetmap/josm/tools/ImageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -1717,27 +1717,14 @@ private static BufferedImage read(ImageInputStream stream, boolean readMetadata,
reader.setInput(stream, true, !readMetadata && !enforceTransparency);
ImageReadParam param = readParamFunction.apply(reader);
BufferedImage bi = null;
try { // NOPMD
try (stream) {
bi = reader.read(0, param);
if (bi.getTransparency() != Transparency.TRANSLUCENT && (readMetadata || enforceTransparency) && Utils.getJavaVersion() < 11) {
Color color = getTransparentColor(bi.getColorModel(), reader);
if (color != null) {
Hashtable<String, Object> properties = new Hashtable<>(1);
properties.put(PROP_TRANSPARENCY_COLOR, color);
bi = new BufferedImage(bi.getColorModel(), bi.getRaster(), bi.isAlphaPremultiplied(), properties);
if (enforceTransparency) {
Logging.trace("Enforcing image transparency of {0} for {1}", stream, color);
bi = makeImageTransparent(bi, color);
}
}
}
} catch (LinkageError e) {
// On Windows, ComponentColorModel.getRGBComponent can fail with "UnsatisfiedLinkError: no awt in java.library.path", see #13973
// Then it can leads to "NoClassDefFoundError: Could not initialize class sun.awt.image.ShortInterleavedRaster", see #15079
Logging.error(e);
} finally {
reader.dispose();
stream.close();
}
return bi;
}
Expand Down
22 changes: 7 additions & 15 deletions src/org/openstreetmap/josm/tools/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,9 @@ public static List<String> getMatches(final Matcher m) {
* @return null if <code>o</code> is null or the type <code>o</code> is not
* a subclass of <code>klass</code>. The casted value otherwise.
*/
@SuppressWarnings("unchecked")
public static <T> T cast(Object o, Class<T> klass) {
if (klass.isInstance(o)) {
return (T) o;
return klass.cast(o);
}
return null;
}
Expand Down Expand Up @@ -1783,15 +1782,10 @@ public static String getJavaLatestVersion() {
"java.baseline.version.url",
Config.getUrls().getJOSMWebsite() + "/remote/oracle-java-update-baseline.version")))
.connect().fetchContent().split("\n", -1);
if (getJavaVersion() <= 11 && isRunningWebStart()) { // OpenWebStart currently only has Java 11
for (String version : versions) {
if (version.startsWith("11")) {
return version;
}
}
} else if (getJavaVersion() <= 17) {
// OpenWebStart currently only has Java 21
if (getJavaVersion() <= 21) {
for (String version : versions) {
if (version.startsWith("17")) { // Use current Java LTS
if (version.startsWith("21")) { // Use current Java LTS
return version;
}
}
Expand Down Expand Up @@ -1851,7 +1845,7 @@ public static boolean isRunningOpenWebStart() {
/**
* Get a function that converts an object to a singleton stream of a certain
* class (or null if the object cannot be cast to that class).
*
* <p>
* Can be useful in relation with streams, but be aware of the performance
* implications of creating a stream for each element.
* @param <T> type of the objects to convert
Expand All @@ -1876,10 +1870,9 @@ public static <T, U> Function<T, Stream<U>> castToStream(Class<U> klass) {
* @param consumer action to take when o is and instance of T
* @since 12604
*/
@SuppressWarnings("unchecked")
public static <T> void instanceOfThen(Object o, Class<T> klass, Consumer<? super T> consumer) {
if (klass.isInstance(o)) {
consumer.accept((T) o);
consumer.accept(klass.cast(o));
}
}

Expand All @@ -1892,10 +1885,9 @@ public static <T> void instanceOfThen(Object o, Class<T> klass, Consumer<? super
* @return {@link Optional} containing the result of the cast, if it is possible, an empty
* Optional otherwise
*/
@SuppressWarnings("unchecked")
public static <T> Optional<T> instanceOfAndCast(Object o, Class<T> klass) {
if (klass.isInstance(o))
return Optional.of((T) o);
return Optional.of(klass.cast(o));
return Optional.empty();
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/org/openstreetmap/josm/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public static Component getComponentByName(Component root, String name) {
public static void assumeWorkingEqualsVerifier() {
// See https://github.com/raphw/byte-buddy/blob/master/byte-buddy-dep/src/main/java/net/bytebuddy/ClassFileVersion.java
// for currently supported Java versions.
if (Utils.getJavaVersion() >= 19) {
if (Utils.getJavaVersion() >= 22) {
// Byte Buddy often supports new class file versions for current EA releases if its experimental flag is set to true
System.setProperty("net.bytebuddy.experimental", "true");
} else {
Expand Down
12 changes: 0 additions & 12 deletions test/unit/org/openstreetmap/josm/plugins/PluginHandlerTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,6 @@ public static void loadAllPlugins() {
}
}

// On Java < 11 and headless mode, filter plugins requiring JavaFX as Monocle is not available
int javaVersion = Utils.getJavaVersion();
if (GraphicsEnvironment.isHeadless() && javaVersion < 11) {
for (Iterator<PluginInformation> it = plugins.iterator(); it.hasNext();) {
PluginInformation pi = it.next();
if (pi.getRequiredPlugins().contains("javafx")) {
System.out.println("Ignoring " + pi.name + " (requiring JavaFX and we're using Java < 11 in headless mode)");
it.remove();
}
}
}

// Skip unofficial plugins in headless mode, too much work for us for little added-value
if (GraphicsEnvironment.isHeadless()) {
for (Iterator<PluginInformation> it = plugins.iterator(); it.hasNext();) {
Expand Down
21 changes: 0 additions & 21 deletions test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package org.openstreetmap.josm.testutils;

import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.io.ByteArrayInputStream;
Expand All @@ -16,7 +14,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.text.MessageFormat;
Expand All @@ -25,7 +22,6 @@
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;
import java.util.logging.Level;

import org.awaitility.Awaitility;
import org.awaitility.Durations;
Expand Down Expand Up @@ -77,8 +73,6 @@
import org.openstreetmap.josm.tools.JosmRuntimeException;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.MemoryManagerTest;
import org.openstreetmap.josm.tools.PlatformManager;
import org.openstreetmap.josm.tools.Utils;
import org.openstreetmap.josm.tools.bugreport.ReportedException;
import org.openstreetmap.josm.tools.date.DateUtils;

Expand Down Expand Up @@ -645,7 +639,6 @@ protected void before() throws InitializationError, ReflectiveOperationException
this.navigableComponentMockingRunnable.run();
}

workaroundJdkBug8159956();
new MainApplication();
JOSMFixture.initContentPane();
JOSMFixture.initMainPanel(true);
Expand All @@ -655,20 +648,6 @@ protected void before() throws InitializationError, ReflectiveOperationException
}
}

private void workaroundJdkBug8159956() {
// Note: This has been backported to Java 8u381 (2023-07-18)
try {
if (PlatformManager.isPlatformWindows() && Utils.getJavaVersion() == 8 && GraphicsEnvironment.isHeadless()) {
// https://bugs.openjdk.java.net/browse/JDK-8159956
Method initIDs = Toolkit.class.getDeclaredMethod("initIDs");
initIDs.setAccessible(true);
initIDs.invoke(Toolkit.getDefaultToolkit());
}
} catch (Exception e) {
Logging.log(Level.WARNING, "Failed to Toolkit.initIDs", e);
}
}

/**
* Clean up what test not using these test rules may have broken.
*/
Expand Down

0 comments on commit 2c95b45

Please sign in to comment.