What is Java Synthetic Method (ACC_SYNTHETIC)? with the example of AbstractPlainSocketImpl.lambda$getInputStream$0() in Java package java.net

A Java method in .class file marked with the ACC_SYNTHETIC flag to indicate that it was generated by Java compiler and does not appear in source code.

Binary Code is always the best document. So let's inspect an example of such case: the Java method AbstractPlainSocketImpl.lambda$getInputStream$0() in Java package java.net is tagged as synthetic in AbstractPlainSocketImpl.class.

The UML generated form the AbstractExecutorService.class file indicates it contains a method named lambda$getInputStream$0(), and the detailed inspection shows this method is generated by compiler, since its "Access Flags" contains the value synthetic, which means this field "not present in the source code".

UML Diagram generated from AbstractExecutorService.class
Basic Information of Method lambda$getInputStream$0()
So why Java compiler is generating this method? Let's continue to inspect the method AbstractPlainSocketImpl.lambda$getInputStream$0().

Decoded byte code of Method lambda$getInputStream$0()

So here is the source code of the AbstractPlainSocketImpl.getInputStream()
method:
  • The row 596 is the lambda expression which generated from
  • We can see that the decoded byte code logic of lambda$getInputStream$0() matches the following lambda expression

589 protected synchronized InputStream getInputStream() throws IOException {
590     synchronized (fdLock) {
591         if (isClosedOrPending())
592             throw new IOException("Socket Closed");
593         if (shut_rd)
594             throw new IOException("Socket input is shutdown");
595         if (socketInputStream == null) {
596             PrivilegedExceptionAction pa = () -> new SocketInputStream(this);
597             try {
598                 socketInputStream = AccessController.doPrivileged(pa);
599             } catch (PrivilegedActionException e) {
600                 throw (IOException) e.getCause();
601             }
602         }
603     }
604     return socketInputStream;
605 }


Comments

Popular posts from this blog

What is Java Bridge Method (ACC_BRIDGE)?

Evolution of Java Dependency Diagrams, from Java 7 to Java 13

What is Java Synthetic Class (ACC_SYNTHETIC)? with the example of BoundMethodHandle$1.class in Java package java.lang.invoke