Files
javademos/java28-features/src/StrictFields.java
T

24 lines
990 B
Java

// JEP 539: strict field initialization is a JVM feature; javac uses it for value classes.
// Compile this file with --enable-preview, then let it read its own class files.
import java.lang.classfile.ClassFile;
import java.nio.file.Files;
import java.nio.file.Path;
public class StrictFields {
static final int ACC_STRICT_INIT = 0x0800;
value record Point(int x, int y) {}
record Plain(int x, int y) {}
public static void main(String[] args) throws Exception {
for (String name : new String[] {"StrictFields$Point", "StrictFields$Plain"}) {
var model = ClassFile.of().parse(Files.readAllBytes(Path.of(args[0], name + ".class")));
for (var field : model.fields()) {
int mask = field.flags().flagsMask();
System.out.printf("%-20s %s flags=0x%04x strictly-initialized=%b%n",
name, field.fieldName().stringValue(), mask, (mask & ACC_STRICT_INIT) != 0);
}
}
}
}