The purpose of JavaObject library is similar to the purpose of Reflection library. Both libraries allow you to directly call Java APIs based on Java reflection features.
JavaObject design is different than Reflection library and in most cases simpler to use. However JavaObject doesn't replace Reflection library as it doesn't support all of its features. In many cases you can use both libraries together (both are lightweight libraries).
JavaObject approach is more "object oriented". You declare a JavaObject object which then "wraps" any other object and provide three methods: SetField, GetField and RunMethod.
JavaObject variable is similar to an Object variable with the addition of the reflection methods.
For example to set the padding of a view:
Note that you do not need to specify the parameters types. However the types should be the exact types. The call will fail if you pass a double instead of an int.
One exception is that you can pass a string instead of Enum.
There are also two Initialize methods.
InitializeStatic - Use this method when you want to call a static method or access a static field.
For example this code calls the static Bitmap.createScaledBitmap method:
InitializeNewInstance - Creates a new instance of the given class with the given parameters (or Null)
Notes
- JavaObject can only access public methods and fields (unlike Reflection library).
- JavaObject doesn't include the helper methods to access the context and other fields as in Reflection library. You can use both libraries together when these fields are needed.
- There is almost no overhead for a JavaObject instance. It is better to create multiple JavaObjects instead of reusing a single instance.
V2.05 is attached.
JavaObject design is different than Reflection library and in most cases simpler to use. However JavaObject doesn't replace Reflection library as it doesn't support all of its features. In many cases you can use both libraries together (both are lightweight libraries).
JavaObject approach is more "object oriented". You declare a JavaObject object which then "wraps" any other object and provide three methods: SetField, GetField and RunMethod.
JavaObject variable is similar to an Object variable with the addition of the reflection methods.
For example to set the padding of a view:
B4X:
Dim jLabel As JavaObject = Label1 'wrap the Label object
jLabel.RunMethod("setPadding", Array As Object(10dip, 10dip, 10dip, 10dip))
One exception is that you can pass a string instead of Enum.
There are also two Initialize methods.
InitializeStatic - Use this method when you want to call a static method or access a static field.
For example this code calls the static Bitmap.createScaledBitmap method:
B4X:
Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int) As Bitmap
Dim bo As JavaObject
bo.InitializeStatic("android.graphics.Bitmap")
Dim bmp As Bitmap = bo.RunMethod("createScaledBitmap", Array As Object(Original, Width, Height, False))
Return bmp
End Sub
InitializeNewInstance - Creates a new instance of the given class with the given parameters (or Null)
Notes
- JavaObject can only access public methods and fields (unlike Reflection library).
- JavaObject doesn't include the helper methods to access the context and other fields as in Reflection library. You can use both libraries together when these fields are needed.
- There is almost no overhead for a JavaObject instance. It is better to create multiple JavaObjects instead of reusing a single instance.
V2.05 is attached.
Attachments
Last edited: