Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document Java Record parameters?

Tags:

How is one supposed to document Java Record parameters? I am referring to the parameters that end up becoming constructor parameters, class fields.

I tried:

/**  * @param name the name of the animal  * @param age the age of the animal  */ public record Animal(String name, int age) { } 

but IntelliJ IDEA flags @params as an error. I couldn't find an online example of how this is supposed to work. The closest discussion I found is https://bugs.openjdk.java.net/browse/JDK-8225055.

I found some unit tests in the JDK that seem to imply this should be working. Perhaps this is an IDE bug?

I am using OpenJDK 14+36-1461, IDEA 2020.1.

I filed a bug report against IDEA just in case.

like image 304
Gili Avatar asked Apr 26 '20 18:04

Gili


People also ask

How do you declare a record in Java?

To create a record, call its constructor and pass all the field information in it. We can then get the record information using JVM generated getter methods and call any of generated methods.

Can Java records have methods?

Static Variables & Methods As with regular Java classes, we can also include static variables and methods in our records.

How do records work in Java?

A record class declares a sequence of fields, and then the appropriate accessors, constructors, equals , hashCode , and toString methods are created automatically. The fields are final because the class is intended to serve as a simple "data carrier".

What is a record header Java?

The Java™ language provides concise syntax for declaring record classes, whereby the record components are declared in the record header. The list of record components declared in the record header form the record descriptor.

How to create and use Records in Java?

Create and use records To create a record, call its constructor and pass all the field information in it. We can then get the record information using JVM generated getter methods and call any of generated methods. 1.3. Under the hood

How do we read data from a record in Java?

We just read the data and pass it to data processor or validator. Also, records are not replacement for mutable Java beans because a record, by design, is immutable. Use records when a class is intended to hold the data for some time and we want to avoid writing lots of boilerplate code.

How do you define a parameter in Java?

Java Reference Java Keywords. ... Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a String called fname as parameter.

What is record type in Java 15?

Java 15 enhances record type further. It is still a preview feature. Record object have implicit constructor with all the parameters as field variables. Record object have implicit field getter methods for each field variables. Record object have implicit field setter methods for each field variables.


Video Answer


1 Answers

IntelliJ Bug / Missing Feature

Using the in-built JDK tool for javadoc with the version 14-ea and above, I could easily generate Javadoc for a record.

enter image description here

The command used for the same is \

/jdk-14.jdk/.../javadoc --release=14 --enable-preview .../src/main/java/.../CityRecord.java 

So this would certainly be something missing in IntelliJ. (Since the 'Add Javadoc' option also doesn't include the components.)

I must add from the IntelliJ development point of view, of course as a preview feature prioritizing the work dedicated towards it to such an extent is also a call that must be taken carefully.


Update: I could verify that this has been fixed with the latest update from IntelliJ:

IntelliJ IDEA 2020.2.2 Preview (Community Edition) Build #IC-202.7319.5, built on September 1, 2020 Runtime version: 11.0.8+10-b944.31 x86_64 
like image 153
Naman Avatar answered Oct 10 '22 09:10

Naman