We have an integration to use Java classes in Xojo projects. Our clients like to load a jar file with some code and call methods in Java. We can do that and we have new functions for version 25.3 of MBS Xojo Java Plugin:
LoadedClasses
You can now query the list of loaded classes:
function JavaVMMBS.LoadedClasses as JavaClassMBS()
This is great to know what classes the loaded jar files provide when you initialize the JavaVMMBS object.
Let’s say you have graphics filters defined in jar files. You would load the jar file, check what classes are available. Then you filter the list of classes to only the ones subclassing the base filter class. Then you list all matching classes in a filter list, so the user can pick one.
Here is an example code to query the list of classes and then call the getName function on each to ask for the name of the class:
// show all class names
Var arg As New MemoryBlock(8)
Var LoadedClassNames() As String
Var LoadedClasses() As JavaClassMBS = vm.LoadedClasses
For Each c As JavaClassMBS In LoadedClasses
Var jc As JavaClassMBS = c.ObjectClass
Var jm As JavaMethodMBS = jc.GetMethod("getName", "()Ljava/lang/String;")
Var jo As Variant = c.CallObjectMethod(jm, arg)
Var js As JavaStringMBS = jo
Var name As String = js.StringValue
LoadedClassNames.Add name
Next
MessageBox String.FromArray(LoadedClassNames, EndOfLine)
Fields
Next we have a function to query the fields on a given class. This provides JavaFieldMBS objects, which you can inspect at runtime and use to get or set fields dynamically.
Once the user picked a filter class from our example above, you could inspect it and have some GUI to set properties, maybe like the Xojo inspector. Depending on the data type, you could show picker for color properties or a text field for a string field.
Here is an example code to query the list of fields and show it:
// Show names of fields
Var jclass As JavaClassMBS = vm.FindClass("test")
Var fields() As JavaFieldMBS = jclass.Fields
Var fieldNames() As String
For Each field As JavaFieldMBS In fields
fieldNames.add field.Name
Next
MessageBox String.FromArray(fieldNames, EndOfLine)
Methods
Last, but not least we have a function to query the methods on a given class. This provides JavaMethodMBS objects, which you can inspect at runtime and use to call methods dynamically.
For our filter example, we could have multiple filter* methods, so we could list them and have the user pick the right one.
Here is an example code to query the list of methods and show it:
// show names of methods
Var jclass As JavaClassMBS = vm.FindClass("test")
Var Methods() As JavaMethodMBS = jclass.Methods
Var MethodNames() As String
For Each Method As JavaMethodMBS In Methods
MethodNames.add Method.Name
Next
MessageBox String.FromArray(MethodNames, EndOfLine)
Please try the new methods and let us know whether they help you. While Java has not directly the introspection like Xojo, we hope to help a little here.