简体   繁体   English

在Java中是否可以使用自定义类隐藏java.lang中的类?

[英]Is it possible in Java to hide classes from java.lang with custom classes?

Is it possible in Java to hide classes from the platform with custom classes? 在Java中是否可以使用自定义类从平台隐藏类? For instance to create a custom java.lang.String or custom java.lang.StringBuffer? 例如,创建自定义java.lang.String或自定义java.lang.StringBuffer? I am asking because I recently discovered there is an option in C# /nostdlib that allows you to redefine the entire System namespace and I was wondering if Java had something similar. 我问,因为我最近发现C#/ nostdlib中有一个选项允许您重新定义整个System命名空间,我想知道Java是否有类似的东西。

Note, C#'s /nostdlib is a compiler option, not something that gets taken into account at runtime. 注意,C#的/ nostdlib是一个编译器选项,而不是在运行时考虑的东西。

You can modify the standard library or substitute your own for personal use, but Oracle doesn't support this and doesn't allow you to distribute the modified version to others. 您可以修改标准库或替换自己的标准库供个人使用,但Oracle不支持此功能,并且不允许您将修改后的版本分发给其他人。 There are various commandline and configuration option to tell the JVM where to look for classes. 有各种命令行和配置选项可以告诉JVM在哪里查找类。 Replacing bootstrap classes is harder but it is possible. 替换bootstrap类更难,但它是可能的。

Note that creating your own version entirely from scratch will be difficult because several classes ( Object and Class at the very least) are necessarily tied very strongly to the JVM implementation. 请注意,完全从头开始创建自己的版本将很困难,因为几个类(至少是ObjectClass )必然与JVM实现非常紧密地联系在一起。 You'll probably want to keep all the classes that use native code the same for simplicity. 为简单起见,您可能希望保留使用本机代码的所有类。

Note that this is different from class Shadowing, which is a purely compile time concept that occurs when multiple identifiers conflict, usually due to imports ( java.lang.* is implicitly imported and there's nothing you can do about that). 请注意,这与类Shadowing不同,后者是一个纯粹的编译时概念,当多个标识符冲突时发生,通常是由于导入( java.lang.*是隐式导入的,你无能为力)。 These conflicts are generally resolved by either silently using one of the possibilities or raising a compile error depending on the situation (see the JLS for details). 这些冲突通常通过静默使用其中一种可能性或根据情况引发编译错误来解决(有关详细信息,请参阅JLS)。

In order to replace a java.lang class, or some other "system" class you must modify the boot classpath. 要替换java.lang类或其他“系统”类,必须修改引导类路径。 If you run Java from a command line this is easily accomplished, but if you run it in some sort of "environment" you may not be allowed to make that change. 如果从命令行运行Java,则很容易实现,但如果在某种“环境”中运行它,则可能不允许进行此更改。

Additionally, many java.lang et al classes have native method implementations, and you must either supply your own replacement or somehow arrange to reuse the existing implementation. 此外,许多java.lang等类都有本机方法实现,您必须提供自己的替换或以某种方式安排重用现有实现。

You can use your own classes for the library classes. 您可以将自己的类用于库类。 When starting you can define a -bootclasspath to define your own class library. 启动时,您可以定义-bootclasspath来定义自己的类库。

This will not an easy task because some classes are tighgtly bound to the VM running, eg the class String . 这不是一件容易的事,因为有些类很难绑定到运行的VM,例如类String You have to look at the JLS for such cases. 您必须查看JLS以了解此类情况。

Sun/Oracle does not allow that by the license of the JSE/JDK. Sun / Oracle不允许通过JSE / JDK的许可证。 However, I recently learned, that Groovy can do amazing things, like adding methods or overwriting methods of the java.lang.* package. 但是,我最近了解到, Groovy可以做一些很棒的事情,比如添加方法或覆盖java.lang。*包的方法。

Groovy is based on Java/JVM, so you can easily combine them within one application. Groovy基于Java / JVM,因此您可以轻松地将它们组合在一个应用程序中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM