-
Notifications
You must be signed in to change notification settings - Fork 1
Optimize casting and fix local class reference leaks; refactor Env
API; add Monitor
, add docs, add is_same_object
, fix ReferenceType
generation
#7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Sorry, the wrong cast between object arrays with items of different class is not prevented by the current cast checker. I will check the previous implementation based on |
I should have realized the bug earlier: static OnceLock usage in trait impl for generics. My current workaround is a bit ugly: // NOTE: This is a performance compromise for returning `&'static JClass`,
// still faster than non-cached `require_class`.
static OBJ_ARR_CLASSES: LazyLock<RwLock<HashMap<String, &'static JClass>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
unsafe impl<T: ReferenceType, E: ThrowableType> ReferenceType for ObjectArray<T, E> {
fn jni_get_class(env: Env) -> &'static JClass {
Self::static_with_jni_type(|t| {
let class_map_reader = OBJ_ARR_CLASSES.read().unwrap();
if let Some(&class) = class_map_reader.get(t) {
class
} else {
drop(class_map_reader);
let class: &'static JClass = Box::leak(Box::new(unsafe { env.require_class(t) }));
let _ = OBJ_ARR_CLASSES.write().unwrap().insert(t.to_string(), class);
class
}
})
}
} |
I'm almost finished. All clippy warnings are fixed (Added safety descriptions for some functions, and the rest are marked with I realized that |
…Type` generation
Added Besides, I made Instantiatable static classclass Test {
static int staticNum = 0;
public static void main(String[] args) {
// static class can be used as an non-static class
StaticClassTest test1 = new StaticClassTest();
test1.num = 7;
StaticClassTest test2 = new StaticClassTest();
test2.num = 9;
System.out.println("test1.num: " + test1.num);
// non-static field of the static class cannot be accessed non-statically
// System.out.println("Wrong: " + StaticClassTest.num);
// the static field value of the nested static class can be modified
System.out.println("StaticClassTest.staticNum: " + StaticClassTest.staticNum);
StaticClassTest.staticNum = 2;
System.out.println("StaticClassTest.staticNum (modified): " + StaticClassTest.staticNum);
// the static field value of the nested non-static class can not be modified (must be final)
System.out.println("ClassTest.staticFinalNum: " + ClassTest.staticFinalNum);
}
public void modifyStaticNonstatically() { // compiles
Test.staticNum = 4;
StaticClassTest.staticNum = 4;
}
public static class StaticClassTest {
public int num = 3;
public static int staticNum = 1;
}
public class ClassTest {
public int num = 3;
public static final int staticFinalNum = 1; // failed to compile without `final`
}
}
class ThisCompilesToo {
public static Test.StaticClassTest createStaticClass() {
return new Test.StaticClassTest();
}
} Notes for
|
Env
APIEnv
API; add Monitor
, add docs, add is_same_object
, fix ReferenceType
generation
…mpl `AsRef` for `Local`
this PR is huge and adds many features that are unrelated and could be merged independently. This makes it quite hard to review. Could you split it in independent PRs? Thanks! |
Sorry, I have made many commits, some of which has corrected errors introduced by prior commits. I cannot simply split them. Instead, I may review this PR for myself and describe all of these changes (maybe later). |
no, I need to review the changes. You can create one branch per feature then use |
The same test case provided in #5 and the test below (based on the previous code) are passed on Android 7.0.
PS: Should I change
jobject
arguments inEnv
methods to&Ref
?