JDK11 的 checkSystemClipboardAccess 替代方案

checkSystemClipboardAccess alternative for JDK11

JDK11 已从 SecurityManager 中删除 checkSystemClipboardAccess。剪贴板处理中建议的替代习惯用法是什么?

我想像这样检查剪贴板的可用性:

public static boolean hasClipboard() {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) return true;
    try {
        sm.checkSystemClipboardAccess();
        return true;
    } catch (SecurityException x) {
        /* */
    }
    return false;
}

但是在JDK11中是无法编译的

编辑 2020 年 2 月 1 日:
根据我现在使用的建议:

public static boolean hasClipboard() {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) return true;
    try {
        sm.checkPermission(new AWTPermission("accessClipboard"));
        return true;
    } catch (SecurityException x) {
        /* */
    }
    return false;
}

一个快速解决方法是将对 sm.checkSystemClipboardAccess(); 的调用简单地替换为对 sm.checkPermission(new java.awt.AWTPermission("accessClipboard"));.

的调用

请参阅 Oracle 的 JDK 8 技术说明 Permissions in the Java Development Kit (JDK),其中明确解决了 checkSystemClipboardAccess():

的弃用问题

Method

public void checkSystemClipboardAccess(); Note: This method is deprecated; use instead public void checkPermission(Permission perm);

Permission

java.awt.AWTPermission "accessClipboard";

[Google] 搜索术语 java checkSystemClipboardAccess 已删除 为 Oracle 的 [= 提供了 link 19=]doc for class SecurityManager (JDK 10),方法 checkSystemClipboardAccess() 的文档包含以下内容:

Deprecated, for removal: This API element is subject to removal in a future version. This method was originally used to check if the calling thread could access the system clipboard. The method has been obsoleted and code should instead use checkPermission(java.security.Permission) to check AWTPermission("accessClipboard"). This method is subject to removal in a future version of Java SE.