Skip to content

MysterionRise/java-asm-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-asm-example

Java 25 ASM 9.9.1 Build License: Apache 2.0

A self-modifying bytecode demo using the ASM library. The program computes a trigonometric expression, then rewrites its own .class file with an equivalent implementation — so the next run executes different bytecode.

Prerequisites

  • Java 25+
  • Maven 3.6+

Build & Run

mvn clean package
java -jar target/java-asm-example-1.0-SNAPSHOT.jar 1.0 2.0

Run it again to see the rewritten bytecode in action:

java -jar target/java-asm-example-1.0-SNAPSHOT.jar 1.0 2.0

How It Works

SinusCalculation

Entry point. Computes 2*sin((x+y)/2)*cos((x-y)/2) with the original calculation() method, then uses ASM to read its own compiled class file and rewrite the calculation method's bytecode with a different (but mathematically equivalent) implementation. The rewritten class is saved back to target/classes/SinusCalculation.class.

SinusVisitor

A ClassVisitor that intercepts the calculation method and randomly replaces its bytecode with one of two equivalent trigonometric forms:

  • sin(x) + sin(y)
  • 2*sin((x+y)/2)*cos((x-y)/2)

License

Apache License 2.0

About

Self-modifying bytecode demo using Java ASM — rewrites its own .class file at runtime

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages