Commit 5852e6e
authored
[Java.Interop] Use
Fixes: #23
Context: dotnet/android@aba2726
Context: b4d44e4
Context: https://github.com/xamarin/monodroid/commit/ed984a3a0bfbe71f6499a7855c647b5bc2b28466
Context: dotnet/android#7616
Commit b4d44e4 noted:
> Android is..."special", in that not all threads get the same
> ClassLoader behavior. Specifically, *managed* threads --
> System.Threading.Thread instances -- get a different ClassLoader than
> the main/UI thread on Android. (Untested, but the ClassLoader *may*
> behave sanely if you use a java.lang.Thread instance instead. But who
> wants to use java.lang.Thread instances...?)
dotnet/android#7616 provided additional context: `JNIEnv::FindClass()`
behavior *is* tied to the thread that calls it, and one of the knock-on
effects is that `Java.Lang.JavaSystem.LoadLibrary("MyLib")` doesn't
work properly when invoked from a new `System.Threading.Thread` thread.
(This is still the case, by the way.)
Which brings us to xamarin/mondroid@ed984a3a, which updated then
Xamarin.Android to use
[`Class.forName(String name, boolean initialize, ClassLoader loader)`][0]
instead of [`ClassLoader.loadClass(String)`][1], because the "real"
JDK cannot use `ClassLoader.loadClass(String)` to load array types:
Class c1 = Class.forName("[Ljava.lang.String;"); // works
Class c2 = ClassLoader.getSystemClassLoader()
.loadClass("[Ljava.lang.String;"); // throws java.lang.ClassNotFoundException
Class c3 = Class.forName("[I"); // works; array of int
Class c4 = ClassLoader.getSystemClassLoader()
.loadClass("[I"); // throws java.lang.ClassNotFoundException
Using `ClassLoader.loadClass(String)` to load array types works on
Android, presumably as an undocumented implementation detail, but as
xamarin/monodroid@ed984a3a was trying to get things working within
the (now dead) Android Designer -- which ran using a Desktop JDK --
Android-specific extensions were not available.
Update `JniEnvironment.Types` to use `Class.forName(String)` instead
of `ClassLoader.loadClass(String)` to load Java classes, as a fallback
for when `JNIEnv::FindClass()` fails to find the class.
`[Obsolete]` the `JniRuntime.CreationOptions.ClassLoader_LoadClass_id`
property as it is no longer used.
[0]: https://developer.android.com/reference/java/lang/Class#forName(java.lang.String,%20boolean,%20java.lang.ClassLoader)
[1]: https://developer.android.com/reference/java/lang/ClassLoader#loadClass(java.lang.String)Class.forName() as fallback to load Java classes (#1326)1 parent bc44f08 commit 5852e6e
File tree
2 files changed
+18
-23
lines changed- src/Java.Interop/Java.Interop
2 files changed
+18
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| 33 | + | |
31 | 34 | | |
| 35 | + | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
| |||
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
60 | | - | |
| 64 | + | |
61 | 65 | | |
62 | | - | |
| 66 | + | |
63 | 67 | | |
| 68 | + | |
| 69 | + | |
64 | 70 | | |
65 | | - | |
| 71 | + | |
66 | 72 | | |
67 | 73 | | |
68 | 74 | | |
| |||
169 | 175 | | |
170 | 176 | | |
171 | 177 | | |
172 | | - | |
| 178 | + | |
173 | 179 | | |
174 | 180 | | |
175 | | - | |
| 181 | + | |
176 | 182 | | |
177 | | - | |
| 183 | + | |
178 | 184 | | |
179 | 185 | | |
180 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
152 | 153 | | |
153 | 154 | | |
154 | 155 | | |
155 | | - | |
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | 209 | | |
214 | 210 | | |
215 | | - | |
216 | | - | |
217 | | - | |
| 211 | + | |
218 | 212 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
228 | 217 | | |
229 | 218 | | |
230 | 219 | | |
| |||
0 commit comments