diff --git a/src/main/java/org/jugistanbul/Info.java b/src/main/java/org/jugistanbul/Info.java new file mode 100644 index 0000000..5ef0dc7 --- /dev/null +++ b/src/main/java/org/jugistanbul/Info.java @@ -0,0 +1,20 @@ +package org.jugistanbul; + +/** + * @author hakdogan (huseyin.akdogan@patikaglobal.com) + * Created on 31.03.2024 + ***/ +public enum Info { + + CLASS_NAME("HelloJEP457"); + + private String className; + + Info(String className){ + this.className = className; + } + + public String getClassName() { + return className; + } +} diff --git a/src/main/java/org/jugistanbul/ParseClass.java b/src/main/java/org/jugistanbul/ParseClass.java new file mode 100644 index 0000000..c8b7d1c --- /dev/null +++ b/src/main/java/org/jugistanbul/ParseClass.java @@ -0,0 +1,38 @@ +package org.jugistanbul; + +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; + +/** + * @author hakdogan (huseyin.akdogan@patikaglobal.com) + * Created on 31.03.2024 + ***/ +public class ParseClass { + + public static void main(String[] args) { + + var classModel = ClassFile.of().parse(getGivenClassBytes(Info.CLASS_NAME.getClassName())); + 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 = STR."\{System.getProperty("user.dir")}/\{className}.class"; + File file = new File(path); + return Files.readAllBytes(file.toPath()); + } catch (IOException ioe){ + throw new RuntimeException(ioe.getMessage()); + } + } +}