B4A Library Reflection library

There will be lots to come in future versions of Basic4Android as Erel adds more of Androids capabilities. In the mean time there will be things that would be nice to have but are not actually implemented. This library will help to solve at least some of those requirements.

This is the Android equivalent of the Basic4ppc Door library that turned out to be so useful. There is a demo for the library that adds onFocus and onTouch events to views that don't implement them, accesses objects fields and runs methods.

A separate demo shows how to "Linkify" text so that URLs in the text become active hyperlinks.

EDIT:- Version 1.1 posted. See post #17 for details.

EDIT:- Version 1.2 posted. Bug fix. See post#19 for details.

EDIT:- Version 1.3 posted. See post#20 for details.

EDIT:- Version 1.4 posted. See post#21 for details.

EDIT:- Version 1.5 posted. See post #25 for details.

EDIT:- Version 1.6 posted. See post #31 for details.

EDIT:- Version 1.7 posted. See post #32 for details.

EDIT:- Version 1.8 posted. See post #33 for details.

EDIT:- Version 1.9 posted. See post #48 for details.

EDIT:- Version 2.0 posted. See post #51 for details.

EDIT:- Version 2.1 posted. See post #53 for details.

EDIT:- Version 2.2 posted for Basic4android 2.0 or later. Version 2.1 remains for earlier versions. See post #57 for details.

EDIT:- Informatix has produced Version 2.3 in post #317

EDIT:- Version 2.4 posted. See post #91 for details.
 

Attachments

  • Reflection2.1.zip
    33.2 KB · Views: 1,890
  • Reflection2.4.zip
    33 KB · Views: 4,133
Last edited:

Informatix

Expert
Licensed User
Longtime User
I found a bug that can be reproduced at will with ICS or JellyBean:
1) declare a touch event listener for a view with SetOnTouchListener
2) touch the view with one finger and the Home button (or History) with another finger
3) release the button, then the view
-> Crash:
java.lang.NullPointerException
at anywheresoftware.b4a.agraham.reflection.Reflection$7.onTouch(Reflection.java:1100)
at android.view.View.dispatchTouchEvent(View.java:7234)
....

A Try/Catch in the Java code to get rid of this would be very appreciated. Thanks in advance.
 

Informatix

Expert
Licensed User
Longtime User
As a matter of principle libraries should not silently swallow exceptions. It is the job of the calling code to anticipate that they might occur, catch them and deal with them in a manner appropriate for the situation.

It's impossible to prevent this issue in the calling code or to catch it. Only the library can handle this. Your library suppose that some values are not null. That's not the case here.
 

agraham

Expert
Licensed User
Longtime User
Sorry but I'm very rusty on Java and Basic4android but I seem to remember that, as stated in the comments in the XML, SetOnTouchListener has issues with Android 4. I was going to remove it but Erel asked me to leave it in as it was useful in some instances that I now forget. I can't remember the details and can't now find the relevant forum posts.

Would you like to comment Erel?
 

Informatix

Expert
Licensed User
Longtime User
Sorry but I'm very rusty on Java and Basic4android but I seem to remember that, as stated in the comments in the XML, SetOnTouchListener has issues with Android 4. I was going to remove it but Erel asked me to leave it in as it was useful in some instances that I now forget. I can't remember the details and can't now find the relevant forum posts.

Would you like to comment Erel?

I have no problem at all with ICS or JellyBean (except the one mentioned above, but it's a bug) and this listener is mandatory when you begin to do serious things with B4A. If you call something from the event handler and that causes an issue, you have to replace CallSub by CallSubDelayed. That's the only issue I'm aware of (and I use it a lot). The B4A Touch event handler is absolutely of no use for my needs (no MotionEvent returned, no possibility to propagate the event to children).
If you don't want to fix this issue, I will have no other solution than rewriting your library and I'd like to avoid this.
 

agraham

Expert
Licensed User
Longtime User
If you don't want to fix this issue, I will have no other solution than rewriting your library and I'd like to avoid this.
Oooh threats! Let me save you the trouble. I've grown to loathe Android, and Google, so much that it is unlikely that I will ever code for Android again.
 

Attachments

  • Reflection.zip
    7.1 KB · Views: 236

burutxaga

New Member
Licensed User
Longtime User
Can you upload your project?

Yes, I have uploaded the project without problems, I can see its code, and the reflection library version 2.20 is instaled.

I've tried with the IME library in the same way, and I have no problems at all.
 

Informatix

Expert
Licensed User
Longtime User
Oooh threats! Let me save you the trouble. I've grown to loathe Android, and Google, so much that it is unlikely that I will ever code for Android again.

Oh no, it's not a threat, it's just a fact. I'm not a native english speaker, so what I say is probably not said with the manner. And I understand very well your reasons to not maintain this library. I've been a major contributor to the Jedi library for Borland users, but it's the past and I didn't write Delphi code since many years. If someone asked me to fix something in my components, I will refuse for sure.

Thanks for the source code. I'm going to do the change and repost it here.
For users of the library, I want to be clear about the change: I'm not going to silently throw away an exception, just handle the case where MotionEvent returns with a null value.
 

Informatix

Expert
Licensed User
Longtime User
Reflection v2.3

Here is the modified version (compiled code + source code).
The problem was due to raiseEvent returning Null. This issue is not new as I saw the same fix for onLongClick and onKey events.
I added to the code five event declarations so it is now possible to use space and tab in the IDE to declare the event handlers.

That saves my day. :)

EDIT: I also mentioned in the comment of SetOnTouchListener that you can use it with DoEvents and MsgBox if you place the code in another sub and call it with CallSubDelayed.
 

Attachments

  • Reflection v2.3.zip
    26.9 KB · Views: 358
Last edited:

hdtvirl

Active Member
Licensed User
Longtime User
Well Done Informatix, thank you for taking the time with the reflections Library and upgrading it to version 2.3. I am sorry Andrew still hates Android !


Andrew come back we miss you !!!!!!!!!!!.

Regards
 

Eduard

Active Member
Licensed User
Longtime User
Version 2.4 now posted in post #1 of this thread.

RunPublicMethod was broken as Basic4android could pass not the type of one of the parameters owing to a cut and paste failure on my part. I decided I might as well fix it.

Thank you!
 

AndreiH

New Member
Licensed User
Longtime User
I'm trying to check if the values that I'm reading from the database are NULL and I read about ISNULL. Unfortunately it won't let me use it, no idea why.
Of course there is a reference to Reflection library (ver 2.40). Code looks like this.

B4X:
   Dim strSQL As String 
   Dim cur As Cursor 
   Dim MyColumn As Int 
   
   strSQL = "SELECT MyColumn , SoundOn FROM Config"
   cur = SQL.ExecQuery (strSQL)
   cur.Position=0 

   If IsNull(cur.GetInt ("MyColumn")) = True Then
      MyColumn = 0
   Else
      MyColumn= cur.GetInt ("MyColumn") 
   End If

Is there another better way to handle null values?

Thank you
 
Last edited:

AndreiH

New Member
Licensed User
Longtime User
I'm getting error using that: java.lang.NumberFormatException: Invalid double: "null".
Anyway, I found a simple workaround by modifying the select statement like this:

B4X:
strSQL = "SELECT ifnull(MyColumn,0) AS MyColumn, SoundOn FROM Config"

Hope it'll help somebody.

Best regards,
Andrei

P.S. B4A is a fantastic product! Keep up the good work guys.
 

ivan.tellez

Active Member
Licensed User
Longtime User
How to set the maxlength in edittext

In forum, i found that B4A cant define a "max length" for EditText.

In java, i found this code:

B4X:
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(FilterArray);

Is there a way to implement in B4A using this lib?

Thanks
 

agraham

Expert
Licensed User
Longtime User
I'm not sure it's worth the effort but here it is, done by Reflection
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout")
   Dim r1, r2 As Reflector
   Dim i As Int
   
   Dim args(1) As Object
   Dim types(1) As String
   args(0) = "android.text.InputFilter$LengthFilter"
   types(0) = "java.lang.String"
   r1.Target = r1.RunStaticMethod("java.lang.Class", "forName", args, types) 
   'Msgbox(r1.TypeName, "")   
   
   Dim args(2) As Object
   Dim types(2) As String
   args(0) = r1.Target
   types(0) = "java.lang.Class"
   i = 1
   args(1) = i
   types(1) = "I"
   r1.Target = r1.RunStaticMethod("java.lang.reflect.Array", "newInstance", args, types)
   'Msgbox(r1.TypeName, "")
   
   Dim args(1) As Object
   Dim types(1) As String
   Dim i As Int = 8
   args(0) = i
   types(0) = "I"
   r2.Target = r2.CreateObject2("android.text.InputFilter$LengthFilter", args, types)
   'Msgbox(r2.TypeName, "")
   
   Dim args(3) As Object
   Dim types(3) As String
   i = 0
   args(0) = r1.Target
   types(0) = "java.lang.Object"
   args(1) = i
   types(1) = "I"
   args(2) = r2.Target
   types(2) = "java.lang.Object"
   r1.RunStaticMethod("java.lang.reflect.Array", "set", args, types)
   'Msgbox(r1.TypeName, "")
   
   Dim args(1) As Object
   Dim types(1) As String   
   r2.Target = EditText1
   args(0) = r1.Target
   types(0) = "[Landroid.text.InputFilter;"
   r2.RunMethod4("setFilters", args, types)   
   
End Sub
I had to use the Class android.text.InputFilter$Length instead of the interface android.text.InputFilter to get it to work.
 

mterveen

Member
Licensed User
Longtime User
get field format

i cannot figure out how to use the getfield member. i know it can't be that hard!!

basically using the following info:

MyClass:

B4X:
Sub Class_Globals
   private v_i as int
End sub

Sub seti(e as int)
   v_i = e
End sub
Sub geti as int
   return v_i
end sub

how do i use the getfield member to access the class variable?
e.gl

Program:

B4X:
dim c as myclass
c.initialize
dim r as reflector
r.target= c             '????????????
log(r.getfield("v_i"))  '??????????????
log(r.getfield("i"))     'also bombs out

trying the above keeps telling me it can't find the field?

help!
 
Top