java - 当将项目的所有访问修饰符更改为 public 时,字节码中项目的语义是否会发生变化?

标签 java jvm bytecode semantics access-modifiers

假设我有一个可运行的 jar (A.jar),其中包含 .class 文件。我还有另一个可运行的 jar (B.jar),它与 ​​A.jar 相同,除了访问修饰符全部更改为 public(对于字段和方法)。某些语义是否有可能发生改变?它(B.jar)的工作方式与前一个(A.jar)完全相同吗?有什么危险?

编辑示例: A.jar 包含字节码(.class 文件)。我用 asm 库解析它,并将其所有修饰符更改为 public。生成的 jar 是 B.jar 文件。

最佳答案

一个可能的行为更改是私有(private)方法不能重写父类(super class)中的方法。这意味着将子类方法更改为公共(public)方法可能会更改在运行时从虚拟调用调用的方法。

来自 JVM 规范 v11 5.4.5

An instance method mC can override another instance method mA iff all of the following are true:

mC has the same name and descriptor as mA.

mC is not marked ACC_PRIVATE.

One of the following is true:

mA is marked ACC_PUBLIC.

mA is marked ACC_PROTECTED.

mA is marked neither ACC_PUBLIC nor ACC_PROTECTED nor ACC_PRIVATE, and either (a) the declaration of mA appears in the same run-time package as the declaration of mC, or (b) if mA is declared in a class A and mC is declared in a class C, then there exists a method mB declared in a class B such that C is a subclass of B and B is a subclass of A and mC can override mB and mB can override mA.

关于java - 当将项目的所有访问修饰符更改为 public 时,字节码中项目的语义是否会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127275/

相关文章:

java - Byte Buddy - 带 ByteBuddyAgent 的 HotSwap

java - Kafka java消费者SSL握手错误: java. security.cert.CertificateException:不存在主题替代名称

java - 我怎样才能在java中混合2个列表

intellij-idea - "IDE has not been initialized yet"说 CLion/PyCharm/IntelliJ 无法启动

Java hprof问题

java - 为什么 OutOfMemoryError 不能用对象集合重现?

java - 从任意 .class 文件创建 BCEL JavaClass 对象

java - JVM 字节码访问修饰符标志 0x1000(十六进制) "synthetic"何时设置?

java - Selenium Internet Explorer 单击按钮

Java GUI方法错误