From fbab485e42c5b07733ae390b236d944abfcedefb Mon Sep 17 00:00:00 2001 From: soares Date: Fri, 12 Jun 2020 12:15:32 +0100 Subject: [PATCH 1/1] JAL-3446 separated out potential exceptions for simplified key mask operation --- src/jalview/util/Platform.java | 45 +++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 62f618a..bc21fa1 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -115,24 +115,45 @@ public class Platform static { - if (!GraphicsEnvironment.isHeadless()) - { + // Using non-deprecated Extended key mask modifiers, but Java 8 has no + // getMenuShortcutKeyMaskEx method + Toolkit tk = Toolkit.getDefaultToolkit(); + Method method = null; try { - - Toolkit tk = Toolkit.getDefaultToolkit(); - Method method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx"); - if (method == null) + method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx"); + } catch (NoSuchMethodException e) + { + System.err.println( + "Could not find Toolkit method getMenuShortcutKeyMaskEx. Trying getMenuShortcutKeyMask."); + } + if (method == null) + { + try + { method = tk.getClass().getMethod("getMenuShortcutKeyMask"); - SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0])); - if (SHORTCUT_KEY_MASK <= 0xF) + } catch (NoSuchMethodException e) { - // shift this into the extended region (was Java 8) - SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6; + System.err.println( + "Could not find Toolkit method getMenuShortcutKeyMaskEx or getMenuShortcutKeyMask."); + e.printStackTrace(); } - } catch (Exception e) + } + if (method != null) { - e.printStackTrace(); + try + { + method.setAccessible(true); + SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0])); + } catch (Exception e) + { + e.printStackTrace(); + } + } + if (SHORTCUT_KEY_MASK <= 0xF) + { + // shift this into the extended region (was Java 8) + SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6; } } } -- 1.7.10.2