Changeset 10178 in josm


Ignore:
Timestamp:
2016-05-10T23:53:43+02:00 (22 months ago)
Author:
Don-vip
Message:

see #11924 - add compatibility with JEP 272 for Java 9 on OSX

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r10152 r10178  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Desktop;
    67import java.awt.Image;
    78import java.awt.Window;
     
    1011import java.io.IOException;
    1112import java.lang.reflect.InvocationHandler;
     13import java.lang.reflect.InvocationTargetException;
    1214import java.lang.reflect.Method;
    1315import java.lang.reflect.Proxy;
     
    4244    public void startupHook() {
    4345        // Here we register callbacks for the menu entries in the system menu and file opening through double-click
     46        // http://openjdk.java.net/jeps/272
     47        // https://bugs.openjdk.java.net/browse/JDK-8048731
     48        // http://cr.openjdk.java.net/~azvegint/jdk/9/8143227/10/jdk/
     49        // This method must be cleaned up after we switch to Java 9
    4450        try {
    4551            Class<?> eawtApplication = Class.forName("com.apple.eawt.Application");
    46             Class<?> eawtQuitHandler = Class.forName("com.apple.eawt.QuitHandler");
    47             Class<?> eawtAboutHandler = Class.forName("com.apple.eawt.AboutHandler");
    48             Class<?> eawtOpenFilesHandler = Class.forName("com.apple.eawt.OpenFilesHandler");
    49             Class<?> eawtPreferencesHandler = Class.forName("com.apple.eawt.PreferencesHandler");
    50             Object appli = eawtApplication.getConstructor((Class[]) null).newInstance((Object[]) null);
     52            Class<?> quitHandler = findHandlerClass("QuitHandler");
     53            Class<?> aboutHandler = findHandlerClass("AboutHandler");
     54            Class<?> openFilesHandler = findHandlerClass("OpenFilesHandler");
     55            Class<?> preferencesHandler = findHandlerClass("PreferencesHandler");
    5156            Object proxy = Proxy.newProxyInstance(PlatformHookOsx.class.getClassLoader(), new Class<?>[] {
    52                 eawtQuitHandler, eawtAboutHandler, eawtOpenFilesHandler, eawtPreferencesHandler}, ivhandler);
    53             eawtApplication.getDeclaredMethod("setQuitHandler", eawtQuitHandler).invoke(appli, proxy);
    54             eawtApplication.getDeclaredMethod("setAboutHandler", eawtAboutHandler).invoke(appli, proxy);
    55             eawtApplication.getDeclaredMethod("setOpenFileHandler", eawtOpenFilesHandler).invoke(appli, proxy);
    56             eawtApplication.getDeclaredMethod("setPreferencesHandler", eawtPreferencesHandler).invoke(appli, proxy);
    57             // this method has been deprecated, but without replacement ATM
    58             eawtApplication.getDeclaredMethod("setEnabledPreferencesMenu", boolean.class).invoke(appli, Boolean.TRUE);
    59             // setup the dock icon. It is automatically set with application bundle and Web start but we need
    60             // to do it manually if run with `java -jar``
    61             eawtApplication.getDeclaredMethod("setDockIconImage", Image.class).invoke(appli, ImageProvider.get("logo").getImage());
    62             // enable full screen
    63             enableOSXFullscreen((Window) Main.parent);
     57                quitHandler, aboutHandler, openFilesHandler, preferencesHandler}, ivhandler);
     58            try {
     59                Object appli = eawtApplication.getConstructor((Class[]) null).newInstance((Object[]) null);
     60                setHandlers(eawtApplication, quitHandler, aboutHandler, openFilesHandler, preferencesHandler, proxy, appli);
     61                // this method has been deprecated, but without replacement ATM
     62                eawtApplication.getDeclaredMethod("setEnabledPreferencesMenu", boolean.class).invoke(appli, Boolean.TRUE);
     63                // setup the dock icon. It is automatically set with application bundle and Web start but we need
     64                // to do it manually if run with `java -jar``
     65                eawtApplication.getDeclaredMethod("setDockIconImage", Image.class).invoke(appli, ImageProvider.get("logo").getImage());
     66                // enable full screen
     67                enableOSXFullscreen((Window) Main.parent);
     68            } catch (IllegalAccessException e) {
     69                // with Java 9, module java.desktop does not export com.apple.eawt, use new Desktop API instead
     70                setHandlers(Desktop.class, quitHandler, aboutHandler, openFilesHandler, preferencesHandler, proxy, Desktop.getDesktop());
     71            }
    6472        } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException ex) {
    6573            // We'll just ignore this for now. The user will still be able to close JOSM by closing all its windows.
     
    7078        if (java != null && java.startsWith("1.7")) {
    7179            askUpdateJava(java);
     80        }
     81    }
     82
     83    protected void setHandlers(Class<?> appClass, Class<?> quitHandler, Class<?> aboutHandler,
     84            Class<?> openFilesHandler, Class<?> preferencesHandler, Object proxy, Object appInstance)
     85                    throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
     86        appClass.getDeclaredMethod("setQuitHandler", quitHandler).invoke(appInstance, proxy);
     87        appClass.getDeclaredMethod("setAboutHandler", aboutHandler).invoke(appInstance, proxy);
     88        appClass.getDeclaredMethod("setOpenFileHandler", openFilesHandler).invoke(appInstance, proxy);
     89        appClass.getDeclaredMethod("setPreferencesHandler", preferencesHandler).invoke(appInstance, proxy);
     90    }
     91
     92    protected Class<?> findHandlerClass(String className) throws ClassNotFoundException {
     93        try {
     94            return Class.forName("com.apple.eawt."+className);
     95        } catch (ClassNotFoundException e) {
     96            return Class.forName("java.awt.desktop."+className);
    7297        }
    7398    }
     
    125150            boolean closed = Main.exitJosm(false, 0);
    126151            if (args[1] != null) {
    127                 args[1].getClass().getDeclaredMethod(closed ? "performQuit" : "cancelQuit").invoke(args[1]);
     152                try {
     153                    args[1].getClass().getDeclaredMethod(closed ? "performQuit" : "cancelQuit").invoke(args[1]);
     154                } catch (IllegalAccessException e) {
     155                    // with Java 9, module java.desktop does not export com.apple.eawt, use new Desktop API instead
     156                    Class.forName("java.awt.desktop.QuitResponse").getMethod(closed ? "performQuit" : "cancelQuit").invoke(args[1]);
     157                }
    128158            }
    129159            break;
Note: See TracChangeset for help on using the changeset viewer.