简体   繁体   English

在同一个包中对sun。*类进行子类化会产生IllegalAccessError

[英]Subclassing sun.* class in same package gives IllegalAccessError

Foreword: 前言:

  1. What am I going to show you is WRONG and I'm well aware of how bad I am for breaking encapsulation by doing such foolish thing. 我要向你展示的是错误的,我很清楚通过做这样愚蠢的事情来打破封装有多糟糕。
  2. I'm not trying to solve any more general I/O problem. 我不是要解决任何更普遍的I / O问题。 It's just an experiment. 这只是一个实验。

I'm trying to sub-class sun.nio.ch.SourceChannelImpl which is package private class with package private constructor present in JDK (in rt.jar) so I have to create it in sun.nio.ch package. 我正在尝试子类sun.nio.ch.SourceChannelImpl子类,它是包私有类,包含JDK中的包私有构造函数(在rt.jar中)所以我必须在sun.nio.ch包中创建它。

Here is my sub-class: 这是我的子类:

package sun.nio.ch;
import java.io.FileDescriptor;
import java.nio.channels.spi.SelectorProvider;
class MySourceChannel extends SourceChannelImpl {
  public MySourceChannel(SelectorProvider sp, FileDescriptor fd) {
    super(sp, fd);
  }
}

Here is my simple test: 这是我的简单测试:

package sun.nio.ch;
import java.io.FileDescriptor;
public class Main {
  public static void main(String[] args) {
    new MySourceChannel(null, FileDescriptor.in);
  }
}

And here's the failure: 而这是失败:

Exception in thread "main" java.lang.IllegalAccessError: class sun.nio.ch.MySourceChannel cannot access its superclass sun.nio.ch.SourceChannelImpl
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.nio.ch.Main.main(Main.java:5)

It's probably not you can't define class in JDK package XYZ ((java|sun).*) type of problem because otherwise I'd get 可能不是你不能在JDK包中定义类XYZ((java | sun)。*)类型的问题因为否则我会得到

java.lang.SecurityException: Prohibited package name: XYZ

Main class works fine in this package. Main类在此包中正常工作。

I've also tried to disable security checks by setting Policy allowing everything and that didn't help neither. 我也尝试通过设置Policy允许一切来禁用安全检查,这也没有帮助。 I've also tried System.setSecurityManager(null); 我也试过System.setSecurityManager(null); (I'm not sure if this actually disables it) and it didn't help neither. (我不确定这是否真的禁用它)并且它也没有帮助。

What's the problem? 有什么问题? How can I fix it please? 我该怎么办呢?

I've tried it with JDK 1.7.0_45, both Oracle and OpenJDK. 我已经尝试过使用JDK 1.7.0_45,Oracle和OpenJDK。

SourceChannelImpl is a "package private" class. SourceChannelImpl是一个“包私有”类。 In the JVM a package is always loaded by a single class loader. 在JVM中,包总是由单个类加载器加载。 If you have two packages with the same name loaded by different class loaders, they are not the same package. 如果有两个具有相同名称的包由不同的类加载器加载,则它们不是同一个包。

You can fix this by loading some or all of your code in the bootstrap class loader with -Xbootclasspath/a:mybootspath . 您可以通过使用-Xbootclasspath/a:mybootspath在引导程序类加载器中加载部分或全部代码来解决此-Xbootclasspath/a:mybootspath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 太阳发生了什么 - what happened to sun.* packages Java 8的Java 7 sun。*类 - Java 7 sun.* classes with Java 8 声明库包的类时出现IllegalAccessError - IllegalAccessError when declaring a class for a library package 在Eclipse调试器中查看sun。*软件包的源 - View sources of sun.* packages in Eclipse debugger 在Eclipse Java项目中使用sun。*类 - Use sun.* classes in Eclipse Java project 在JMock中访问包私有类时出现IllegalAccessError - IllegalAccessError when accessing package private class in JMock 太阳之间的区别。* vs com.sun。* - Difference between sun.* vs com.sun.* 将类添加到Swing包中-结果是可见函数上的IllegalAccessError - Adding a class to the Swing package — result is an IllegalAccessError on a visible function java.lang.IllegalAccessError:尝试从类sun.security.ssl.ClientHandshaker访问字段sun.security.ssl.Handshaker.localSupportedSignAlgs - java.lang.IllegalAccessError: tried to access field sun.security.ssl.Handshaker.localSupportedSignAlgs from class sun.security.ssl.ClientHandshaker 在 Java 17 上使用 Spark 3.3.0 运行单元测试失败并显示 IllegalAccessError:class StorageUtils 无法访问 class sun.nio.ch.DirectBuffer - Running unit tests with Spark 3.3.0 on Java 17 fails with IllegalAccessError: class StorageUtils cannot access class sun.nio.ch.DirectBuffer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM