B4J Question Access to a Type structure by Code?

Discussion in 'B4J Questions' started by MarkusR, Sep 4, 2018.

Tags:
  1. MarkusR

    MarkusR Well-Known Member Licensed User

    if me have a Type structure.
    is it possible to get the fields and datatype from within source code?

    me thought about to generate a input form from it automatically.
     
  2. Daestrum

    Daestrum Well-Known Member Licensed User

    yes - small example (uses JavaObject library)

    Code:
    Sub Process_Globals
     
    Private fx As JFX
     
    Private MainForm As Form
     
    Type myType(name As String,age As Int,height As Int)
    End Sub
    Sub AppStart (Form1 As Form, Args() As String)
     MainForm = Form1
     MainForm.Show
     
    Dim a As myType
     a.Initialize
    ' the next line calls the getFields method in the #if java block
    ' it passes the class of the type (type is a class) 
     Log(asJO(Me).RunMethod("getFields",Array(asJO(a).RunMethod("getClass",Null))))  ' call the inline function
    End Sub

    Sub asJO(o As JavaObject)As JavaObject
     
    Return o
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
     
    Return True
    End Sub

    #if java
    import java.lang.reflect.Field;
    public static String getFields(Class c){
     String fs = "";
     Field[] f = c.getDeclaredFields();
     for (Field ff : f){
      fs += ff.getName().toString()+"\t";
      fs += ff.getType().getSimpleName().toString()+"\n\n";
     }
     return fs;
    }
    #End If
     
    Mashiane and MarkusR like this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    MarkusR likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice