Java 中的综合类
Java 中的合成类是什么? 为什么要使用它? 我该如何使用它?
What is a synthetic class in Java? Why should it be used? How can I use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java 中的合成类是什么? 为什么要使用它? 我该如何使用它?
What is a synthetic class in Java? Why should it be used? How can I use it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
Java 具有在运行时创建类的能力。 这些类称为综合类或动态代理。
请参阅 http://java.sun.com/j2se/ 1.5.0/docs/guide/reflection/proxy.html 了解更多信息。
其他开源库,例如 CGLIB 和 ASM 还允许您生成合成类,并且比 JRE 提供的库更强大。
合成类由 AOP(面向方面编程)库(例如 Spring AOP 和 AspectJ)以及 ORM 库(例如 Hibernate)使用。
Java has the ability to create classes at runtime. These classes are known as Synthetic Classes or Dynamic Proxies.
See http://java.sun.com/j2se/1.5.0/docs/guide/reflection/proxy.html for more information.
Other open-source libraries, such as CGLIB and ASM also allow you to generate synthetic classes, and are more powerful than the libraries provided with the JRE.
Synthetic classes are used by AOP (Aspect Oriented Programming) libraries such as Spring AOP and AspectJ, as well as ORM libraries such as Hibernate.
好吧,我在谷歌上找到了第一个问题的答案:
这只是一个基本定义,但我在论坛帖子中找到它,但没有解释。 还在寻找更好的...
Well I found the answer to the first question on google:
This is just a basic definition but I found it in a forum thread and there was no explanation. Still looking for a better one...
例如,当您有一个 switch 语句时,java 会创建一个以 $ 开头的变量。 如果您想查看这样的示例,请查看其中包含 switch 语句的类的 java 反射。 当类中的任何位置至少有一个 switch 语句时,您将看到这些变量。
为了回答你的问题,我不相信你能够访问(除了反射之外)合成类。
如果您正在分析一个您一无所知的类(通过反射)并且需要了解该类的非常具体和低级的信息,那么您最终可能会使用与合成类有关的 Java 反射方法。 这里唯一的“用途”是获取有关该类的更多信息,以便在代码中正确使用它。
(如果您这样做,您可能正在构建其他开发人员可以使用的某种框架。)
否则,如果您不使用反射,据我所知,合成类没有实际用途。
For example, When you have a switch statement, java creates a variable that starts with a $. If you want to see an example of this, peek into the java reflection of a class that has a switch statement in it. You will see these variables when you have at least one switch statement anywhere in the class.
To answer your question, I don't believe you are able to access(other than reflection) the synthetic classes.
If you are analyzing a class that you don't know anything about (via reflection) and need to know very specific and low-level things about that class, you may end up using Java reflection methods that have to do with synthetic classes. The only "use" here is get more information about the class in order to use it appropriately in your code.
(If you're doing this, you're probably building a framework of some sorts that other developers could use. )
Otherwise, if you are not using reflection, there are no practical uses of synthetic classes that I know of.
合成类/方法/字段:
这些对于虚拟机来说很重要。 看看下面的代码片段:
synthetic classes / methods / fields:
These things are important for the VM. Have a look at following code snippet:
根据此讨论,尽管语言规范描述了类的“isSynthetic”属性,但它几乎被实现忽略,并且不用于动态代理或匿名类。 合成字段和构造函数用于实现嵌套类(字节码中没有嵌套类的概念,仅在源代码中)。
我认为合成类的概念已经被证明是没有用的,即没有人关心一个类是否是合成的。 对于字段和方法,它可能只用在一个地方:确定在 IDE 类结构视图中显示什么内容 - 您希望正常的方法和字段显示在那里,而不是用于模拟嵌套类的合成方法和字段。 OTOH,您确实希望匿名类出现在那里。
According to this discussion, though the language specification describes an "isSynthetic" proprty for classes, this is pretty much ignored by implementations and not used for either dynamic proxies or anonymous classes. Synthetic fields and constructors are used to implement nested classes (there is not concept of nested classes in byte code, only in source code).
I think that the concept of synthetic classes has simply proven to be not useful, i.e. nobody cares whether a class is synthetic. With fields and methods, it's probably used in exactly one place: to determine what to show in an IDE class structure view - you want normal methods and fields to show up there, but not the synthetic ones used to simulate nested classes. OTOH, you DO want anonymous classes to show up there.
它们是由 JVM 在运行时调用内部类的私有成员以进行调试时创建的。
JVM 在运行时为其执行目的创建的方法、字段、类称为合成
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
http://javapapers.com/core-java/java-synthetic-类方法字段/
They are created by JVM at run time when they invoke private members of inner class for debugging purpose
The methods,fields,class created by JVM during run time for its execution purpose are called Synthetic
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
http://javapapers.com/core-java/java-synthetic-class-method-field/
正如各种答案已经指出的那样,编译器可以生成不直接对应于源代码中的某些内容的各种构造(包括类)。 这些必须标记为合成:
13.1。 二进制的形式
正如 @Holger 在对另一个问题的评论中指出的,此类构造的相关示例是 Class表示方法引用和 lambda 的对象:
输出:
虽然没有明确提及,但它是从
15.27.4。 Lambda 表达式的运行时评估:
以及方法引用的几乎相同的措辞 (15.13.3. 方法引用的运行时评估)。
由于此类在源代码中没有明确提及,因此它必须是合成的。
As various answers have already pointed out, the compiler is allowed to generate various constructs (including classes) that do not directly correspond to something in source code. These have to be marked as synthetic:
13.1. The Form of a Binary
As pointed out by @Holger in a comment to another question, relevant examples for such constructs are the Class objects representing method references and lambdas:
Output:
While this is not explicitly mentioned, it follows from
15.27.4. Run-Time Evaluation of Lambda Expressions:
and the almost identical wording for method references (15.13.3. Run-Time Evaluation of Method References).
As this class is not explicitly mentioned anywhere in source code, it has to be synthetic.
synthetic
类是由 生成的.class
文件Java编译器并且它不存在于源代码中。synthetic
类的用法示例:匿名内部类synthetic
类,它是 java.text.DigitListDigitList$1.java
但它是DigitList.java
中的内部文件它是 Java 编译器逻辑内部生成
.class
文件的机制不,开发人员不直接使用它。
Java编译器使用
synthetic
生成.class
文件,然后JVM读取.class
文件来执行程序逻辑。更多详细信息
合成
类的详细信息synthetic
类在 JDK 中退出A
synthetic
class is a.class
file generated by Java Compiler and it does not exist in the source code.Example usage of
synthetic
class: Anonymous inner classsynthetic
class and it is a anonymous inner class inside java.text.DigitListDigitList$1.java
but it is a inner file inDigitList.java
It is a mechanism inside Java compiler logic to generate the
.class
fileNo, developers do NOT use it directly.
Java compiler use
synthetic
to generate.class
file, and then JVM reads the.class
file to execute the program logic.More details
synthetic
class in detailssynthetic
class exits in JDKEasyMock 还使用合成类或动态代理在运行时创建接口或抽象类的实现。
http://www.easymock.org/
Also Synthetic Classes or Dynamic Proxies are used by EasyMock to create implementations of interfaces or abstract classes at runtime.
http://www.easymock.org/
当 Java 编译器编译某些构造(例如内部类)时,它会创建合成构造; 这些是类、方法、字段和其他在源代码中没有相应构造的构造。
用途:
综合构造使 Java 编译器能够实现新的 Java 语言功能,而无需更改 JVM。 但是,合成构造可能因不同的 Java 编译器实现而异,这意味着 .class 文件也可能因不同的实现而异。
参考:docs.oracle.com
When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code.
Uses:
Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well.
reference:docs.oracle.com
如果我猜对了,合成类是动态生成的,无需给它一个明确的名称。 例如:
这创建了 Thread 的合成子类并重写其 run() 方法,然后实例化并启动它。
If I get it right, a synthetic class is one generated on the fly, without having to give it an explicit name. For example:
This creates a synthetic subclass of Thread and overrides its run() method, then instantiates it and starts it.
合成类不会出现在您的代码中:它是由编译器组成的。
例如,由java中的编译器组成的桥接方法通常是合成的。
使用命令
javap -verbose DateInterval
,可以看到一个桥接方法:这是编译器编造的; 它不会出现在您的代码中。
A synthetic class does not appear in your code: it is made up by compiler.
E.g. A bridge method made up by compiler in java is typically synthetic.
Using the command
javap -verbose DateInterval
, you can see a bridge method:This was made up by compiler; it does not appear in your code.
合成构造是在源代码中没有相应构造的类、方法、字段等。 综合构造使 Java 编译器能够实现新的 Java 语言功能,而无需更改 JVM。 但是,合成构造可能因不同的 Java 编译器实现而异,这意味着 .class 文件也可能因不同的实现而异。
Synthetic constructs are classes, methods, fields etc that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well.