Java 中的综合类

发布于 2024-07-11 03:24:26 字数 41 浏览 7 评论 0原文

Java 中的合成类是什么? 为什么要使用它? 我该如何使用它?

What is a synthetic class in Java? Why should it be used? How can I use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

过气美图社 2024-07-18 03:24:26

Java 具有在运行时创建类的能力。 这些类称为综合类或动态代理。

请参阅 http://java.sun.com/j2se/ 1.5.0/docs/guide/reflection/proxy.html 了解更多信息。

其他开源库,例如 CGLIBASM 还允许您生成合成类,并且比 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.

楠木可依 2024-07-18 03:24:26

好吧,我在谷歌上找到了第一个问题的答案:

一个类可以被标记为合成类,如果
它是由编译器生成的,即
是的,它没有出现在源代码中
代码。

这只是一个基本定义,但我在论坛帖子中找到它,但没有解释。 还在寻找更好的...

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...

抠脚大汉 2024-07-18 03:24:26

例如,当您有一个 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.

浅忆 2024-07-18 03:24:26

合成类/方法/字段:

这些对于虚拟机来说很重要。 看看下面的代码片段:

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() {
    }
  }
}

synthetic classes / methods / fields:

These things are important for the VM. Have a look at following code snippet:

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() {
    }
  }
}
泡沫很甜 2024-07-18 03:24:26

根据此讨论,尽管语言规范描述了类的“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.

素衣风尘叹 2024-07-18 03:24:26

它们是由 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/

早茶月光 2024-07-18 03:24:26

正如各种答案已经指出的那样,编译器可以生成不直接对应于源代码中的某些内容的各种构造(包括类)。 这些必须标记为合成:

13.1。 二进制的形式

类或接口的二进制表示还必须包含以下所有内容:
[...]
11. 如果 Java 编译器发出的构造与源代码中显式或隐式声明的构造不对应,则必须将其标记为合成构造,除非发出的构造是类初始化方法(JVMS §2.9)。
[...]

正如 @Holger 在对另一个问题的评论中指出的,此类构造的相关示例是 Class表示方法引用和 lambda 的对象:

System.out.println(((Runnable) System.out::println).getClass().isSynthetic());
System.out.println(((Runnable) () -> {}).getClass().isSynthetic());

输出:

true
true

虽然没有明确提及,但它是从
15.27.4。 Lambda 表达式的运行时评估

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

A binary representation for a class or interface must also contain all of the following:
[...]
11. A construct emitted by a Java compiler must be marked as synthetic if it does not correspond to a construct declared explicitly or implicitly in source code, unless the emitted construct is a class initialization method (JVMS §2.9).
[...]

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:

System.out.println(((Runnable) System.out::println).getClass().isSynthetic());
System.out.println(((Runnable) () -> {}).getClass().isSynthetic());

Output:

true
true

While this is not explicitly mentioned, it follows from
15.27.4. Run-Time Evaluation of Lambda Expressions:

The value of a lambda expression is a reference to an instance of a class with the following properties: [...]

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.

梦里°也失望 2024-07-18 03:24:26

什么是 Java 中的合成类?

synthetic 类是由 生成的 .class 文件Java编译器并且它不存在于源代码中。

synthetic 类的用法示例:匿名内部类

为什么要使用它?

它是 Java 编译器逻辑内部生成 .class 文件的机制

我该如何使用它?

不,开发人员直接使用它。

Java编译器使用synthetic生成.class文件,然后JVM读取.class文件来执行程序逻辑。

更多详细信息

  • 这篇文章解释了合成类的详细信息
  • 链接列出了所有synthetic 类在 JDK 中退出

What is a synthetic class in Java?

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 class

  • java.text.DigitList$1 is a synthetic class and it is a anonymous inner class inside java.text.DigitList
  • And there is no source code file named like DigitList$1.java but it is a inner file in DigitList.java

Why should it be used?

It is a mechanism inside Java compiler logic to generate the .class file

How can I use it?

No, 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

  • This article explains synthetic class in details
  • This link lists all synthetic class exits in JDK
烟花易冷人易散 2024-07-18 03:24:26

EasyMock 还使用合成类或动态代理在运行时创建接口或抽象类的实现。

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/

白芷 2024-07-18 03:24:26

当 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

染年凉城似染瑾 2024-07-18 03:24:26

如果我猜对了,合成类是动态生成的,无需给它一个明确的名称。 例如:

//...
Thread myThread = new Thread() {
         public void run() {
           // do something ...
         }
       };
myThread.start();
//...

这创建了 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:

//...
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.

翻了热茶 2024-07-18 03:24:26

合成类不会出现在您的代码中:它是由编译器组成的。
例如,由java中的编译器组成的桥接方法通常是合成的。

public class Pair<T> {
    private T first;
    private T second;
    public void setSecond(T newValue) {
        second = newValue;
    }
}


public class DateInterval extends Pair<String> {
    public void setSecond(String second) {
        System.out.println("OK sub");
    }

    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        DateInterval interval = new DateInterval();
        Pair pair = interval;
        pair.setSecond("string1");
    }
}

使用命令javap -verbose DateInterval,可以看到一个桥接方法:

public void setSecond(java.lang.Object);
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC

这是编译器编造的; 它不会出现在您的代码中。

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.

public class Pair<T> {
    private T first;
    private T second;
    public void setSecond(T newValue) {
        second = newValue;
    }
}


public class DateInterval extends Pair<String> {
    public void setSecond(String second) {
        System.out.println("OK sub");
    }

    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        DateInterval interval = new DateInterval();
        Pair pair = interval;
        pair.setSecond("string1");
    }
}

Using the command javap -verbose DateInterval, you can see a bridge method:

public void setSecond(java.lang.Object);
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC

This was made up by compiler; it does not appear in your code.

明月夜 2024-07-18 03:24:26

合成构造是在源代码中没有相应构造的类、方法、字段等。 综合构造使 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文