JVM如何确定类是否“属于”(例如内部类或嵌套类)到另一个类? [英] How can a JVM decide if a class “belongs” (e.g. inner or nested classes) to another class?

查看:500
本文介绍了JVM如何确定类是否“属于”(例如内部类或嵌套类)到另一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解类文件和内部/嵌套类,我想知道以下事情:




  • InnerClasses 属性用于引用'contain''类中的内部/嵌套类,或者它是否在内部/嵌套类中用于引用'container'类? / li>
  • 类文件中的 InnerClasses 属性是否足够?例如。内部/嵌套类必须遵循 $ 的名称吗?或者这只是一个约定?

  • 是否有办法类看起来像JVM的内部/嵌套类,而不设置 InnerClasses 属性,这取决于JLM供应商? (我记得听说IBM的实现对某些部分的要求不太严格。)

  • JVM的类加载机制与Java反射有多少交互?是否可以让JVM不同意Java反射的结果?



我试图在JVM规范中查找它,但didn找不到实际机制的说明。



我只在 InnerClasses Attribute远程连接到我的问题: p>


Java虚拟机目前不检查
的一致性InnerClasses属性与实际表示
类的任何类文件



解决方案

上一个答案的几个补充:


InnerClasses属性用于在包含类中存储包含的内部/嵌套类,或者在内部/嵌套类中用于引用'container'类?


每个编译类的字节码存储在一个单独的.class文件中。实际的内部类不存储在该属性中。正如上一篇文章指出的,该属性只指向编译器在创建字节码时知道的类。


类文件足够吗?例如。内部/嵌套类必须跟随$的名称或者这只是一个约定?



有一种方法使一个类看起来像一个内部/嵌套类到JVM,而不设置InnerClasses属性,这取决于JLM供应商? (我记得听说IBM的实现对某些部分的要求不太严格。)


对于这两个问题,我不确定。但我认为内部/嵌套类的概念是Java语言(因此Java编译器提供)。在字节码中,被声明为正常公共类的类与某个嵌套或内部类之间不应有任何差异。
你可以很容易地尝试一个给定的VM如何处理这样:




  • 创建一个有一些嵌套和内部类的类

  • 编写一个程序,通过从定义类的范围之外的反射来尝试加载和实例化一个内部类。您必须在这里使用反射,因为Java编译器不会允许您实例化不在范围内的嵌套类!












$ b b

JVM的类加载机制与Java反射交互多少?是否有可能让JVM不同意Java反射的结果?


我不明白这最后一个问题。当你说虚拟机和反射应该不同意时,你能解释一下你的意思吗?


I want to understand class files and inner/nested classes a bit better and I'm wondering about the following things:

  • Is the InnerClasses attribute used to refer tothe inner/nested classes in the ´containing´ class or is it used in the inner/nested classes to refer to the ‘container’ class?
  • Is the InnerClasses attribute in class files sufficient? E.g. Do inner/nested classes have to follow the name mangling with $ or is this just a convention?
  • Is there a way to make a class look like an inner/nested class to the JVM without setting the InnerClasses attribute and does this depend on the JLM vendor? (I remember hearing that IBM's implementation had less strict requirements in some parts.)
  • How much does the class loading mechanism of the JVM interact with Java reflection? Would it be possible to make the JVM disagree with the results from Java reflection?

I tried looking it up in the JVM specification but didn't find a description of the actual mechanism.

I only found this sentence in "The InnerClasses Attribute" remotely connected to my question:

The Java virtual machine does not currently check the consistency of the InnerClasses attribute with any class file actually representing a class or interface referenced by the attribute.

解决方案

A few additions to the previous answer:

Is the InnerClasses attribute used to store the contained inner/nested classes in the containing class or is it used in the inner/nested classes to refer to the ‘container’ class?

The bytecode of every compiled class is stored in a separate .class file. The the actual "inner classes" are not stored in that attribute. As the previous post pointed out, that attribute only points to classes that the compiler knew about when creating the bytecode.

Is the InnerClasses attribute in class files sufficient? E.g. Do inner/nested classes have to follow the name mangling with $ or is this just a convention?

Is there a way to make a class look like an inner/nested class to the JVM without setting the InnerClasses attribute and does this depend on the JLM vendor? (I remember hearing that IBM's implementation had less strict requirements in some parts.)

For both questions, I am not certain. But I think the concept of inner/nested classes is something that the Java language (and hence the Java compiler provides). In the bytecode, there should not be any difference between a class that was declared as a normal public class and some nested or inner class. You could easily try out how an given VM handles this like so:

  • Create a class with some nested and inner classes
  • Write a little program that tries to load and instantiate one of the inner classes through reflection from outside the scope of defining class. You must use reflection here, because the Java compiler will not allow you to instantiate a nested class that is not in scope! If you can successfully instantiate the class, that is evidence that internally the VM does not handle nested and normal classes differently.

How much does the class loading mechanism of the JVM interact with Java reflection? Would it be possible to make the JVM disagree with the results from Java reflection?

I don't understand this last question. Could you explain a bit more what you mean when you say the VM and reflection should disagree?

这篇关于JVM如何确定类是否“属于”(例如内部类或嵌套类)到另一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆