java bytecode playground

A playground project about Java Bytecode runtime manipulation.

java-bytecode-playground

A playground project about Java Bytecode runtime manipulation. For study purposes only.

Examples

The famous “Hello, World!”

This project exposes an annotation called @MainMethod and it must be applied on classes. When you annotate a class with @MainMethod, this library will generate a main method into the class that prints a message in console. The default message is “Hello, World!”, but you can pass a custom message to annotation that will be printed in console. If the annotated class already contains the main method, an exception will be thrown.

Ex.:

    @MainMethod public class HelloWorld {
    
    }
    @MainMethod(message = "Hi!") public class HelloWorld {
    
    }

Profiling method execution

This project exposes an annotation called @Profiling and it must be applied on methods. When you annotate a method with @Profiling, this library will measure the execution time of the annotated method by getting the current milliseconds at the begining of the method and at the end of method, and then calculates the difference beetwen initial milliseconds and final milliseconds. The result will be printed in console.

Ex.:

    public class Foo {
        
        @Profiling public void bar() throws InterruptedException {
            // Do some heavy operation
            Thread.sleep(10000);
        }
    
    }

How to run?

1- Compile the project and generate jar file:

./gradlew clean assemble

2- Compile your Java source file with the generated jar in classpath:

javac -cp /path/to/jar YourClass.java

3- Run the generated bytecode with the generated jar as a javaanget:

java -javaagent:/path/to/jar YourClass

License

Copyright 2018 Wellington Costa

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.