Java 中的合成类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/399546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Synthetic Class in Java
提问by andHapp
What is a synthetic class in Java? Why should it be used? How can I use it?
Java 中的合成类是什么?为什么要使用它?我怎样才能使用它?
采纳答案by Milhous
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.
例如,当您有一个 switch 语句时,java 会创建一个以 $ 开头的变量。如果您想看到这样的示例,请查看其中包含 switch 语句的类的 Java 反射。当您在类中的任何地方至少有一个 switch 语句时,您就会看到这些变量。
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.
如果您正在分析一个您(通过反射)一无所知的类,并且需要了解有关该类的非常具体和低级的事情,那么您最终可能会使用与合成类有关的 Java 反射方法。此处唯一的“用途”是获取有关该类的更多信息,以便在您的代码中适当地使用它。
(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.
否则,如果您不使用反射,那么我所知道的合成类就没有实际用途。
回答by andHapp
Well I found the answer to the first question on google:
好吧,我在谷歌上找到了第一个问题的答案:
A class may be marked as synthetic if it is generated by the compiler, that is, it does not appear in the source code.
如果一个类是由编译器生成的,即它没有出现在源代码中,则它可能被标记为合成类。
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...
这只是一个基本定义,但我在论坛帖子中找到了它,没有任何解释。仍在寻找更好的...
回答by Andrew Newdigate
Java has the ability to create classes at runtime. These classes are known as Synthetic Classes or Dynamic Proxies.
Java 具有在运行时创建类的能力。这些类被称为合成类或动态代理。
See http://java.sun.com/j2se/1.5.0/docs/guide/reflection/proxy.htmlfor more information.
有关详细信息,请参阅http://java.sun.com/j2se/1.5.0/docs/guide/reflection/proxy.html。
Other open-source libraries, such as CGLIBand ASMalso allow you to generate synthetic classes, and are more powerful than the libraries provided with the JRE.
其他开源库(例如CGLIB和ASM)也允许您生成合成类,并且比 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.
合成类由 AOP(面向方面的编程)库(例如 Spring AOP 和 AspectJ)以及 ORM 库(例如 Hibernate)使用。
回答by user2427
Also Synthetic Classes or Dynamic Proxies are used by EasyMock to create implementations of interfaces or abstract classes at runtime.
EasyMock 还使用合成类或动态代理在运行时创建接口或抽象类的实现。
回答by Michael Borgwardt
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).
根据这个讨论,虽然语言规范描述了类的“isSynthetic”属性,但实现几乎忽略了这一点,并且不用于动态代理或匿名类。合成字段和构造函数用于实现嵌套类(字节码中没有嵌套类的概念,只有在源代码中)。
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.
我认为合成类的概念已经被证明是没有用的,即没有人关心一个类是否是合成的。对于字段和方法,它可能正好用在一个地方:确定在 IDE 类结构视图中显示什么 - 您希望在那里显示普通方法和字段,而不是用于模拟嵌套类的合成方法和字段。OTOH,您确实希望匿名类出现在那里。
回答by Milhous
synthetic classes / methods / fields:
合成类/方法/字段:
These things are important for the VM. Have a look at following code snippet:
这些东西对 VM 很重要。看看下面的代码片段:
class MyOuter {
private MyInner inner;
void createInner() {
// The Compiler has to create a synthetic method
// to construct a new MyInner because the constructor
// is private.
// --> synthetic "constructor" method
inner = new MyInner();
// The Compiler has to create a synthetic method
// to doSomething on MyInner object because this
// method is private.
// --> synthetic "doSomething" method
inner.doSomething();
}
private class MyInner {
// the inner class holds a syntetic ref_pointer to
// the outer "parent" class
// --> synthetic field
private MyInner() {
}
private void doSomething() {
}
}
}
回答by Ivan
If I get it right, a synthetic class is one generated on the fly, without having to give it an explicit name. For example:
如果我猜对了,合成类是动态生成的,而不必给它一个明确的名称。例如:
//...
Thread myThread = new Thread() {
public void run() {
// do something ...
}
};
myThread.start();
//...
This creates a synthetic subclass of Thread and overrides its run() method, then instantiates it and starts it.
这将创建 Thread 的合成子类并覆盖其 run() 方法,然后实例化它并启动它。
回答by anavaras lamurep
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
当 Java 编译器编译某些构造(例如内部类)时,它会创建合成构造;这些是在源代码中没有相应构造的类、方法、字段和其他构造。
用途:合成构造使 Java 编译器能够在不更改 JVM 的情况下实现新的 Java 语言功能。但是,不同的 Java 编译器实现之间的合成构造可能会有所不同,这意味着 .class 文件也可能因不同的实现而有所不同。
参考:docs.oracle.com
回答by sathis
They are created by JVM at run time when they invoke private members of inner class for debugging purpose
它们是由 JVM 在运行时为调试目的调用内部类的私有成员时创建的
The methods,fields,class created by JVM during run time for its execution purpose are called Synthetic
JVM 在运行时为其执行目的创建的方法、字段、类称为 Synthetic
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
http://javapapers.com/core-java/java-synthetic-class-method-field/
http://javapapers.com/core-java/java-synthetic-class-method-field/
回答by Anoop Dixith
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.
合成构造是在源代码中没有相应构造的类、方法、字段等。合成构造使 Java 编译器能够在不更改 JVM 的情况下实现新的 Java 语言功能。但是,不同的 Java 编译器实现之间的合成构造可能会有所不同,这意味着 .class 文件也可能因不同的实现而有所不同。