Java classfile api example

https://openjdk.org/jeps/457 can anyone provide an example of using this to get the contents of a class method? its relatively new tech and couldn't find it anywhere
6 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @asdru! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
asdru
asdruOP3w ago
import java.io.File;
import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.lang.classfile.FieldModel;
import java.lang.classfile.MethodModel;
import java.nio.file.Files;


void main() {

var classModel = ClassFile.of().parse(getGivenClassBytes("/src/main/java/Person"));
for (var classElement : classModel) {
switch (classElement) {
case MethodModel mm ->
System.out.printf("Method name %s type %s%n", mm.methodName().stringValue(), mm.methodType().stringValue());
case FieldModel fm ->
System.out.printf("Field name %s type %s%n", fm.fieldName().stringValue(), fm.fieldType().stringValue());

default -> {
}
}
}
}

private static byte[] getGivenClassBytes(final String className) {
try {
var path = System.getProperty("user.dir") + "/" + className + ".class";
File file = new File(path);
return Files.readAllBytes(file.toPath());
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage());
}
}
import java.io.File;
import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.lang.classfile.FieldModel;
import java.lang.classfile.MethodModel;
import java.nio.file.Files;


void main() {

var classModel = ClassFile.of().parse(getGivenClassBytes("/src/main/java/Person"));
for (var classElement : classModel) {
switch (classElement) {
case MethodModel mm ->
System.out.printf("Method name %s type %s%n", mm.methodName().stringValue(), mm.methodType().stringValue());
case FieldModel fm ->
System.out.printf("Field name %s type %s%n", fm.fieldName().stringValue(), fm.fieldType().stringValue());

default -> {
}
}
}
}

private static byte[] getGivenClassBytes(final String className) {
try {
var path = System.getProperty("user.dir") + "/" + className + ".class";
File file = new File(path);
return Files.readAllBytes(file.toPath());
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage());
}
}
i got this basic example but i still cant get it to print the contents of the function like
func @"sub" @loc="19:5:file:/.../ExpressionGraphs.java" (%0 : double, %1 : double)double -> {
%2 : Var<double> = var %0 @"a" @loc="19:5";
%3 : Var<double> = var %1 @"b" @loc="19:5";
%4 : double = var.load %2 @loc="21:16";
%5 : double = var.load %3 @loc="21:20";
%6 : double = sub %4 %5 @loc="21:16";
return %6 @loc="21:9";
};
func @"sub" @loc="19:5:file:/.../ExpressionGraphs.java" (%0 : double, %1 : double)double -> {
%2 : Var<double> = var %0 @"a" @loc="19:5";
%3 : Var<double> = var %1 @"b" @loc="19:5";
%4 : double = var.load %2 @loc="21:16";
%5 : double = var.load %3 @loc="21:20";
%6 : double = sub %4 %5 @loc="21:16";
return %6 @loc="21:9";
};
dan1st
dan1st3w ago
Just mm.code()? and then iterate icer it in a foreach loop and finally you can switch over that
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
nothing
nothing3w ago
nice
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?