Skip to content

Jonkimi/fastjson-jrebel-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fastjson JRebel Plugin

A JRebel plugin to make class reloading seamless when using the Fastjson serialization library.

This plugin clears Fastjson's internal caches when a class is reloaded by JRebel. This prevents ClassCastException and other serialization/deserialization errors that can occur when a class's structure (e.g., adding/removing fields) is changed at runtime.

Features

  • Prevents Hot-Reload Errors: Solves common serialization issues when hot-reloading Java classes that are processed by Fastjson.
  • Broad Compatibility: Supports both major versions of Fastjson:
    • Fastjson 1.x
    • Fastjson 2.x
  • Automatic Detection: The plugin automatically detects which version of Fastjson your project is using.
  • Disables ASM: To ensure stability during reloads, the plugin programmatically disables Fastjson's ASM usage, which can interfere with JRebel's class modifications.

How It Works

The plugin hooks into JRebel's class-reloading lifecycle.

  1. Event Listener: It registers a ClassEventListener.
  2. Reload Detection: When JRebel reloads a class (EVENT_RELOADED), the listener is triggered.
  3. Cache Clearing: The plugin then uses Java Reflection to access and clear the internal caches within Fastjson where it stores class metadata, serializers, and deserializers.
    • For Fastjson 1.x, it clears TypeUtils.mappings, ParserConfig.deserializers, and SerializeConfig.serializers.
    • For Fastjson 2.x, it cleans up ObjectReaderProvider and ObjectWriterProvider caches.
  4. ASM Deactivation: On startup, the plugin disables the ASM-based field access and object creation in Fastjson, forcing it to use standard reflection, which is more compatible with JRebel's bytecode manipulation.

This ensures that the next time Fastjson needs to serialize or deserialize the modified class, it will re-analyze it and use the new structure, avoiding errors from stale cache data.

Building the Plugin

You can build the plugin from the source using Apache Maven.

  1. Prerequisites:

    • JDK 1.8 or higher
    • Apache Maven 3.x
  2. Build Command: Navigate to the project's root directory and run:

    mvn clean package

    This will produce a JAR file in the target/ directory (e.g., fastjson-jrebel-plugin-1.0-SNAPSHOT.jar).

Installation

To install the plugin for your JRebel agent:

  1. Locate the JAR: Find the JAR file you built in the previous step.

  2. Add to JRebel Agent: Add the path to the generated JAR file to your application's startup arguments using the -javaagent parameter for JRebel. You can add it as a plugin by pointing to its location.

    For example, if you are using IntelliJ IDEA, you can add it in the VM options for your run configuration:

    -javaagent:/path/to/jrebel.jar
    -Drebel.plugins=/path/to/your/fastjson-jrebel-plugin-1.0-SNAPSHOT.jar
    

    Replace the paths with the actual locations of your jrebel.jar and the plugin JAR.

Now, when you run your application with the JRebel agent, the Fastjson plugin will be loaded, and you can safely reload classes without restarting the application.

About

jrebel plugin for fastjson

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages