Ejemplo n.º 1
0
  /**
   * Returns the list of variants that can be converted from a given object class.
   *
   * @param sourceClass The source class.
   * @param targetVariant The expected representation metadata.
   * @return The list of variants that can be converted.
   */
  public static List<VariantInfo> getVariants(Class<?> sourceClass, Variant targetVariant) {
    List<VariantInfo> result = null;
    List<VariantInfo> helperVariants = null;

    for (ConverterHelper ch : Engine.getInstance().getRegisteredConverters()) {
      // List of variants that can be converted from the source class
      helperVariants = ch.getVariants(sourceClass);

      if (helperVariants != null) {
        // Loop over the variants list
        for (VariantInfo helperVariant : helperVariants) {
          if (targetVariant == null) {
            result = addVariant(result, helperVariant);
          } else if (helperVariant.includes(targetVariant)) {
            // Detected a more generic variant, but still consider
            // the conversion is possible to the target variant.
            result = addVariant(result, new VariantInfo(targetVariant.getMediaType()));
          } else if (targetVariant.includes(helperVariant)) {
            // Detected a more specific variant, but still consider
            // the conversion is possible to the target variant.
            result = addVariant(result, helperVariant);
          }
        }
      }
    }

    return result;
  }