Is there a simpler way to access Java arrays?

Post your questions and help other users.

Moderator: Martin

jaejunks
Posts: 13
Joined: 03 Apr 2018 13:31

Is there a simpler way to access Java arrays?

Post by jaejunks » 24 Apr 2018 20:52

By "Java array", I meant the one returned from below example code.

[code]result = callJavaMethod(getContext(), "android.content.Context", "fileList()");[/code]

FYI, the `fileList()` method of `android.content.Context` class returns a `String[]` value which is an array of strings.

If I pass it as the `Message` parameter of a `Message Dialog` action, it'll show as e.g.:

[code][Ljava.lang.String;@6ff7e14[/code]

And accessing the element of that Java string array can only be done using `for-each` loop. The `getElement()` function is not usable for this. And bracket notation can't be used also, since it's currently available only for number list.

jaejunks
Posts: 13
Joined: 03 Apr 2018 13:31

Re: Is there a simpler way to access Java arrays?

Post by jaejunks » 24 Apr 2018 20:59

Also, there seem to be no way to know the length of that Java array, without using a `for-each` loop and manually counting the number of loops.

PS) Sorry for the disabled BBCode in the initial post. They should be like below.

Code 1:

Code: Select all

result = callJavaMethod(getContext(), "android.content.Context", "fileList()");
Code 2:

Code: Select all

[Ljava.lang.String;@6ff7e14

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Is there a simpler way to access Java arrays?

Post by Desmanto » 25 Apr 2018 17:36

I also stuck at the same place. Previously I want to find the timezone using java, and stuck here too. I have found a shorter method to find it anyway, but still curious about how to parse the data from the java object.

I remembered only 2 example here (you can search it using "java" keyword in my index) :
WIFI Scan: Get Level of specific BSSID?
Way to get display resolution

At there, the java object is passed to another function again. How to pass it and how to know which function to use is beyond my current knowledge.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Is there a simpler way to access Java arrays?

Post by bogdyro » 26 Apr 2018 14:11

You can get its length and iterate through the items,yes.

ctx=getContext();
str=newList();
str=callJavaMethod(ctx, "android.content.Context", "fileList()");
lentgh=length(str);
for (s in str)
log(s)

You can also create your own list and copy the items

MyList=newList();
for(s in str)
addelement(MyList,s)

jaejunks
Posts: 13
Joined: 03 Apr 2018 13:31

Re: Is there a simpler way to access Java arrays?

Post by jaejunks » 26 Apr 2018 16:18

Code: Select all

ctx=getContext();
str=newList();
str=callJavaMethod(ctx, "android.content.Context", "fileList()");
lentgh=length(str);
THAT, surprisingly works. But it just doesn't make any sense on how it works. The second line on the above code should make the `str` variable entirely a Java array value. There should be no trace or inheritance from the `List` type caused by the fist line. Passing it to the `length()` function shouldn't work, but it does. Somehow...

This, at least, solve the array length retrieval. So, thank you bogdyro.

Now, if only there's a way to retrieve an element of the Java array without using a loop...

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Is there a simpler way to access Java arrays?

Post by Desmanto » 26 Apr 2018 17:50

@bogdyro : Thanks for the example. I don't declare the str=newList() anymore, and it still works. So when str populated by java result, it will be in the form of java object. Using the Automagic function will convert it to automagic object then (list type).

I will try out to see what I can do with it. It makes more sense now.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Is there a simpler way to access Java arrays?

Post by bogdyro » 26 Apr 2018 17:54

Actually with or without
Str=newList() everything is the same.
Only Martin can give us more info or implement a convert function between Java types and AM variables.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Is there a simpler way to access Java arrays?

Post by Martin » 26 Apr 2018 19:51

Hi,

Automagic has very limited support for arbitrary java types. Iterating a list using the for each loop and testing the length of an array plus function isEmpty are the only scripting features that understand regular java arrays.
You can convert the string array to a regular list of strings using a code snippet like this:

Code: Select all

ctx = getContext();
array = callJavaMethod(ctx, "android.content.Context", "fileList()");
list = newList();
for (v in array) addElement(list, v);
Converting an array like this should be quite fast.

Regards,
Martin

jaejunks
Posts: 13
Joined: 03 Apr 2018 13:31

Re: Is there a simpler way to access Java arrays?

Post by jaejunks » 26 Apr 2018 21:24

@Martin: Oh, OK. I guess I'll have to use that for now. Thanks for confirming this.

I've been looking into Java classes and found that maybe I could use the `java.lang.reflect.Array.get()` static method to retrieve an element array. So I tried this.

Code: Select all

ctx = getContext();
array = callJavaMethod(ctx, "android.content.Context", "fileList()");
val = callJavaStaticMethod("java.lang.reflect.Array", "get(java.lang.Object, java.lang.Integer)", array, 0);
But it throws an error during `callJavaStaticMethod()`:
No method with name get found in class java.lang.reflect.Array
Does that mean that the `get()` method is not available? Or did I miss something?

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Is there a simpler way to access Java arrays?

Post by bogdyro » 27 Apr 2018 08:26

Nice find. In my opinion it's simpler to copy the array to a list.
But this method does work also

callJavaStaticMethod("java.lang.reflect.Array", "get(java.lang.Object, int)", name_of_array, index)

Your 'get' method signature is weird. I have 'int' instead of 'java.lang.Integer' for the second parameter

Post Reply