Esempio n. 1
0
  /**
   * Replace any references to the <i>referenceObject</i> with the <i>replacementObject</i>. The
   * model will be identified as having a reference it there are any "getXXX()" methods on the model
   * that return the referenced object.
   *
   * @param modelObject
   * @param referencedObject
   * @return
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   */
  public static void replaceReferenceTo(
      Object modelObject, Object referencedObject, Object replacementObject)
      throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    // inspect user object to determine if it has any methods that return an object
    // of the type we want
    List<Method[]> getSetPairs =
        org.openhealthtools.mdht.mdmi.editor.common.ClassUtil.getMethodPairs(
            modelObject.getClass(), referencedObject.getClass());
    if (getSetPairs.size() != 0) {
      for (Method[] methodPair : getSetPairs) {
        Method getMethod = methodPair[0];
        Method setMethod = methodPair[1];
        Object objectInModel = null;
        try {
          objectInModel = getMethod.invoke(modelObject);
        } catch (Exception e) {
          // don't care on the "get"
        }

        if (objectInModel != null) {
          // did we find it?
          if (referencedObject.equals(objectInModel)) {
            // change it
            setMethod.invoke(modelObject, replacementObject);
          }
        }
      }
    }
  }
Esempio n. 2
0
  /**
   * Determine if the provided model has any references to the <i>referenceObject</i>. The model
   * will be identified as having a reference it there are any "getXXX()" methods on the model that
   * return the referenced object.
   *
   * @param modelObject
   * @param referencedObject
   * @return
   */
  private static boolean hasReferenceTo(Object modelObject, Object referencedObject) {

    // inspect user object to determine if it has any methods that return an object
    // of the type we want
    List<Method[]> getSetPairs =
        org.openhealthtools.mdht.mdmi.editor.common.ClassUtil.getMethodPairs(
            modelObject.getClass(), referencedObject.getClass());
    if (getSetPairs.size() != 0) {
      for (Method[] methodPair : getSetPairs) {
        Method getMethod = methodPair[0];
        try {
          Object objectInModel = getMethod.invoke(modelObject);

          // did we find it?
          if (referencedObject.equals(objectInModel)) {
            return true;
          }
        } catch (Exception e) {
          // don't care
        }
      }
    }

    return false;
  }