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.
- 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.
The plugin hooks into JRebel's class-reloading lifecycle.
- Event Listener: It registers a
ClassEventListener. - Reload Detection: When JRebel reloads a class (
EVENT_RELOADED), the listener is triggered. - 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, andSerializeConfig.serializers. - For Fastjson 2.x, it cleans up
ObjectReaderProviderandObjectWriterProvidercaches.
- For Fastjson 1.x, it clears
- 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.
You can build the plugin from the source using Apache Maven.
-
Prerequisites:
- JDK 1.8 or higher
- Apache Maven 3.x
-
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).
To install the plugin for your JRebel agent:
-
Locate the JAR: Find the JAR file you built in the previous step.
-
Add to JRebel Agent: Add the path to the generated JAR file to your application's startup arguments using the
-javaagentparameter 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.jarReplace the paths with the actual locations of your
jrebel.jarand 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.