public static Config fromJSONFile()

in legacy/java/piranha/src/main/java/com/uber/piranha/config/Config.java [348:457]


  public static Config fromJSONFile(String configFile, boolean isArgumentIndexOptional)
      throws PiranhaConfigurationException {
    try {
      Path configFilePath = Paths.get(configFile);
      boolean configFileExists = configFilePath.toFile().exists();
      if (!configFileExists) {
        // We will catch this with other IOExceptions and wrap it around as
        // PiranhaConfigurationException (See catch block)
        throw new IOException("Provided config file not found");
      }

      String linkURL = DEFAULT_PIRANHA_URL;
      ImmutableMultimap.Builder<String, MethodRecord> unnecessaryTestMethodsBuilder =
          ImmutableMultimap.builder();
      ImmutableMultimap.Builder<String, PiranhaMethodRecord> flagMethodsBuilder =
          ImmutableMultimap.builder();
      ImmutableMultimap.Builder<String, PiranhaEnumRecord> enumsBuilder =
          ImmutableMultimap.builder();
      TestAnnotationResolver.Builder annotationResolverBuilder = TestAnnotationResolver.builder();

      JSONParser parser = new JSONParser();
      JSONObject propertiesJson =
          (JSONObject)
              parser.parse(Files.newBufferedReader(configFilePath, StandardCharsets.UTF_8));
      if (propertiesJson.containsKey(LINK_URL_KEY)) {
        linkURL = (String) propertiesJson.get(LINK_URL_KEY);
      }
      if (propertiesJson.get(ANNOTATIONS_KEY) != null) {
        for (Object annotationJSON : (List<Object>) propertiesJson.get(ANNOTATIONS_KEY)) {
          if (annotationJSON instanceof String) {
            annotationResolverBuilder.addSpecFromName((String) annotationJSON);
          } else if (annotationJSON instanceof JSONObject) {
            annotationResolverBuilder.addSpecFromJSONObject((JSONObject) annotationJSON);
          } else {
            throw new PiranhaConfigurationException(
                "Unexpected annotation specification format inside "
                    + ANNOTATIONS_KEY
                    + " : "
                    + annotationJSON.toString());
          }
        }
      }
      if (propertiesJson.get(METHODS_KEY) != null) {
        for (Map<String, Object> methodProperty :
            (List<Map<String, Object>>) propertiesJson.get(METHODS_KEY)) {
          PiranhaMethodRecord methodRecord =
              PiranhaMethodRecord.parseFromJSONPropertyEntryMap(
                  methodProperty, isArgumentIndexOptional);
          flagMethodsBuilder.put(methodRecord.getMethodName(), methodRecord);
        }
        if (propertiesJson.get(UNNECESSARY_TEST_METHOD_KEY) != null) {
          for (Map<String, Object> methodProperty :
              (List<Map<String, Object>>) propertiesJson.get(UNNECESSARY_TEST_METHOD_KEY)) {
            MethodRecord methodRecord =
                MethodRecord.parseFromJSONPropertyEntryMap(methodProperty, isArgumentIndexOptional);
            unnecessaryTestMethodsBuilder.put(methodRecord.getMethodName(), methodRecord);
          }
        }
      } else {
        throw new PiranhaConfigurationException("methodProperties not found, required.");
      }
      if (propertiesJson.get(ENUMS_KEY) != null) {
        for (Map<String, Object> enumProperty :
            (List<Map<String, Object>>) propertiesJson.get(ENUMS_KEY)) {
          PiranhaEnumRecord enumRecord =
              PiranhaEnumRecord.parseFromJSONPropertyEntryMap(
                  enumProperty, isArgumentIndexOptional);
          enumsBuilder.put(enumRecord.getEnumName(), enumRecord);
        }
      }
      ImmutableMap.Builder<String, Object> cleanupOptionsBuilder = ImmutableMap.builder();
      final Object configOptsObj = propertiesJson.get(CLEANUP_OPTS_KEY);
      if (configOptsObj != null && configOptsObj instanceof JSONObject) {
        final JSONObject configOpts = (JSONObject) configOptsObj;
        configOpts.forEach(
            (k, v) -> {
              String valK = validateConfigOptsKey(k);
              validateConfigOptsValue(valK, v);
              cleanupOptionsBuilder.put(valK, v);
            });
      }
      return new Config(
          flagMethodsBuilder.build(),
          unnecessaryTestMethodsBuilder.build(),
          enumsBuilder.build(),
          annotationResolverBuilder.build(),
          cleanupOptionsBuilder.build(),
          linkURL);
    } catch (IOException fnfe) {
      throw new PiranhaConfigurationException(
          "Error reading config file " + Paths.get(configFile).toAbsolutePath() + " : " + fnfe);
    } catch (ParseException pe) {
      String extraWarning = "";
      if (configFile.endsWith(".properties")) {
        // Ends in space to make link clickable on terminal.
        extraWarning =
            "\nWARNING: With version 0.1.0, PiranhaJava has changed its configuration file format to json "
                + "(properties.json), but it looks you are passing the old piranha.properties format. Please "
                + "migrate your configuration to json. "
                + "See. https://github.com/uber/piranha/blob/master/java/README.md ";
      }
      throw new PiranhaConfigurationException(
          "Invalid or incorrectly formatted config file. " + pe + extraWarning);
    } catch (PiranhaConfigurationException pce) {
      // Already in the right format, re-throw, to avoid falling on catch-all below.
      throw pce;
    } catch (Exception e) {
      throw new PiranhaConfigurationException("Some other exception thrown while parsing config");
    }
  }