public static void disableWarning() { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); Unsafe u = (Unsafe) theUnsafe.get(null); Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger"); Field logger = cls.getDeclaredField("logger"); u.putObjectVolatile(cls, u.staticFieldOffset(logger), null); } catch (Exception e) { // ignore } } 这段代码有什么作用
时间: 2024-03-30 18:33:17 浏览: 19
这段代码的作用是禁用Java 9及以上版本中的IllegalAccessLogger警告。在Java 9中,当代码访问不属于它的模块时,会出现警告。这段代码通过使用Java反射API访问Unsafe类和IllegalAccessLogger类的私有字段和方法,从而禁用了IllegalAccessLogger警告。需要注意的是,这是一种hack方式,不建议在生产环境中使用。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![jpg](https://img-home.csdnimg.cn/images/20210720090814.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)