Skip to content

Do you want some inline JVM Bytecode in your Java code? Check this out!

Notifications You must be signed in to change notification settings

Thihup/bytecode-annotation-processor

Repository files navigation

Bytecode annotation processor

This project allows the usage of inline bytecode in the Java programming language. It uses the JASM syntax for the instructions.

One use case would be to use the InvokeDynamic instruction and/or the CONSTANT_Dynamic. Both features are not available in Java programming language.

It is heavily inspired by the Groovy Bytecode AST project.

However, this project uses only supported API.

Example

public class HelloWorld {

    @Bytecode(
        value = """
            getstatic java/lang/System.out:"Ljava/io/PrintStream;";
            ldc "Hello, world!";
            invokevirtual java/io/PrintStream.println:"(Ljava/lang/String;)V";
            return;
            """,
        className="dev.thihup.bytecode.annotation.examples.HelloWorldImpl"
    )
    void myMethod() {
        HelloWorldImpl.invoke();
    }

}

See the tests folder for more examples.

Limitations

  • As we can only generate external classes, it is not possible to create an inner class to store the inline bytecode (the generated class is valid, but you won't be able to reference it in a Java source file);
  • For the same reason as above, it is not possible to access private field of the annotated class.

About

Do you want some inline JVM Bytecode in your Java code? Check this out!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages