B4J Question Class Explorer: Getting Methods and Properties

Mashiane

Expert
Licensed User
Longtime User
Hi

Is it possible to get all the methods and properties of a class?

As an Example, lets say I have a class named clsPerson with Method, Initialize, SetAge, GetAge and properties Age, Gender, MobilePhone.

How can I get these properies by reading the class via a loop?

B4X:
For each prop as Property in clsPerson.Properties
    log(prop.Name)
next

B4X:
for each meth as method in clsPerson.Methods
    log(meth.definition)
    log(meth.variables)
    for each v as variable in meth.variables
        log(v.name)
        log(v.type)    'boolean, string, datetime  
    next
next


Is there anything like this possible?

Anyone please advise?
 

Daestrum

Expert
Licensed User
Longtime User
Little example
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim theCode,inline As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    inline = Me
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
    ' you need to know the name of the class contained in the jar
    ' (jarFile).(className) etc
    theCode.InitializeNewInstance("java.lang.String",Null)
    Dim s As String = theCode.RunMethod("getClass",Null)
    Dim l As List = inline.RunMethod("getMethods",Array(s.replace("class ","")))
    For Each item As String In l
        Log(item)
    Next
End Sub
#if java
import java.util.List;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

public static List<String> getMethods(String cname) {
        List<String> li = new ArrayList<String>();
        Method[] methods = null;
        Class<?> c = null;
        try {
            c = Class.forName(cname);
            methods = c.getDeclaredMethods();
            System.out.println("Method count = " + methods.length);
            if (methods.length > 0 && methods != null){
                for (int a = 0 ; a < methods.length;a++){
                    li.add(methods[a].toString());
                }   
            } 
        } catch (ClassNotFoundException ex) {
            System.out.println("Class not found");
        }
        return li;
    }
#end if
Hope it helps.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Little example
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim theCode,inline As JavaObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    inline = Me
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
    ' you need to know the name of the class contained in the jar
    ' (jarFile).(className) etc
    theCode.InitializeNewInstance("java.lang.String",Null)
    Dim s As String = theCode.RunMethod("getClass",Null)
    Dim l As List = inline.RunMethod("getMethods",Array(s.replace("class ","")))
    For Each item As String In l
        Log(item)
    Next
End Sub
#if java
import java.util.List;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

public static List<String> getMethods(String cname) {
        List<String> li = new ArrayList<String>();
        Method[] methods = null;
        Class<?> c = null;
        try {
            c = Class.forName(cname);
            methods = c.getDeclaredMethods();
            System.out.println("Method count = " + methods.length);
            if (methods.length > 0 && methods != null){
                for (int a = 0 ; a < methods.length;a++){
                    li.add(methods[a].toString());
                }  
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Class not found");
        }
        return li;
    }
#end if
Hope it helps.

Thanks a lot, how do I pass to this, assuming the jarfile is me.jar and the clasname is dothat
(jarFile).(className)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It's like the default B4J package is b4j.example so u would pass "b4j.example.?" to the init static method.

Sorry if the jarfile.classname part was misleading, my original code reads directly from jar files, saves adding them to the project with #AdditionalJar.

You would add the jar with #AdditionalJar as usual, then simply call with "dothat" unless you have a package name, then it would be
"your.package.name.dothat"
 
Upvote 0
Top