B4J Question Access to a Type structure by Code?

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.
 

Daestrum

Well-Known Member
Licensed User
yes - small example (uses JavaObject library)

B4X:
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 String) As 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
 
Top