Feat: add include/exclude filters for JuiceRemapper

- add thread-safe include/exclude sets to Remapper; shouldRemap matches
  exact name, package prefix (trailing '/'), or nested class ('\$' after
  the prefix), with exclude taking priority
- remapping stays OFF by default: an empty include set remaps nothing;
  it is only enabled once addInclude is called (no config include)
- implement addInclude/addExclude/removeInclude/removeExclude/
  clearIncludes/clearExcludes JNI bindings
- apply shouldRemap filter in JuiceRemapperService::onClassFileLoadHook
This commit is contained in:
xiaozhou233 committed 2026-08-18 16:41:55 +08:00
1 parent 4bed99f417
commit 6fe467bccf
3 files changed
+113 -18

No files matched your search

+67
View File
@@ -150,12 +150,79 @@ public:
void setRemapClass(jclass clazz) { remapClass_ = clazz; }
void setRemapMethodId(jmethodID methodId) { remapMethodId_ = methodId; }
// ---- Include/Exclude filters ----
void addInclude(std::string name) {
std::lock_guard<std::mutex> lock(filterMutex_);
includes_.insert(std::move(name));
}
void removeInclude(const std::string& name) {
std::lock_guard<std::mutex> lock(filterMutex_);
includes_.erase(name);
}
void clearIncludes() {
std::lock_guard<std::mutex> lock(filterMutex_);
includes_.clear();
}
void addExclude(std::string name) {
std::lock_guard<std::mutex> lock(filterMutex_);
excludes_.insert(std::move(name));
}
void removeExclude(const std::string& name) {
std::lock_guard<std::mutex> lock(filterMutex_);
excludes_.erase(name);
}
void clearExcludes() {
std::lock_guard<std::mutex> lock(filterMutex_);
excludes_.clear();
}
// A class should be remapped when:
// 1. its name is not excluded, and
// 2. its name matches an include filter.
// Remapping is OFF by default: an empty include set means nothing is
// remapped. To enable remapping for a package, add it as an include
// (e.g. "com/example/" matches the whole package).
//
// Matching rules (applied to both include and exclude):
// - exact internal name match
// - package prefix (ends with '/', e.g. "java/lang/")
// - class prefix + nested classes (e.g. "java/lang/Shutdown"
// also excludes "java/lang/Shutdown$Lock")
bool shouldRemap(const std::string& name) const {
std::lock_guard<std::mutex> lock(filterMutex_);
for (const auto& exc : excludes_) {
if (name == exc) return false;
if (exc.back() == '/' && name.rfind(exc, 0) == 0) return false;
if (name.size() > exc.size() &&
name.rfind(exc, 0) == 0 && name[exc.size()] == '$') return false;
}
for (const auto& inc : includes_) {
if (name == inc) return true;
if (inc.back() == '/' && name.rfind(inc, 0) == 0) return true;
if (name.size() > inc.size() &&
name.rfind(inc, 0) == 0 && name[inc.size()] == '$') return true;
}
return false;
}
private:
Remapper() = default;
bool initialized_ = false;
jclass remapClass_ = nullptr; // global ref
jmethodID remapMethodId_ = nullptr; // static method id
mutable std::mutex filterMutex_;
std::unordered_set<std::string> includes_;
std::unordered_set<std::string> excludes_;
};
}
+40 -16
View File
@@ -72,45 +72,69 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_initN
}
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addInclude
(JNIEnv *, jclass, jstring) {
// TODO: implement
spdlog::warn("JuiceRemapper::addInclude");
(JNIEnv *env, jclass, jstring name) {
if (!name) return JNI_FALSE;
const char* utf = env->GetStringUTFChars(name, nullptr);
if (!utf) return JNI_FALSE;
std::string s(utf);
env->ReleaseStringUTFChars(name, utf);
JuiceAgent::Services::Remapper::getInstance().addInclude(std::move(s));
spdlog::debug("[JuiceRemapper JNI] addInclude");
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addExclude
(JNIEnv *, jclass, jstring) {
// TODO: implement
spdlog::warn("JuiceRemapper::addExclude");
(JNIEnv *env, jclass, jstring name) {
if (!name) return JNI_FALSE;
const char* utf = env->GetStringUTFChars(name, nullptr);
if (!utf) return JNI_FALSE;
std::string s(utf);
env->ReleaseStringUTFChars(name, utf);
JuiceAgent::Services::Remapper::getInstance().addExclude(std::move(s));
spdlog::debug("[JuiceRemapper JNI] addExclude");
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeInclude
(JNIEnv *, jclass, jstring) {
// TODO: implement
spdlog::warn("JuiceRemapper::removeInclude");
(JNIEnv *env, jclass, jstring name) {
if (!name) return JNI_FALSE;
const char* utf = env->GetStringUTFChars(name, nullptr);
if (!utf) return JNI_FALSE;
std::string s(utf);
env->ReleaseStringUTFChars(name, utf);
JuiceAgent::Services::Remapper::getInstance().removeInclude(s);
spdlog::debug("[JuiceRemapper JNI] removeInclude");
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeExclude
(JNIEnv *, jclass, jstring){
// TODO: implement
spdlog::warn("JuiceRemapper::removeExclude");
(JNIEnv *env, jclass, jstring name){
if (!name) return JNI_FALSE;
const char* utf = env->GetStringUTFChars(name, nullptr);
if (!utf) return JNI_FALSE;
std::string s(utf);
env->ReleaseStringUTFChars(name, utf);
JuiceAgent::Services::Remapper::getInstance().removeExclude(s);
spdlog::debug("[JuiceRemapper JNI] removeExclude");
return JNI_TRUE;
}
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearIncludes
(JNIEnv *, jclass) {
// TODO: implement
spdlog::warn("JuiceRemapper::clearIncludes");
JuiceAgent::Services::Remapper::getInstance().clearIncludes();
spdlog::debug("[JuiceRemapper JNI] clearIncludes");
}
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearExcludes
(JNIEnv *, jclass) {
// TODO: implement
spdlog::warn("JuiceRemapper::clearExcludes");
JuiceAgent::Services::Remapper::getInstance().clearExcludes();
spdlog::debug("[JuiceRemapper JNI] clearExcludes");
}
@@ -48,7 +48,7 @@ private:
return;
}
spdlog::debug("[JuiceRemapperService] Enabled");
spdlog::debug("[JuiceRemapperService] Enabled. Waiting for addInclude to start remapping");
}
void onClassFileLoadHook(const EventClassFileLoadHook& e) {
@@ -64,7 +64,11 @@ private:
if (std::string_view(e.name).starts_with("cn/xiaozhou233/juiceremapper/"))
return;
// TODO: Include/Exclude check
// Include/Exclude filter check
if (!remapper.shouldRemap(e.name)) {
spdlog::trace("[JuiceRemapperService] Skipped by filter: {}", e.name);
return;
}
jclass remapClass = remapper.getRemapClass();
jmethodID remapMethod = remapper.getRemapMethodId();