As you have already discovered, MIDP does not provide a method for getting the superclass of a class, nor for enumerating all classes in the application.
So all you can do is keep track of the class hierarchy yourself.
Having a common superclass makes it slightly easier, because you can have the new object add its own class to a global class collection (if not already present) in the superclass constructor:
abstract class View {
protected View() {
classHierarchy.add(this.getClass());
}
}
but unfortunately this will not work for abstract classes, because no instances are ever created.
Keeping track of superclass/subclass relations for a known subset of classes is easy enough. e.g.:
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
public class ClassHierarchy {
public ClassHierarchy() {
childToParentMap = new Hashtable();
parentToChildMap = new Hashtable();
parentToChildMap.put(Object.class, new Vector());
}
public boolean addClass(Class toAdd) {
if (toAdd.isInterface()) return false;
if (toAdd.equals(Object.class)) return false;
if (childToParentMap.get(toAdd) != null) return false;
addClassBelow(toAdd, Object.class, new Vector());
return true;
}
public Class getParent(Class subclass) {
return (Class) childToParentMap.get(subclass);
}
private void addClassBelow(Class toAdd, Class parent, Vector initialChildren) {
Vector children = (Vector) parentToChildMap.get(parent);
Class reparented;
do {
reparented = null;
for (Enumeration childEnum = children.elements();
childEnum.hasMoreElements();
) {
Class child = (Class) childEnum.nextElement();
if (child.isAssignableFrom(toAdd)) {
addClassBelow(toAdd, child, initialChildren);
return;
} else if (toAdd.isAssignableFrom(child)) {
children.removeElement(child);
initialChildren.addElement(child);
childToParentMap.put(child, toAdd);
// Guard against concurrent modification
reparented = child;
break;
}
}
} while (reparented != null);
children.addElement(toAdd);
childToParentMap.put(toAdd, parent);
parentToChildMap.put(toAdd, initialChildren);
}
private Hashtable childToParentMap;
private Hashtable parentToChildMap;
}
But this can "miss" intermediate classes that are added later, e.g. if you have these classes:
Object >= View >= A >= B >= C
and add A
and C
to the tree and asked it for the superclass of C
it would give you A
, and if you later added B
it would replace A
as the superclass of C
, but not until the wrong styler had been returned for some instances of C
.
So I think you will have to add the restriction that ancestor classes (that have stylers defined for them) must be added to the tree first. Possibly from the static initializer block of the class that overrides createStylerForViewClass
, or the static initializer of the view class itself.
I did think of one other evil hack, but I can't really recommend it:
- In the
View
constructor, create a new Exception
, but don't throw it.
- Temporarily swap
System.err
for your own writer that writes to a ByteArrayOutputStream
- Call
printStackTrace()
on the exception
- Restore
System.err
to its original value
- Parse the stack trace from the
ByteArrayOutputStream
. The names of the intermediate classes' constructors will be in the stack trace. Now you can look them up using Class.forName()
and add them to the tree.