Java Question Abstract class with non void methods

spillo3d96

Member
Licensed User
Longtime User
Hello, I have wrapped libraries and classes for B4A before but this is the first time I face an abstract class with non void methods.

Suppose I have this abstract class

B4X:
public abstract class MyClass {
public MyClass() {}

public abstract int method1();

public abstract int method2();

public abstract int method3(Object someObject);

}

So far, I have managed to do like this:

B4X:
@BA.Events(values = {"Method1()", "Method2()", "Method3(someObject as Object)"})
@BA.ShortName("MyClassWrapper")
public class MyClassWrapper extends MyClass {

    private BA ba;
    private String eventName;

    private static final boolean throwErrorIfMissingSub = true;

    public void Initialize(final BA ba, String EventName) {
        this.ba = ba;
        this.eventName = EventName;
    }

    @BA.Hide
    @Override
    public int method1() {
        return (Integer) ba.raiseEvent2(MyClassWrapper.this, false, "_method1", throwErrorIfMissingSub);
    }

    @BA.Hide
    @Override
    public int method2() {
        return (Integer) ba.raiseEvent2(MyClassWrapper.this, false, "_method2", throwErrorIfMissingSub);
    }

    @BA.Hide
    @Override
    public int method3(Object someObject) {
        return (Integer) ba.raiseEvent2(MyClassWrapper.this, false, "_method3", throwErrorIfMissingSub, someObject);
    }
}

Is this way correct? Also, should throwErrorIfMissingSub be set to True like I did?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Also, should throwErrorIfMissingSub be set to True like I did?
It's up to you.

There is a small mistake here which will cause a null pointer exception in some cases where the event cannot be raised.
It is better to write it like this:
B4X:
Integer i = ba.raiseEvent2(...)
if (i == null)
 return 0;
return i;
 
Top