public CrossReference toCrossReference(O cvObject) {
    if (cvObject == null) {
      throw new IllegalArgumentException("CvObject must not be null. ");
    }

    // name of the cv is the fullname
    String text =
        cvObject.getFullName() != null ? cvObject.getFullName() : cvObject.getShortLabel();
    String identity = cvObject.getIdentifier();

    if (identity == null) {
      throw new NullPointerException(
          cvObject.getClass().getSimpleName() + "(" + text + ") didn't have an identity");
    }

    final CvObjectXref idXref =
        findMatchingIdentityXref(
            cvObject.getXrefs(),
            identity); // XrefUtils.getIdentityXref(cvObject, CvDatabase.PSI_MI_MI_REF);

    if (idXref != null) {
      try {
        CrossReference ref = crossRefConverter.createCrossReference(idXref, false);
        ref.setText(text);
        return ref;
      } catch (Exception e) {
        throw new RuntimeException("An exception occured while building a cv object : " + text, e);
      }
    } else {
      CrossReference ref =
          new CrossReferenceImpl(CrossReferenceConverter.DATABASE_UNKNOWN, identity, text);
      return ref;
    }
  }
 @SuppressWarnings("unchecked")
 <O, R> R convert(O input, Class<R> type) {
   Class<O> inputType = (Class<O>) input.getClass();
   if (inputType == type) {
     return (R) input;
   }
   ETransformer<O, R> transformer = transformerManager.getTransformerSafe(inputType, type);
   return transformer.from(input);
 }
  @SuppressWarnings({
    "unchecked",
    "rawtypes"
  }) // OK, because type.isEnum() is checked before casting to Enum
  private static <O> void applySetters(
      final O instance, final Map<String, Method> methodMap, final Map<String, String> attributes) {

    for (Map.Entry<String, String> attribute : attributes.entrySet()) {
      final String methodName = attribute.getKey().toLowerCase();
      final Method method = methodMap.get(methodName);
      if (null == method) {
        throw new MetafactureException(
            "Method '"
                + methodName
                + "' does not exist in '"
                + instance.getClass().getSimpleName()
                + "'!");
      }
      final Class<?> type = method.getParameterTypes()[0];

      try {
        if (type == boolean.class) {
          method.invoke(instance, Boolean.valueOf(attribute.getValue()));
        } else if (type == int.class) {
          method.invoke(instance, Integer.valueOf(attribute.getValue()));
        } else if (type.isEnum()) {
          method.invoke(
              instance, Enum.valueOf((Class<Enum>) type, attribute.getValue().toUpperCase()));
        } else {
          method.invoke(instance, attribute.getValue());
        }
      } catch (IllegalArgumentException e) {
        setMethodError(methodName, instance.getClass().getSimpleName(), e);
      } catch (IllegalAccessException e) {
        setMethodError(methodName, instance.getClass().getSimpleName(), e);
      } catch (InvocationTargetException e) {
        setMethodError(methodName, instance.getClass().getSimpleName(), e);
      }
    }
  }
Exemple #4
0
 /**
  * Append the given object to the given array, returning a new array consisting of the input array
  * contents plus the given object.
  *
  * @param array the array to append to (can be {@code null})
  * @param obj the object to append
  * @return the new array (of the same component type; never {@code null})
  */
 public static <A, O extends A> A[] addObjectToArray(A[] array, O obj) {
   Class<?> compType = Object.class;
   if (array != null) {
     compType = array.getClass().getComponentType();
   } else if (obj != null) {
     compType = obj.getClass();
   }
   int newArrLength = (array != null ? array.length + 1 : 1);
   @SuppressWarnings("unchecked")
   A[] newArr = (A[]) Array.newInstance(compType, newArrLength);
   if (array != null) {
     System.arraycopy(array, 0, newArr, 0, array.length);
   }
   newArr[newArr.length - 1] = obj;
   return newArr;
 }
Exemple #5
0
  public static <A, O extends A> A[] addObjectToArray(A[] array, O obj) {
    Class compType = Object.class;
    if (array != null) {
      compType = array.getClass().getComponentType();
    } else if (obj != null) {
      compType = obj.getClass();
    }

    int newArrLength = array != null ? array.length + 1 : 1;
    Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
    if (array != null) {
      System.arraycopy(array, 0, newArr, 0, array.length);
    }

    newArr[newArr.length - 1] = obj;
    return (A[]) newArr;
  }
  @SuppressWarnings("unchecked")
  <O, R> List<R> convert(Iterable<O> inputs, Class<R> type) {
    Iterator<O> iterator = inputs.iterator();
    if (!iterator.hasNext()) {
      return Collections.emptyList();
    }

    List<R> results = new ArrayList<R>();
    ETransformer<O, R> transformer = null;
    for (O t : inputs) {
      if (transformer == null) {
        Class<O> inputType = (Class<O>) t.getClass();
        transformer = transformerManager.getTransformerSafe(inputType, type);
      }
      results.add(transformer.from(t));
    }

    return results;
  }
  @Override
  public <O> O retrieve(O model, ConfigurationItem configuration) throws Exception {

    // Obtain the XmlUnmarshaller for the Model
    XmlUnmarshaller unmarshaller = this.obtainUnmarshaller(model.getClass());

    // Obtain configuration
    InputStream configInput = configuration.getConfiguration();

    try {
      // Configure and register the Model
      unmarshaller.unmarshall(configInput, model);
    } finally {
      // Close the stream
      configInput.close();
    }

    // Return the configured model
    return model;
  }
 public static <O> void applySetters(final O instance, final Map<String, String> attributes) {
   applySetters(instance, extractMethods(instance.getClass()), attributes);
 }