Skip to content

Commit b4f0c30

Browse files
committed
JDK 24: example of Class File API
1 parent 7cd2669 commit b4f0c30

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

java-24/BasicClass.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.Serializable;
2+
3+
/**
4+
* BasicClass
5+
*/
6+
public class BasicClass implements Serializable {
7+
private String attribute;
8+
private int timestamp;
9+
private boolean active;
10+
11+
public BasicClass() {
12+
}
13+
14+
public BasicClass(String attribute, int timestamp, boolean active) {
15+
this.attribute = attribute;
16+
this.timestamp = timestamp;
17+
this.active = active;
18+
}
19+
20+
public String getAttribute() {
21+
return attribute;
22+
}
23+
24+
public void setAttribute(String attribute) {
25+
this.attribute = attribute;
26+
}
27+
28+
public int getTimestamp() {
29+
return timestamp;
30+
}
31+
32+
public void setTimestamp(int timestamp) {
33+
this.timestamp = timestamp;
34+
}
35+
36+
public boolean isActive() {
37+
return active;
38+
}
39+
40+
public void setActive(boolean active) {
41+
this.active = active;
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.lang.classfile.*;
2+
import java.lang.classfile.constantpool.ClassEntry;
3+
import java.lang.constant.*;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Run: `javac BasicClass.java && java --source 24 --enable-preview ClassFileApiReadingExample.java`
10+
*/
11+
public class ClassFileApiReadingExample {
12+
public static void main(String[] args) throws Exception {
13+
var classBytes = Files.readAllBytes(Paths.get("BasicClass.class"));
14+
15+
// ClassModel is an immutable description of a class file
16+
ClassModel cm = ClassFile.of().parse(classBytes);
17+
18+
// ClasModel is lazy, iterating over it parses the entire class
19+
for (ClassElement ce : cm) {
20+
// possible values: https://download.java.net/java/early_access/jdk24/docs/api/java.base/java/lang/classfile/ClassElement.html
21+
switch (ce) {
22+
case Superclass cn -> System.out.println("Superclass: " + cn.superclassEntry().name().stringValue());
23+
24+
case Interfaces i -> {
25+
var interfaces = i.interfaces().stream()
26+
.map(ClassEntry::name)
27+
.collect(Collectors.joining(","));
28+
System.out.println("Interfaces: " + interfaces);
29+
}
30+
31+
// ClassModel.fields()
32+
case FieldModel fm -> {
33+
var fieldType = fm.fieldTypeSymbol().displayName();
34+
var fieldName = fm.fieldName().stringValue();
35+
System.out.printf("Field: %s %s%n", fieldType, fieldName);
36+
}
37+
38+
// ClassModel.methods()
39+
case MethodModel mm -> {
40+
var symbol = mm.methodTypeSymbol();
41+
var returnType = symbol.returnType().displayName();
42+
var parameters = symbol.parameterList().stream()
43+
.map(ClassDesc::displayName)
44+
.collect(Collectors.joining(","));
45+
var methodName = mm.methodName().stringValue();
46+
System.out.printf("Method: %s %s(%s)%n", returnType, methodName, parameters);
47+
}
48+
default -> System.out.printf("Other: %s%n", ce);
49+
}
50+
}
51+
}
52+
}

java-24/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ To run each example use: `java --enable-preview --source 24 <FileName.java>`
9292
```java
9393
ClassModel cm = ClassFile.of().parse(bytes);
9494
for (ClassElement ce : cm) {
95-
case FieldModel fm -> System.out.println("Field " + mm.methodName().stringValue());
96-
case MethodModel mm -> System.out.println("Method " + mm.methodName().stringValue());
97-
default -> System.out.println("Other " + ce);
95+
switch (ce) {
96+
case FieldModel fm -> System.out.println("Field " + fm.fieldName().stringValue());
97+
case MethodModel mm -> System.out.println("Method " + mm.methodName().stringValue());
98+
default -> System.out.println("Other " + ce);
99+
}
98100
}
99101
```
100102
* `ClassFile` also provides methods to access the elements we need (parsing only the part need to get the methods):
@@ -153,7 +155,7 @@ To run each example use: `java --enable-preview --source 24 <FileName.java>`
153155
* re-preview with no change
154156
* introduced new terminology "simple source file" to indicate a Java file with a implicitly declared class
155157
* **Structured Concurrency**
156-
*
158+
* re-preview with no change
157159
158160
## Links
159161

0 commit comments

Comments
 (0)