Return to site

☕1️⃣8️⃣ JDK18 NEWS: Code Snippets in Java API Documentation #JEP413

· java

A new JavaDoc tag '@𝐬𝐧𝐢𝐩𝐩𝐞𝐭' is available in JDK18.

It can be used to define a code fragment.

It is more flexible than the '@𝐜𝐨𝐝𝐞' existing tag, in the way that it does not require to escape special characters.

'@𝐬𝐧𝐢𝐩𝐩𝐞𝐭'  also allows to include code fragments from external files.

𝐄𝐱𝐚𝐦𝐩𝐥𝐞:

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet :
 * if (v.isPresent()) {
 *    System.out.println("v: " + v.get());
 * }
 * }
 */

 

𝐄𝐱𝐚𝐦𝐩𝐥𝐞 𝐰𝐢𝐭𝐡 𝐞𝐱𝐭𝐞𝐫𝐧𝐚𝐥 𝐟𝐢𝐥𝐞𝐬 𝐢𝐧𝐜𝐥𝐮𝐬𝐢𝐨𝐧:

/**
 * The following code shows how to use {@code Optional.isPresent}:
 * {@snippet file="ShowOptional.java" region="example"}
 */

 

public class ShowOptional {
  void show(Optional<String> v) {
    // @start region="example"
    if (v.isPresent()) {
      System.out.println("v: " + v.get());
    }
    // @end
  }
}

Those external files can be defined in the Unit tests