コード例 #1
0
  /**
   * Get the name of the enumeration from the specified class. The name to retrieve is which enum
   * matches the passed value.
   */
  private static String getNameOfEnumValue(Class enumType, long value) {
    try {
      // Get all fields in the class.
      Field[] fields = enumType.getFields();

      // / Create a new instance of the enumeration class.
      Object enumObject = enumType.newInstance();

      // Loop through all fields and find the enumeration whose value
      // matches.
      for (Field field : fields) {
        if (field.getLong(enumObject) == value) return field.getName();
      }

      // Couldn't find value, return empty string.
      return "";
    } catch (Exception e) {
      return "";
    }
  }
コード例 #2
0
  /**
   * Aspose.Barcode for Java stores enumerations in classes. This method random enumeration value
   * stored within the specified class. Not all of these classes have built-in methods to retrieve
   * the name and value of the internal enumerations so we will extract them using reflection
   * instead.
   */
  private static int getRandomEnumValue(Class enumType) {
    try {
      // Get all fields of this class.
      Field[] fields = enumType.getFields();

      // Java implements nextInt similarly to .NET, the upper value is
      // exclusive so the result will always be one less
      // than the passed length.
      int num = rnd.nextInt(fields.length);

      // Get a random enumeration.
      Field field = fields[num];

      // Retrieve the value from a new instance of this class.
      Object enumObject = enumType.newInstance();

      // Return the value.
      return field.getInt(enumObject);
    } catch (Exception e) {
      return 0;
    }
  }