Java源码中机器生成(mechanically-generated)的源文件

本文翻译自 https://stackoverflow.com/questions/24904034/mechanically-generated-java-source-files-in-the-java-source-code

当我查看Java源码时,我发现一些特别的文件,主要涉及的有java.nio包下的ByteBuffer等类似的类,有舍非常凌乱的源码,还写着This file was mechanically generated: Do not edit! ,这些文件通过机器生成的,不要编辑!

这些文件包含了大量空行(有些升值在javadocs中),大概是为了防止行号更改,我也看过一些java反编译器,类似procyon-decompiler,它有一个选项来保持行号,但我怀疑是这种情况,因为在最终accolade前添加空白行不会改变什么。

下面是其中一些文件(我在网上找不到它们的链接,也没有粘贴,因为我不想破坏任何版权,但你能在JDK安装文件夹根目录下的src.zip中找到它们)

  • java.nio.ByteBuffer
  • java.nio.DirectByteBufferR
  • java.nio.Bits
  • java.nio.BufferOverflowException

我很想知道

  • 什么工具生成的这些文件?
  • 为什么工具要保持行号不变,为了更容易debugger吗?
  • 为什么要使用工具生成它们,而其他所有类都是由人编写的?

我可能不能回答所有的问题,但一些背景是:

在Makefile的http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/make/java/nio/Makefile中,他们通过一些预处理程序从相同的模板文件生成不同的java源文件:

...
$(BUF_GEN)/CharBuffer.java: $(X_BUF_TEMPLATE) $(GEN_BUFFER_SH)
    $(prep-target)
    @$(RM) $@.temp
    TYPE=char SRC=$< DST=$@.temp $(GEN_BUFFER_CMD)
    $(MV) $@.temp $@
$(BUF_GEN)/ShortBuffer.java: $(X_BUF_TEMPLATE) $(GEN_BUFFER_SH)
    $(prep-target)
    @$(RM) $@.temp
    TYPE=short SRC=$< DST=$@.temp $(GEN_BUFFER_CMD)
    $(MV) $@.temp $@
...

$(X_BUF_TEMPLATE) 指的事 X-Buffer.java.template, 它是诸如CharbufferShortbuffer等类型缓冲区的源代码。

注意:URL将来可能会改变。也很抱歉引用了Java7。在Java 8中他们已经修改了构建系统,到目前为止我还没有找到相应的makefile。

什么工具生成的这些文件?

GEN_BUFFER_SH / GEN_BUFFER_CMD 最终指向了 genBuffer.sh, 创建了这些文件的脚本是 http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/make/java/nio/genBuffer.sh.

为什么要使用工具生成它们,而其他所有类都是由人类编写的?

对于这种特殊情况,我没有权威的答案,但通常你使用代码生成工具可能由以下情况

  • 如果你需要创建许多相似的类/方法,这些类/方法只在某些细节上有所不同,但非常微妙,因此您不能使用已建立的机制,如泛型或方法参数(可能是这里的情况,因为缓冲区是为不能与泛型一起使用的基本类型生成的)
  • 如果需要从更简单的表示创建复杂的算法(比如从语法生成解析器)。

为什么工具要保持行号不变,为了更容易debugger吗?

我的猜测:是。它将行号保留在堆栈跟踪中,以便它们与模板文件匹配。其他工具比如C预处理器工作原理类似。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VITA 62-2016 英文原版标准,带目录。 VITA 62 defines both the mechanical and electrical requirements for COTS modules intended for creating modular power supplies. The primary focus is to support [VITA 46.0] and [VITA 65] systems with off the shelf modules that are compatible with the VPX specifications. The embedded market has focused primarily on the need to standardize the use of COTS processing add-in modules as part of the VPX suite of standards. Before this standard system designers treated the power supply as a necessary evil, which is often an afterthought. Modular power supply suppliers provided ad hoc solutions that system developers must implement. Providing power for these systems had historically been left up the module vendor, requiring them to define what has, at times, been seen as a form of “black art.” This standard, however, defines a set of rules by which industry power supply vendors can build COTS products, for VPX systems, that will fit into VITA 62 compatible backplanes. Consequently, system developers can have multiple vendors for power supply modules. At this time the focus of this standard is to define modules for VPX systems. In the future this standard may also be extended into other form factors. As part of the work to create VITA 62, a new connector system has been defined, optimized for the needs of VITA 62. This connector mechanically fits within the envelope defined in [VITA 46.0] for 3U and 6U Plug-In modules. This will facilitate the production of backplanes and allow system designers to define where and how many power supply modules are to be used within any given system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值