optimize code for getting methodmodel of methods with @Select annotation

public static byte[] getClassBytes(Class<?> clazz) throws IOException {
String classPath = clazz.getSimpleName() + ".class"; // Uses simple name for same package
try (InputStream inputStream = clazz.getResourceAsStream(classPath)) {
if (inputStream == null) {
throw new IOException("Class file not found: " + classPath);
}
return inputStream.readAllBytes();
}
}


public static List<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<?> annotation) {
List<Method> selectedMethods = new ArrayList<>();
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(annotation.asSubclass(java.lang.annotation.Annotation.class))) {
selectedMethods.add(method);
}
}
return selectedMethods;
}

void main() throws IOException {
byte[] bytes = getClassBytes(Test.class);
ClassModel cm = ClassFile.of().parse(bytes);
getMethodsAnnotatedWith(Test.class, Select.class).forEach(m -> cm.methods().forEach(mm -> {
if (Objects.equals(mm.methodName().stringValue(), m.getName())) {
mm.code().get().elementList().forEach(System.out::println);
}
}));
}
public static byte[] getClassBytes(Class<?> clazz) throws IOException {
String classPath = clazz.getSimpleName() + ".class"; // Uses simple name for same package
try (InputStream inputStream = clazz.getResourceAsStream(classPath)) {
if (inputStream == null) {
throw new IOException("Class file not found: " + classPath);
}
return inputStream.readAllBytes();
}
}


public static List<Method> getMethodsAnnotatedWith(Class<?> clazz, Class<?> annotation) {
List<Method> selectedMethods = new ArrayList<>();
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(annotation.asSubclass(java.lang.annotation.Annotation.class))) {
selectedMethods.add(method);
}
}
return selectedMethods;
}

void main() throws IOException {
byte[] bytes = getClassBytes(Test.class);
ClassModel cm = ClassFile.of().parse(bytes);
getMethodsAnnotatedWith(Test.class, Select.class).forEach(m -> cm.methods().forEach(mm -> {
if (Objects.equals(mm.methodName().stringValue(), m.getName())) {
mm.code().get().elementList().forEach(System.out::println);
}
}));
}
Im using java24's classfile api to get the string rapresentation of functions, and i want to select only the ones with the @Select annotation. my current implementation uses two loops, one to get the annotated methods and another to check if the name of the method matches with the method model. this O^2 implementation kinda sucks and i couldnt find in the classfile docs a better way to do this
java.lang.classfile (Java SE 24 & JDK 24 [build 35])
declaration: module: java.base, package: java.lang.classfile
1 Reply
JavaBot
JavaBot5d 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. 💤 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?