Java Question Wrapping many sub-classes of same super class

warwound

Expert
Licensed User
Longtime User
I'm looking for a quick/elegant way to wrap a super class and many different sub-classes that all extend the same super class.

Say i have a class MySuper and i have already wrapped that class using AbsObjectWrapper:

B4X:
public class MySuperWrapper extends AbsObjectWrapper<MySuper> {
  public void Initialize(){
    setObject(new MySuper());
  }

  public void DoSomething(){
    getObject().doSomething();
  }
}

Now i have 6 or 7 other classes that extend MySuper class, each of these 6 or 7 other classes add various methods and properties to MySuper.
MySuper already has many methods and properties.

So a 'brute force' solution is to wrap each sub-class of MySuper and duplicate the code that wraps all methods and properties of MySuper:

B4X:
public class SubClass1Wrapper extends AbsObjectWrapper<SubClass1> {
  public void Initialize(){
    setObject(new SubClass1());
  }

  public void DoSomething(){
    getObject().doSomething();
  }

  public void NewMethod(){
    getObject().newMethod();
  }
}
B4X:
public class SubClass2Wrapper extends AbsObjectWrapper<SubClass2> {
  public void Initialize(){
    setObject(new SubClass2());
  }

  public void DoSomething(){
    getObject().doSomething();
  }

  public void AnotherNewMethod(){
    getObject().anotherNewMethod();
  }
}

Do-able but there must be a better and more elegant solution?

I was reading this recent thread: http://www.b4x.com/forum/bugs-wishl...d-feature-toggle-button-click.html#post152489.
Is there a solution that uses generics and parameterized classes?
(These are java concepts i have yet to deal with in any depth).

Could i wrap the super class and then re-use that wrapped class when wrapping the sub-classes and avoid lots of duplicate code?
If the java library and these classes i am wrapping gets updated then maintenance of the b4a library will be a pain in the a#se unless i can find the solution i am looking for.

Thanks.

Martin.
 

agraham

Expert
Licensed User
Longtime User

warwound

Expert
Licensed User
Longtime User
Thanks for the reply - it's your post in the thread you linked to that i also linked to in my post.

I think what i was asking for was an example of how to use generics with AbsObjectWrapper...
So i did some experiments and found an answer:

B4X:
public class MySuperWrapper<T extends MySuper> extends AbsObjectWrapper<T> {
  public void Initialize(){
    setObject(new MySuper());
  }

  public void DoSomething(){
    getObject().doSomething();
  }
}

B4X:
public class SubClass1Wrapper<T extends MySuper> extends MySuperWrapper<T> {
  public void Initialize(){
    setObject(new SubClass1());
  }

  public void NewMethod(){
    getObject().newMethod();
  }
}

B4X:
public class SubClass2Wrapper<T extends MySuper> extends MySuperWrapper<T> {
  public void Initialize(){
    setObject(new SubClass2());
  }

  public void AnotherNewMethod(){
    getObject().anotherNewMethod();
  }
}

Eclipse now complains The method setObject(T) in the type AbsObjectWrapper<T> is not applicable for the arguments (SubClass1) (that refers to SubClass1Wrapper).
It suggests to cast the new instance of SubClass1 to T and then everything compiles ok.

Martin.
 

warwound

Expert
Licensed User
Longtime User
I was simple trying to find a way to include the 'already wrapped and commented' parts of the super class in the yet to be wrapped sub classes.
Not sure if that requires generics or not...

BUT i found a better solution.
I looked through all of these classes and they contain no methods or properties - just a lot of fields (instance members) - they are data structures for various types of eeproms.
Previously i planned to write a lot of getter and setter methods to access the fields of the wrapped object.
The fields are all public so i've now simply created a new simple class that extends the class i was originally going to wrap with AbsObjectWrapper:

B4X:
@ShortName("MySuper")
public class MySuper extends third.party.package.name.MySuper{
  // the body of this new class is empty
}

I've done the same for the sub classes of the super class.

Quick n simple!
The library compiles and in the b4a IDE auto-complete list all fields from the super and sub classes appear as expected.

The only thing it lacks is documentation/reference for the fields in the IDE, if that is essential then i will manually add it to the library XML file once the library is complete - though that will itself be a maintenance headache if i need to recompile the library.

Erel has said before that using AbsObjectWrapper is not always the the best choice he's right!

Martin.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…