Java Question ba object exposed in newer libraries.

corwin42

Expert
Licensed User
Longtime User
It seems that the ba object is exposed in newer libraries now. Maybe since B4A 6.50? I'm not sure.
This is true for Donmanfreds libraries, too.

I have no idea why there is a ba field now.

 

corwin42

Expert
Licensed User
Longtime User
It should be a private field.
Yes. Seems that libraries which extend ViewWrapper have the ba object exposed.

The above CardViewWrapper class has only these local fields:

B4X:
public class CardViewWrapper extends ViewWrapper<CardView> implements
        DesignerCustomView {

    private BA mBa;
    private String mEventName;

I have no idea why there is a ba field in the IDE.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you should avoid having instance variables in classes that extend AbsObjectWrapper or ViewWrapper. The instance variables will be Null with code such as:
B4X:
List1.Add(CardView1) 'Only the inner object is added to the list
Dim CardView2 As CardView = List1.Get(0) 'a new wrapper is created

I'm unable to reproduce the ba issue.

Can you try it with the attached BADoclet files?
 

Attachments

  • BADoclet.zip
    9.4 KB · Views: 323

corwin42

Expert
Licensed User
Longtime User
Note that you should avoid having instance variables in classes that extend AbsObjectWrapper or ViewWrapper. The instance variables will be Null with code such as:

Oops, yes. The local member variables are not really necessary here. I fixed it (and I will check my other libraries if I have used local members anywhere).

I'm unable to reproduce the ba issue.

Can you try it with the attached BADoclet files?

I tried but the field is still there.

Maybe it has somethiing to do with the classpath? I use a batch file to generate the xml and the classpath is automatically generated by Android Studio.

Here is the output from the xml generation:

 

corwin42

Expert
Licensed User
Longtime User
Try it with the attached doclet (v1.06). It skips fields named ba.

This one works. The ba field is gone in the xml.
 

DonManfred

Expert
Licensed User
Longtime User
Note that you should avoid having instance variables in classes that extend AbsObjectWrapper or ViewWrapper.
So for me (i guess i learned it by myself the wrong way as i always use Abs- or Viewwrapper.
and i always am using instance variables.

If i would now stop it then i need to go another way?

What is suggested then?

I mean in case of using AbsObjectWrapper: Do i need to use
instead of
B4X:
public class TelegramBotApiWrapper extends AbsObjectWrapper<JTelegramBot>
i need to use??
B4X:
public class TelegramBotApiWrapper extends JTelegramBot
and then using @override for all methods i want to wrap?

Do you have an example i could learn from?

PD: Edit: lol.... The forum does use my annotation to mark a specific forum user named override
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is problematic to create a wrapper the directly extends other objects. All the methods will be exposed and it will cause other issues as well.

Don't extend any class. Add JTelegramBot as an instance variable:
B4X:
@Hide
public JTelegramBot bot;
Use bot instead of getObject().

The reason that it is a hidden public variable is to allow developers to access the native object with JavaObject (with JavaObject.GetField).
 

corwin42

Expert
Licensed User
Longtime User
The advantage of the ViewWrapper is that it directly has the methods every B4A View should have and in my opinion it helps a lot with wrapping another view.
The limitation that you can't have local members is normally not a problem since you don't need them. If you add functionality which requires local members there are two options.

1.) Create a Custom class which extends the class you want to wrap and add the members and functionality you need there. Then in the class which extends ViewWrapper/AbsObjectWrapper you can wrap the custom class and you can access the fields with getObject().getXXX().
2.) Do it the way Erel suggested.

As for AbsObjectWrapper it is great in some situations because you don't have to care about wrapping/unwrapping the native object. It is used in AppCompat library for the ACMenu and ACMenuItem objects for example. Because they are just wrappers for the Menu and MenuItem classes they can be transparently used for PopupMenus, NavigationView, ActionMode and so on.

As for local members. If I'm correct public static local members like for constants (see below) are no problem. Erel, please correct me if I'm wrong:
B4X:
public static int SHOW_AS_ACTION_IF_ROOM = MenuItemCompat.SHOW_AS_ACTION_IF_ROOM;
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a third option if you want to extend ViewWrapper (or any other wrapper) and do need to add instance variables.

You can use this code to store any data you like:
B4X:
AbsObjectWrapper.getExtraTags(getObject()).put("ba", ba);
AbsObjectWrapper.getExtraTags(getObject()).put("eventName", eventName);
AbsObjectWrapper.getExtraTags(getObject()).put("something else", 101);

public String getEventName() {
     return (String) AbsObjectWrapper.getExtraTags(getObject()).get("eventName");
   }
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…