Synthetic Class in Java

后端 未结 13 1733
广开言路
广开言路 2020-11-29 17:15

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

相关标签:
13条回答
  • 2020-11-29 17:31

    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.

    0 讨论(0)
  • 2020-11-29 17:32

    Also Synthetic Classes or Dynamic Proxies are used by EasyMock to create implementations of interfaces or abstract classes at runtime.

    http://www.easymock.org/

    0 讨论(0)
  • 2020-11-29 17:35

    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.

    0 讨论(0)
  • 2020-11-29 17:36

    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
    0 讨论(0)
  • 2020-11-29 17:42

    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() {
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 17:42

    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.

    0 讨论(0)
提交回复
热议问题