从AppContext中删除类的Java 9+等效项是什么?需要重新加载PrintServiceLookup类 [英] What is the Java 9+ equivalent for removing a class from AppContext; need to reload PrintServiceLookup class

查看:54
本文介绍了从AppContext中删除类的Java 9+等效项是什么?需要重新加载PrintServiceLookup类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将较旧的打印机API代码迁移到Java 8,并出现以下警告:

I migrated older printer API code to Java 8, and the following warning appears:

访问限制:方法'AppContext.getAppContext()'不是API(对必需的库'C:\ Program Files \ Java \ jre1.8.0_51 \ lib \ rt.jar'的限制)

Access restriction: The method 'AppContext.getAppContext()' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_51\lib\rt.jar')

这是由于此代码(最初源自此SO问题):

It is due to this code (originally sourced from this SO question):

/**
 * Printer list does not refresh itself; need to run this to refresh if necessary.
 */
public static void refreshSystemPrinterList() {

    Class[] classes = PrintServiceLookup.class.getDeclaredClasses();

    for (int i = 0; i < classes.length; i++) {

        if ("javax.print.PrintServiceLookup$Services".equals(classes[i].getName())) {

            AppContext.getAppContext().remove(classes[i]);  // the line that throws the warning
            break;
        }
    }

据我了解, PrintServiceLookup 会在加载类时加载可见打印机列表,但显然不会刷新此列表,或具有触发刷新的功能.长时间运行的应用程序刷新列表的方式将是卸载代码(正在执行的类)或重新启动自身.

As I understand it, PrintServiceLookup loads a list of visible printers when it is classloaded, but it apparently does not refresh this list, or have the ability trigger a refresh. The way for a long-running application to refresh the list would be to either unload the class, which is what the code is doing, or restart itself.

关于警告的轻度研究表明,Java 9将无法访问 AppContext.getAppContext().无需进一步研究,我目前的补救想法是使用可访问的类加载器加载此类,并在调用此方法时清除该类加载器.

Mild research on the warning reveals come Java 9, AppContext.getAppContext() will be inaccessible. Without further research, my current remediation idea is to use an accessible classloader for loading this class, and purging the classloader when this method is called.

最终,我想知道我需要做些什么才能正确替换它.当前在Windows上运行,可能正在迁移到Linux;我看到有关此SO问题的很多更新Linux实施.

Ultimately, I want to know what I need to do to properly replace this. Currently running on Windows, may be moving to Linux ; I see a lot of updates to this SO question regarding the Linux implementation.

推荐答案

PrintServiceLookupProvider (the only concrete implementation of PrintServiceLookup provides a thread that is supposed to do this.

它无限循环地检查更改,如果有更改,则调用 refreshServices .

It loops infinitely checking for changes, and calls refreshServices if there is one.

class PrinterChangeListener implements Runnable {
    long chgObj;
    PrinterChangeListener() {
        chgObj = notifyFirstPrinterChange(null);
    }

    @Override
    public void run() {
        if (chgObj != -1) {
            while (true) {
                // wait for configuration to change
                if (notifyPrinterChange(chgObj) != 0) {
                    try {
                        refreshServices();
                    } catch (SecurityException se) {
                        break;
                    }
                } else {
                    notifyClosePrinterChange(chgObj);
                    break;
                }
            }
        }
    }
}

从这里开始:

Thread thr = new Thread(null, new PrinterChangeListener(), "PrinterListener", 0, false);
thr.setDaemon(true);
thr.start();

如果您对此有疑问,请查看为什么 notifyPrinterChange 不能表明已更改某些内容.这是一种本地方法,因此实现取决于您的操作系统.

If you are having problems with this not working, you should look into why notifyPrinterChange is not indicating that something changed. It's a native method, so the implementation will depend on your OS.

不幸的是,您甚至不能使用反射来手动调用 refreshServices ,因为对Sun模块的反射是受限制的(您可以覆盖它,但是如果这样做,那么您也可以覆盖 AppContext )

Unfortunately you cannot even use reflection to call refreshServices manually, since reflection over Sun modules is restricted (you can override it, but if you were doing that then you may as well override the export of AppContext)

这篇关于从AppContext中删除类的Java 9+等效项是什么?需要重新加载PrintServiceLookup类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆