views:

172

answers:

2

How can I get the the superclass of a Class instance in Java ME. That is, fake the Class.getSuperclass() functionality with the limited functionality available in CLDC 1.1?

What I want to do is to let the abstract super class do something like this:

public Styler getStylerForViewClass(Class clazz) {
   Styler s = stylers.get(clazz);
   if (s == null) {
     for (Class c = clazz; s == null; c = c.getSuperclass()) {
       if (c == Object.class) {
         throw new IllegalArgumentException("No Styler for " + clazz.getName());
       }
       s = createStylerForViewClass(c);
     }
     stylers.put(clazz, s);
   }
   return s;
}
public Styler createStylerForViewClass(Clazz clazz) {
  if (clazz == View.class) {
    return new DefaultStyler();
  } else {
    return null;
  }
}

Sub classes could then add specializations like this:

 public Styler createStylerForViewClass(Class clazz) {
   if (clazz == SpecialView.class) {
     return new SpecialStyler();
   } else {
     return super.createSylerForViewClass(clazz);
   }
 }
+1  A: 

You have two options:

If you know that the super class belongs to a limited set, you can just call instanceof or use the Class.isInstance() method.

Alternatively, you can have a preprocessor to run on you code and create a data structure that is saved separately which holds your classes information. Probably even a custom doclet can do it. The output can be a text or binary file that describes the structure:

 ClassA:SuperClass
 ClassB:AnotherSuperClass
 etc.

Notice you may have problem with the obfuscation in this way.

David Rabinowitz
Personally I would try to stick to `instanceof` operator and try to make use of a limited set of classes. I don't really like using preprocessors as they can make your code more obscure
Malcolm
I didn't meant to use preprocessor to manipulate the text. See the response for clarification.
David Rabinowitz
I can not know what the superclasses may be, since I am implementing framework. I am not in control on what stylers clients wants to use. I just want to create a flexible and lazy loading method for fetching the correct styler for any given class.
PeyloW
+1  A: 

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.
finnw
Good enough to work with. I think I will require all View subclasses to call `registerClass(MyClass.class)` as a static initializer. Hard to enforce, but good enough. The ugly hack of yours is not doable, since `System.err` if `final` in CLDC, I have already contemplated over this as well :(.
PeyloW
No standard way to get a stack trace in J2ME. Either run this in an emulator or use a Symbian phone and its "redirect://" GCF protocol.
QuickRecipesOnSymbianOS