From f603f599d3c318f6e81fa17b4380e9103700f1d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Akdo=C4=9Fan?= Date: Sun, 31 Mar 2024 17:32:48 +0300 Subject: [PATCH] parser added --- src/main/java/org/jugistanbul/Info.java | 20 ++++++++++ src/main/java/org/jugistanbul/ParseClass.java | 38 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/main/java/org/jugistanbul/Info.java create mode 100644 src/main/java/org/jugistanbul/ParseClass.java 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()); + } + } +}