Replacing Class.getSuperclass () with Java ME?

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

I want the abstract superclass to 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;
  }
}

The subclass can then add the following specializations:

 public Styler createStylerForViewClass(Class clazz) {
   if (clazz == SpecialView.class) {
     return new SpecialStyler();
   } else {
     return super.createSylerForViewClass(clazz);
   }
 }
+3
source share
2 answers

As you have already discovered, MIDP does not provide a method for obtaining a superclass of a class or for listing all classes in an application.

So, all you can do is keep track of the class hierarchy yourself.

, ( ) :

abstract class View {
    protected View() {
        classHierarchy.add(this.getClass());
    }
}

, , , .

/ . :.

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;
}

"" , , . :

Object >= View >= A >= B >= C

A C C, A, B, A C, , C.

, , , ( ). , , createStylerForViewClass, .

, :

  • View Exception, .
  • System.err , ByteArrayOutputStream
  • printStackTrace()
  • System.err
  • ByteArrayOutputStream. . Class.forName() .
+1

:

, , instanceof Class.isInstance().

, , , . , doclet. , :

 ClassA:SuperClass
 ClassB:AnotherSuperClass
 etc.

, .

+1

Source: https://habr.com/ru/post/1729246/


All Articles