Example usage for java.util.prefs Preferences getClass

List of usage examples for java.util.prefs Preferences getClass

Introduction

In this page you can find the example usage for java.util.prefs Preferences getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.mindquarry.desktop.util.AutostartUtilities.java

public static void setAutostart(boolean autostart, Set<String> targetPatterns) {
    // check if we are on a Windows platform, otherwise skip processing
    boolean windows = SVNFileUtil.isWindows;
    if (!windows) {
        log.debug("not setting autostart, os is not windows: " + System.getProperty("os.name"));
        return;/*from   www . j  a va  2  s . c o m*/
    }
    log.debug("setting autostart, os: " + System.getProperty("os.name"));
    // registry variables

    // create registry method objects
    final Preferences userRoot = Preferences.userRoot();
    final Class<? extends Preferences> clz = userRoot.getClass();

    try {
        // define methods for registry manipulation
        final Method mOpenKey = clz.getDeclaredMethod("openKey", //$NON-NLS-1$
                new Class[] { byte[].class, int.class, int.class });
        mOpenKey.setAccessible(true);

        final Method mCloseKey = clz.getDeclaredMethod("closeKey", //$NON-NLS-1$
                new Class[] { int.class });
        mCloseKey.setAccessible(true);

        final Method mWinRegSetValue = clz.getDeclaredMethod("WindowsRegSetValueEx", //$NON-NLS-1$
                new Class[] { int.class, byte[].class, byte[].class });
        mWinRegSetValue.setAccessible(true);

        final Method mWinRegDeleteValue = clz.getDeclaredMethod("WindowsRegDeleteValue", //$NON-NLS-1$
                new Class[] { int.class, byte[].class });
        mWinRegDeleteValue.setAccessible(true);

        // open registry key
        Integer hSettings = (Integer) mOpenKey.invoke(userRoot, new Object[] { toByteArray(AUTOSTART_KEY),
                new Integer(KEY_ALL_ACCESS), new Integer(KEY_ALL_ACCESS) });

        // check autostart settings
        if (autostart) {
            // find classpath entry for desktop client JAR:
            String path = JarUtilities.getJar(targetPatterns);
            // write autostart value
            mWinRegSetValue.invoke(userRoot,
                    new Object[] { hSettings, toByteArray(AUTOSTART_ENTRY), toByteArray(path) });
        } else {
            // delete autostart entry
            mWinRegDeleteValue.invoke(userRoot, new Object[] { hSettings, toByteArray(AUTOSTART_ENTRY) });
        }
        // close registry key
        mCloseKey.invoke(Preferences.userRoot(), new Object[] { hSettings });
    } catch (Exception e) {
        // TODO: throw exception and catch it outside to display an error dialog
        log.error("Error while writing registry entries.", e); //$NON-NLS-1$
    }
}