Exemplo n.º 1
0
 private static void vis(String name, Class type, Object value, String indent) throws Exception {
   if (value == null) { // nothing interesting to say
     prt(indent + name + ": null");
   } else if (type.isPrimitive()) { // primitive
     if (type == Boolean.TYPE) prt(indent + name + ": " + ((Boolean) value).booleanValue());
     else if (type == Character.TYPE) prt(indent + name + ": " + ((Character) value).charValue());
     else if (type == Byte.TYPE) prt(indent + name + ": " + ((Byte) value).byteValue());
     else if (type == Short.TYPE) prt(indent + name + ": " + ((Short) value).shortValue());
     else if (type == Integer.TYPE) prt(indent + name + ": " + ((Integer) value).intValue());
     else if (type == Long.TYPE) prt(indent + name + ": " + ((Long) value).longValue());
     else if (type == Float.TYPE) prt(indent + name + ": " + ((Float) value).floatValue());
     else if (type == Double.TYPE) prt(indent + name + ": " + ((Double) value).doubleValue());
     else throw new IllegalArgumentException(); // NotReached
   } else if (type.isArray()) { // array
     Class componentType = type.getComponentType();
     prt(indent + name + ": " + componentType.getName() + "[]");
     for (int i = 0, n = Array.getLength(value); i < n; ++i)
       vis("" + i, componentType, Array.get(value, i), indent + "   ");
   } else if (type.getName()
       .equals("java.lang.String")) { // treat frequently used type as special case
     prt(indent + name + ": \"" + (String) value + "\"");
   } else { // class (or interface)
     Field[] fields =
         type
             .getFields(); // !!note: Sun's jvm only returns *public* fields, jx returns *all*
                           // fields
     if (fields.length == 0) { // no fields
       // prt(indent + name + ": declared as " + type.getName() + " actually is " + (value == null
       // ? "null" : value.getClass().getName()) + " self-described as \"" + value + "\"");
       prt(
           indent
               + name
               + ": declared as "
               + type.getName()
               + " actually is "
               + (value == null ? "null" : value.getClass().getName()));
     } else { // use fields to describe object
       prt(indent + name + ": " + type.getName());
       for (int i = 0, n = fields.length; i < n; ++i) {
         Field f = fields[i];
         if (Modifier.isStatic(f.getModifiers()))
           vis(f.getName(), f.getType(), f.get(value), indent + "   static ");
         else vis(f.getName(), f.getType(), f.get(value), indent + "   ");
       }
     }
   }
 }
Exemplo n.º 2
0
 // Visualize an object
 // Taken: name
 //        type
 //        value (or wrapper, if primitive type)
 //
 static void visualize(String name, Class type, Object value) {
   SystemOut.println("===");
   try {
     vis(name, type, value, "");
   } catch (Exception e) {
     e.printStackTrace(System.out);
   }
 }