Javassist Documentation

repository·master·Indexed 26 days ago

https://github.com/jboss-javassist/javassist

A Java bytecode engineering toolkit for manipulating Java bytecode. Javassist allows programs to define new classes at runtime and modify existing class files. It provides a source-level API for editing class files using Java language vocabulary and a bytecode-level API for direct editing, including utilities like the Bytecode class for producing bytecode sequences, CodeAnalyzer for computing max stack size, and ExceptionTable for managing exception handlers.

Tokens
4.5K
Snippets
4
Records
40
Agent score
88%

What's inside Javassist

  1. Overview of Javassist

    master

    Javassist (JAVA programming ASSISTant) is a Java bytecode engineering toolkit used for manipulating Java bytecode. It allows Java programs to define new classes at runtime and modify existing class files when the JVM loads them.

    Javassist provides two distinct API levels:

    1. Source-level API: Allows editing class files using standard Java language vocabulary without needing knowledge of bytecode specifications. You can specify inserted bytecode as source text, which Javassist compiles on the fly.
    2. Bytecode-level API: Allows for direct editing of class files, similar to other bytecode editors.
  2. Run Javassist program examples using Apache Ant

    master

    If you have Apache Ant installed, you can run all sample tasks at once using the sample-all target.

    Note: All programs are for demonstration only and are not intended for production use. JDK 1.4 or later is required.

    % ant sample-all
  3. Use the Bytecode class to produce bytecode sequences

    master

    The Bytecode class is a utility for producing a bytecode sequence. It acts as an unbounded array containing bytecode and is used to build the CodeAttribute for a class. You can append various instructions (opcodes), constants, and method calls to the sequence.

    To use it, construct a Bytecode object with a ConstPool, append instructions using the provided add... methods, and finally convert it to a CodeAttribute using toCodeAttribute().

    ConstPool cp = ...;    // constant pool table
    Bytecode b = new Bytecode(cp, 1, 0);
    b.addIconst(3);
    b.addReturn(CtClass.intType);
    CodeAttribute ca = b.toCodeAttribute();
  4. Access fields and behaviors of a CtClass

    master

    To interact with the members of a class, use these methods:

    • getFields(): Returns an array of all accessible fields (including inherited ones).
    • getDeclaredFields(): Returns an array of fields declared specifically in this class.
    • getField(String name, String desc): Retrieves a specific field by name and descriptor.
    • getConstructors(): Returns an array of public constructors.
    • getDeclaredBehaviors(): Returns an array containing both constructors and methods declared in this class.
  5. Inspect ExceptionTable entries

    master

    Use the following methods to retrieve information about entries in the ExceptionTable by their index (nth):

    • size(): Returns the number of entries in the table.
    • startPc(int nth): Returns the start program counter of the $n$-th entry.
    • endPc(int nth): Returns the end program counter of the $n$-th entry.
    • handlerPc(int nth): Returns the handler program counter of the $n$-th entry.
    • catchType(int nth): Returns an index into the constant pool table, or zero if the handler is for all exceptions.
  6. Initialize a Bytecode object

    master

    You can initialize a Bytecode object in two ways:

    1. With stack and local variable limits: Specify the initial max_stack and max_locals.
    2. With only a constant pool: Initializes max_stack and max_locals to zero.

    Note that max_stack may be automatically updated when instructions are appended via addOpcode(), but max_locals usually needs to be set explicitly or via setMaxLocals(boolean isStatic, CtClass[] params, int locals).