B4J Question [Solved]JavaObject - Can anyone explain why this doesn't work?

keirS

Well-Known Member
Licensed User
Longtime User
GetDeclaredFields tries to use JavaObject to get the fields for a passed object. GetDeclaredFields2 does the same thing but uses inline Java. Why doesn't GetDeclaredFields work?

B4X:
Sub Class_Globals
    Private fx As JFX
    Dim AString As String
    Dim AMap As Map
    Dim AList As List
    Dim AInt As Int
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize



End Sub

Public Sub GetDeclaredFields(O As Object) 
    Dim JO As JavaObject 
    Dim Fields() As Object
    JO = O
   Fields = JO.RunMethodJO("getClass",Null).RunMethod("getDeclaredFields",Null)
   
   For i = 0 To Fields.Length -1
     JO = Fields(i)
     Log(JO.RunMethod("toString",Null))
    Next
   
End Sub


Public Sub GetDeclaredFields2(O As Object)
    Dim NativeMe As JavaObject = Me
    Dim Fields() As Object
    Dim JO As JavaObject
    Fields = NativeMe.RunMethod("GetDeclaredFieldsJ",Array(O))
    For i = 0 To Fields.Length -1
        JO = Fields(i)
    Log(JO.RunMethod("toString",Null))
    Next
End Sub

#If Java
 import java.lang.reflect.*;

 public Object[]  GetDeclaredFieldsJ(Object O) {
    return O.getClass().getDeclaredFields();
 }
 #End If
 
Top