B4J Question Set members of an instance via a Map with reflection?

MathiasM

Active Member
Licensed User
Hello

I'm trying to find an easy solution for this:
I want to read a json file, and update an object accordingly. I made a small example of what I want to achieve. In my example I have a User class I want to alter:

B4X:
Sub Class_Globals
    Private mFirstname As String
    Private mLastname As String
End Sub

Public Sub Initialize(Firstname As String, Lastname As String)
    mFirstname = Firstname
    mLastname = Lastname
End Sub

Public Sub getFirstname As String
    Return mFirstname
End Sub

Public Sub getLasttname As String
    Return mLastname
End Sub

Public Sub UpdateByMap(m As Map)
    Dim reflect As Reflector
    reflect.Target = Me
    For Each key As String In m.Keys
        reflect.SetField2("m" & key, m.Get(key))
    Next
End Sub

I'm calling this code this way:

B4X:
Sub AppStart (Args() As String)
    Dim u As User
    
    u.Initialize("Tom", "Jones")
    
    Dim m As Map
    m.Initialize
    m.Put("Firstname", "Matt")
    
    u.UpdateByMap(m)
    
    Log(u.Firstname & " " & u.Lastname)
    StartMessageLoop
End Sub

Does anyone knows what is wrong here, It's the first time I use this reflection library: https://www.b4x.com/b4j/help/jreflection.html

The error is:
java.lang.NoClassDefFoundError: javafx/beans/value/ChangeListener

Thank you!
 

agraham

Expert
Licensed User
Longtime User
I don't know where to start with that Reflection code. It is so wrong that it is beyond comment. To use Reflection you need to understand the Java objects and their methods and you seem not to.

I don't see why you need to use Reflection at all. Cannot you just update the Map normally. You need to explain what you are trying to achieve.

P.S. I wrote the Reflection library :)
 
Upvote 0

MathiasM

Active Member
Licensed User
I don't know where to start with that Reflection code. It is so wrong that it is beyond comment. To use Reflection you need to understand the Java objects and their methods and you seem not to.
I'm sorry I butchered your library. I did look at the generated Java code, but it didn't point me to the right direction, as I miss some fundamentals to use this library I think.
I don't see why you need to use Reflection at all. Cannot you just update the Map normally. You need to explain what you are trying to achieve.
Well, I don't want to update a Map. I want to use a Map to update a User instance.

In the User class in my post above, the User has 2 fields. Firstname and Lastname.

What I actually want to do, is have a JSON file, something like this:
{
"Firstname": "Matt",
"Lastname":"Jones"
}

Then I want to load this JSON to a Map. In this Map the keys will be the field names of the User class, the values will be the values that I want to set on their respective field names.
That's why I wrote this code:
B4X:
Public Sub UpdateByMap(m As Map)
    Dim reflect As Reflector
    reflect.Target = Me '<- Set the reflector target to this user instance
    For Each key As String In m.Keys '<- For each key, AKA Firstname, Lastname
        reflect.SetField2("m" & key, m.Get(key)) '<- Set the field name m + key AKA mFirstname, mLastname to their respective values AKA Matt, Jones
    Next
End Sub

Is what I want to do, setting fields trough a variable field name (from a JSON, Map, Whatever) possible?

Basically, I want something like CallSub, but for setting Fields, not calling subs. I hope I somewhat explained clearly what I want to achieve.
P.S. I wrote the Reflection library :)

Very nice! Way above my league!

Thanks for your reply!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Upvote 0
Top