Giter Club home page Giter Club logo

class-file-api's Introduction

Class File API

JEP 457 Class-File API is a preview API to provide a standard API for parsing, generating, and transforming Java class files.

This repository demonstrates how to create a class that has a field, invokes a method, and prints a message, and also how to parse it with the said API.

Generating

var className = Info.CLASS_NAME.getClassName();
ClassFile.of().buildTo(Path.of(STR."\{className}.class"), of(className),
classBuilder -> classBuilder
.withField("myField", of("java.lang.String"), ACC_PRIVATE)
.withMethodBody("hello", MethodTypeDesc.of(CD_void), ACC_STATIC,
codeBuilder -> codeBuilder
.getstatic(of("java.lang.System"), "out", of("java.io.PrintStream"))
.ldc("Hello, this class is generated by Class-File API which is a preview API to provide a standard API for parsing, generating, and transforming Java class files.")
.invokevirtual(of("java.io.PrintStream"), "println",
MethodTypeDesc.of(CD_void, of("java.lang.Object")))
.return_())
.withMethodBody("main", MethodTypeDesc.of(CD_void, of("java.lang.String").arrayType()), ACC_PUBLIC | ACC_STATIC,
codeBuilder -> codeBuilder
.invokeInstruction(Opcode.INVOKESTATIC, of(className), "hello", MethodTypeDesc.of(CD_void), false)
.return_()));
}

Parsing

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 -> {
}
}
}

Build and run

sh run.sh

class-file-api's People

Contributors

hakdogan avatar

Stargazers

İbrahim ATAY avatar Murat Dindar avatar Mevlüt Atila Güneş avatar Adem Uğurlugelen avatar Ali Burak Bozan avatar Süeda avatar Merve Gündoğmuş avatar

Watchers

 avatar  avatar

Forkers

murodin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.