Example #1
0
 int[] arrayDimensions(Object a_object) {
   int count = 0;
   for (Class clazz = a_object.getClass(); clazz.isArray(); clazz = clazz.getComponentType()) {
     count++;
   }
   int dim[] = new int[count];
   for (int i = 0; i < count; i++) {
     dim[i] = Array.getLength(a_object);
     a_object = Array.get(a_object, 0);
   }
   return dim;
 }
Example #2
0
 boolean hasPublicConstructor(Class a_class) {
   if (a_class != String.class) {
     try {
       return a_class.newInstance() != null;
     } catch (Throwable t) {
     }
   }
   return false;
 }
Example #3
0
  public boolean isEqual(Object a_compare, Object a_with, String a_path, Stack a_stack) {
    if (a_path == null || a_path.length() < 1) {
      if (a_compare != null) {
        a_path = a_compare.getClass().getName() + ":";
      } else {
        if (a_with != null) {
          a_path = a_with.getClass().getName() + ":";
        }
      }
    }

    String path = a_path;

    if (a_compare == null) {
      return a_with == null;
    }
    if (a_with == null) {
      return false;
    }
    Class clazz = a_compare.getClass();
    if (clazz != a_with.getClass()) {
      return false;
    }

    if (isSimple(clazz)) {
      return a_compare.equals(a_with);
    }

    if (a_stack == null) {
      a_stack = new Stack();
    }

    // takes care of repeating calls to the same object
    if (a_stack.contains(a_compare)) {
      return true;
    }
    a_stack.push(a_compare);

    Field fields[] = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
      if (storeableField(clazz, fields[i])) {
        Platform4.setAccessible(fields[i]);
        try {
          path = a_path + fields[i].getName() + ":";
          Object compare = fields[i].get(a_compare);
          Object with = fields[i].get(a_with);
          if (compare == null) {
            if (with != null) {
              return false;
            }
          } else if (with == null) {
            return false;
          } else {
            if (compare.getClass().isArray()) {
              if (!with.getClass().isArray()) {
                return false;
              } else {
                compare = normalizeNArray(compare);
                with = normalizeNArray(with);
                int len = Array.getLength(compare);
                if (len != Array.getLength(with)) {
                  return false;
                } else {
                  for (int j = 0; j < len; j++) {
                    Object elementCompare = Array.get(compare, j);
                    Object elementWith = Array.get(with, j);
                    //										if (l_persistentArray)
                    if (!isEqual(elementCompare, elementWith, path, a_stack)) {
                      return false;
                    } else if (elementCompare == null) {
                      if (elementWith != null) {
                        return false;
                      }
                    } else if (elementWith == null) {
                      return false;
                    } else {
                      Class elementCompareClass = elementCompare.getClass();
                      if (elementCompareClass != elementWith.getClass()) {
                        return false;
                      }
                      if (hasPublicConstructor(elementCompareClass)) {
                        if (!isEqual(elementCompare, elementWith, path, a_stack)) {
                          return false;
                        }
                      } else if (!elementCompare.equals(elementWith)) {
                        return false;
                      }
                    }
                  }
                }
              }
            } else if (hasPublicConstructor(fields[i].getType())) {
              if (!isEqual(compare, with, path, a_stack)) {
                return false;
              }
            } else {
              if (!compare.equals(with)) {
                return false;
              }
            }
          }
        } catch (IllegalAccessException ex) {
          // probably JDK 1
          // never mind this field
          return true;
        } catch (Exception e) {
          System.err.println("STCompare failure executing path:" + path);
          e.printStackTrace();
          return false;
        }
      }
    }
    return true;
  }