lundi 11 septembre 2017

How to write java class if references classs that dont exist on some systems

I have this method that only runs on Windows and uses classes from sun.awt.shell, however the application can run on non-windows systems, and on Java it can be used with the OpenSdk and I suspect this does not include the sun.awt.shell class.

I have removed sun.awt.shell import statements from the class so that the class name is only referenced in this method, and I can guarantee that this method will never actually be run on a non-windows machine.

But is this enough or will the class fail to load anyway on systems missing these classes, do I need to rewrite the method to use reflection ?

 public static boolean isRemote(String newPath)
    {
        try
        {
            Path root = Paths.get(newPath).getRoot();
            sun.awt.shell.ShellFolder shellFolder = sun.awt.shell.ShellFolder.getShellFolder(root.toFile());
            sun.awt.shell.ShellFolderColumnInfo[] cols = shellFolder.getFolderColumns();
            for (int i = 0; i < cols.length; i++)
            {
                if (cols[i].getTitle().equals(WINDOWS_SHELL_SIZE)
                        &&  ((String) shellFolder.getFolderColumnValue(i)).startsWith("Network Drive"))
                {
                    return true;
                }
                else if (cols[i].getTitle().equals(WINDOWS_SHELL_ATTRIBUTES)
                        &&  ((String) shellFolder.getFolderColumnValue(i)).startsWith("\\"))
                {
                    return true;
                }
            }
        }
        catch (Exception ex)
        {
            MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
            return false;
        }
        return false;
    }





Aucun commentaire:

Enregistrer un commentaire