B4J Question [jReflection] Method not found

MathiasM

Active Member
Licensed User
Hello

I try to run a method which retrieves a string on an object using the jReflection library.
Here is a testcase:

I have an class named "MyObject"

B4X:
Sub Class_Globals
    Private mName As String
End Sub

Public Sub Initialize(Name As String)
    mName = Name
End Sub

Public Sub getName As String
    Return mName
End Sub

So I try to run getName so I receive the name of the instance. I now have this code:

B4X:
Dim mo As MyObject
mo.Initialize("Testname")

Dim reflect As Reflector
reflect.Target = mo
Log(reflect.RunMethod("_getname"))
'Log(reflect.RunMethod("getname"))
'Log(reflect.RunMethod("getName"))

I always run into an error stating that _getname doesn't exist. "getname" and "getName" also won't work.
While in the java files, "_getname" really exists:

Java:
public String  _getname(b4j.example.myobject __ref) throws Exception{
__ref = this;
RDebugUtils.currentModule="myobject";
if (Debug.shouldDelegate(ba, "getname", false))
     {return ((String) Debug.delegate(ba, "getname", null));}
RDebugUtils.currentLine=720896;
 //BA.debugLineNum = 720896;BA.debugLine="Public Sub getName As String";
RDebugUtils.currentLine=720897;
 //BA.debugLineNum = 720897;BA.debugLine="Return mName";
if (true) return __ref._mname /*String*/ ;
RDebugUtils.currentLine=720898;
 //BA.debugLineNum = 720898;BA.debugLine="End Sub";
return "";
}

What am I missing here?

Thanks a bunch!
 

Star-Dust

Expert
Licensed User
Longtime User
Please note that if you fill in with obfuscation the variables may change no and if you don't put a low line in the name. (my_name)
In any case use JavaObject, because reflect would use them if the variable was private not public
 
Upvote 0

MathiasM

Active Member
Licensed User
Thanks @Star-Dust for your reply. I tried using the JavaObject Library, yet a comparable error appears:

B4X:
Dim mo As MyObject
mo.Initialize("Testname")

Dim jo As JavaObject = mo
'Log(jo.RunMethod("_getname", Null))
'Log(jo.RunMethod("getname", Null))
'Log(jo.RunMethod("getName", Null))

The 3 different methods throw the same error:

java.lang.RuntimeException: Method: getName not found in: b4j.example.myobject
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private mName As String
End Sub

Public Sub Initialize(Name As String)
    mName = Name
End Sub

Public Sub getMy_Name As String 'modify this
    Return mName
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
While in the java files, "_getname" really exists:
it exists compiled in debug mode, but if you compile in obfuscated it does not exist
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If I understand well, you have written the class in B4J?
Then why do you want to use Reflection or JavaObject?
Just use:
B4X:
Log(mo.Name)

This routine:
B4X:
Public Sub getName As String
    Return mName
End Sub
Means that Name is a readable property, because of 'get' at the beginning.
A routine beginning with 'set' means a writable property.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
always run into an error stating that _getname doesn't exist. "getname" and "getName" also won't work.
You are calling _getname with no parameters whereas your Java fragment shows that it takes one parameter. The error message is correct in this case. Because Java can overload method names as long as the method signatures differ it is correctly telling you that a method _getname with no parameters does not exist. You would need to call it with the instance of MyObject as a parameter.
 
Upvote 0

MathiasM

Active Member
Licensed User

Spot on, this was the problem. I was confused with overloading and the fact that the 'method didn't exist' without parameters indeed. Thanks a lot!


Thanks again for your reply. I changed the code as you said but the problem persisted. See @agraham

If I understand well, you have written the class in B4J?
Then why do you want to use Reflection or JavaObject?
Just use:
B4X:
Log(mo.Name)
Thanks for your reply @klaus , This example was very simplified. In my 'real' application the property names were loaded from a JSON file, not typed in code.

Thank you everybody for thinking with me and the very fast response times! Saved my day!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…