-
Notifications
You must be signed in to change notification settings - Fork 74
Refactor JObject structure #998
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57324dd
s/fromRef/fromReference/
HosseinYousefi 7341df5
Refactor package:jni
HosseinYousefi ca333d2
Refactor package:jnigen
HosseinYousefi 442d3d7
Fix tests
HosseinYousefi e63c6ac
Update readme
HosseinYousefi 8f5a536
Add better docs for renaming logic
HosseinYousefi 662358c
Mention the removal of JObject reflective methods in the changelog
HosseinYousefi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,8 @@ String toJavaStringUsingEnv(int n) => using((arena) { | |
i.ref.i = n; | ||
final res = env.CallStaticObjectMethodA(cls, mId, i); | ||
final str = env.toDartString(res); | ||
env.deleteAllRefs([res, cls]); | ||
env.DeleteGlobalRef(res); | ||
env.DeleteGlobalRef(cls); | ||
return str; | ||
}); | ||
|
||
|
@@ -37,21 +38,25 @@ int randomUsingEnv(int n) => using((arena) { | |
final random = env.NewObject(randomCls, ctor); | ||
final nextInt = env.GetMethodID(randomCls, "nextInt".toNativeChars(arena), | ||
"(I)I".toNativeChars(arena)); | ||
final res = env.CallIntMethodA(random, nextInt, Jni.jvalues([n])); | ||
env.deleteAllRefs([randomCls, random]); | ||
final res = | ||
env.CallIntMethodA(random, nextInt, toJValues([n], allocator: arena)); | ||
env.DeleteGlobalRef(randomCls); | ||
env.DeleteGlobalRef(random); | ||
return res; | ||
}); | ||
double randomDouble() { | ||
final math = Jni.findJClass("java/lang/Math"); | ||
final random = math.callStaticMethodByName<double>("random", "()D", []); | ||
final math = JClass.forName("java/lang/Math"); | ||
final random = | ||
math.staticMethod("random", "()D").call(math, const jdoubleType(), []); | ||
math.release(); | ||
return random; | ||
} | ||
|
||
int uptime() { | ||
return Jni.findJClass("android/os/SystemClock").use( | ||
(systemClock) => systemClock.callStaticMethodByName<int>( | ||
"uptimeMillis", "()J", [], JniCallType.longType), | ||
return JClass.forName("android/os/SystemClock").use( | ||
(systemClock) => systemClock | ||
.staticMethod("uptimeMillis", "()J") | ||
.call(systemClock, const jlongType(), []), | ||
); | ||
} | ||
|
||
|
@@ -62,8 +67,9 @@ String backAndForth() { | |
} | ||
|
||
void quit() { | ||
JObject.fromRef(Jni.getCurrentActivity()) | ||
.use((ac) => ac.callMethodByName<void>("finish", "()V", [])); | ||
JObject.fromReference(Jni.getCurrentActivity()).use((ac) => ac.jClass | ||
.instanceMethod("finish", "()V") | ||
.call(ac, const jvoidType(), [])); | ||
HosseinYousefi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void showToast(String text) { | ||
|
@@ -74,18 +80,21 @@ void showToast(String text) { | |
// In this example, Toaster class wraps android.widget.Toast so that it | ||
// can be called from any thread. See | ||
// android/app/src/main/java/com/github/dart_lang/jni_example/Toaster.java | ||
Jni.invokeStaticMethod<JObject>( | ||
"com/github/dart_lang/jni_example/Toaster", | ||
"makeText", | ||
"(Landroid/app/Activity;Landroid/content/Context;" | ||
"Ljava/lang/CharSequence;I)" | ||
"Lcom/github/dart_lang/jni_example/Toaster;", | ||
[ | ||
Jni.getCurrentActivity(), | ||
Jni.getCachedApplicationContext(), | ||
"😀", | ||
0, | ||
]).callMethodByName<void>("show", "()V", []); | ||
final toasterClass = | ||
JClass.forName('com/github/dart_lang/jni_example/Toaster'); | ||
final makeText = toasterClass.staticMethod( | ||
'makeText', | ||
'(Landroid/app/Activity;Landroid/content/Context;' | ||
'Ljava/lang/CharSequence;I)' | ||
'Lcom/github/dart_lang/jni_example/Toaster;'); | ||
final toaster = makeText.call(toasterClass, const JObjectType(), [ | ||
Jni.getCurrentActivity(), | ||
Jni.getCachedApplicationContext(), | ||
'😀', | ||
0, | ||
]); | ||
final show = toasterClass.instanceMethod('show', '()V'); | ||
show(toaster, const jvoidType(), []); | ||
} | ||
|
||
void main() { | ||
|
@@ -103,13 +112,14 @@ void main() { | |
Example("Back and forth string conversion", () => backAndForth()), | ||
Example( | ||
"Device name", | ||
() => Jni.retrieveStaticField<String>( | ||
"android/os/Build", "DEVICE", "Ljava/lang/String;")), | ||
() => JClass.forName("android/os/Build") | ||
.staticField("DEVICE", const JStringType().signature)), | ||
Example( | ||
"Package name", | ||
() => JObject.fromRef(Jni.getCurrentActivity()).use((activity) => | ||
activity.callMethodByName<String>( | ||
"getPackageName", "()Ljava/lang/String;", [])), | ||
() => JObject.fromReference(Jni.getCurrentActivity()).use((activity) => | ||
activity.jClass | ||
.instanceMethod("getPackageName", "()Ljava/lang/String;") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a JFunctionType to be able to call |
||
.call(activity, JString.type, [])), | ||
), | ||
Example("Show toast", () => showToast("Hello from JNI!"), | ||
runInitially: false), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.