From 3cd1b6e7c4272b67978f233b49ee2d2856392f0d Mon Sep 17 00:00:00 2001 From: Seah Shau Chung Nicholas <97350362+nseah21@users.noreply.github.com> Date: Sun, 31 Dec 2023 16:35:26 +0100 Subject: [PATCH] [#1989] Reduce scope of try-catch block in ArgsParser::parse (#2074) The old scope of the try block in ArgsParser::parse is too permissive, making it less apparent which methods throw the associated exceptions. Let's reduce the scope of the try block in ArgsParser::parse. --- .../java/reposense/parser/ArgsParser.java | 128 +++++++++--------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/src/main/java/reposense/parser/ArgsParser.java b/src/main/java/reposense/parser/ArgsParser.java index ff6ea7ae95..91a46c180f 100644 --- a/src/main/java/reposense/parser/ArgsParser.java +++ b/src/main/java/reposense/parser/ArgsParser.java @@ -262,76 +262,78 @@ private static ArgumentParser getArgumentParser() { * @throws ParseException if the given string arguments fails to parse to a {@link CliArguments} object. */ public static CliArguments parse(String[] args) throws HelpScreenException, ParseException { + ArgumentParser parser = getArgumentParser(); + Namespace results; + try { - ArgumentParser parser = getArgumentParser(); - Namespace results = parser.parseArgs(args); - - Path configFolderPath = results.get(CONFIG_FLAGS[0]); - Path reportFolderPath = results.get(VIEW_FLAGS[0]); - Path outputFolderPath = results.get(OUTPUT_FLAGS[0]); - ZoneId zoneId = results.get(TIMEZONE_FLAGS[0]); - Path assetsFolderPath = results.get(ASSETS_FLAGS[0]); - List locations = results.get(REPO_FLAGS[0]); - List formats = FileType.convertFormatStringsToFileTypes(results.get(FORMAT_FLAGS[0])); - boolean isStandaloneConfigIgnored = results.get(IGNORE_CONFIG_FLAGS[0]); - boolean isFileSizeLimitIgnored = results.get(IGNORE_SIZELIMIT_FLAGS[0]); - boolean shouldIncludeLastModifiedDate = results.get(LAST_MODIFIED_DATE_FLAGS[0]); - boolean shouldPerformShallowCloning = results.get(SHALLOW_CLONING_FLAGS[0]); - boolean shouldFindPreviousAuthors = results.get(FIND_PREVIOUS_AUTHORS_FLAGS[0]); - boolean isTestMode = results.get(TEST_MODE_FLAG[0]); - int numCloningThreads = results.get(CLONING_THREADS_FLAG[0]); - int numAnalysisThreads = results.get(ANALYSIS_THREADS_FLAG[0]); - - CliArguments.Builder cliArgumentsBuilder = new CliArguments.Builder() - .configFolderPath(configFolderPath) - .reportDirectoryPath(reportFolderPath) - .outputFilePath(outputFolderPath) - .zoneId(zoneId) - .assetsFilePath(assetsFolderPath) - .locations(locations) - .formats(formats) - .isStandaloneConfigIgnored(isStandaloneConfigIgnored) - .isFileSizeLimitIgnored(isFileSizeLimitIgnored) - .isLastModifiedDateIncluded(shouldIncludeLastModifiedDate) - .isShallowCloningPerformed(shouldPerformShallowCloning) - .isFindingPreviousAuthorsPerformed(shouldFindPreviousAuthors) - .numCloningThreads(numCloningThreads) - .numAnalysisThreads(numAnalysisThreads) - .isTestMode(isTestMode); - - LogsManager.setLogFolderLocation(outputFolderPath); - - if (locations == null && configFolderPath.equals(DEFAULT_CONFIG_PATH)) { - logger.info(MESSAGE_USING_DEFAULT_CONFIG_PATH); - } + results = parser.parseArgs(args); + } catch (HelpScreenException hse) { + throw hse; + } catch (ArgumentParserException ape) { + throw new ParseException(getArgumentParser().formatUsage() + ape.getMessage() + "\n"); + } - addReportConfigToBuilder(cliArgumentsBuilder, results); - addAnalysisDatesToBuilder(cliArgumentsBuilder, results); + Path configFolderPath = results.get(CONFIG_FLAGS[0]); + Path reportFolderPath = results.get(VIEW_FLAGS[0]); + Path outputFolderPath = results.get(OUTPUT_FLAGS[0]); + ZoneId zoneId = results.get(TIMEZONE_FLAGS[0]); + Path assetsFolderPath = results.get(ASSETS_FLAGS[0]); + List locations = results.get(REPO_FLAGS[0]); + List formats = FileType.convertFormatStringsToFileTypes(results.get(FORMAT_FLAGS[0])); + boolean isStandaloneConfigIgnored = results.get(IGNORE_CONFIG_FLAGS[0]); + boolean isFileSizeLimitIgnored = results.get(IGNORE_SIZELIMIT_FLAGS[0]); + boolean shouldIncludeLastModifiedDate = results.get(LAST_MODIFIED_DATE_FLAGS[0]); + boolean shouldPerformShallowCloning = results.get(SHALLOW_CLONING_FLAGS[0]); + boolean shouldFindPreviousAuthors = results.get(FIND_PREVIOUS_AUTHORS_FLAGS[0]); + boolean isTestMode = results.get(TEST_MODE_FLAG[0]); + int numCloningThreads = results.get(CLONING_THREADS_FLAG[0]); + int numAnalysisThreads = results.get(ANALYSIS_THREADS_FLAG[0]); + + CliArguments.Builder cliArgumentsBuilder = new CliArguments.Builder() + .configFolderPath(configFolderPath) + .reportDirectoryPath(reportFolderPath) + .outputFilePath(outputFolderPath) + .zoneId(zoneId) + .assetsFilePath(assetsFolderPath) + .locations(locations) + .formats(formats) + .isStandaloneConfigIgnored(isStandaloneConfigIgnored) + .isFileSizeLimitIgnored(isFileSizeLimitIgnored) + .isLastModifiedDateIncluded(shouldIncludeLastModifiedDate) + .isShallowCloningPerformed(shouldPerformShallowCloning) + .isFindingPreviousAuthorsPerformed(shouldFindPreviousAuthors) + .numCloningThreads(numCloningThreads) + .numAnalysisThreads(numAnalysisThreads) + .isTestMode(isTestMode); + + LogsManager.setLogFolderLocation(outputFolderPath); + + if (locations == null && configFolderPath.equals(DEFAULT_CONFIG_PATH)) { + logger.info(MESSAGE_USING_DEFAULT_CONFIG_PATH); + } - boolean isViewModeOnly = reportFolderPath != null - && !reportFolderPath.equals(EMPTY_PATH) - && configFolderPath.equals(DEFAULT_CONFIG_PATH) - && locations == null; - cliArgumentsBuilder.isViewModeOnly(isViewModeOnly); + addReportConfigToBuilder(cliArgumentsBuilder, results); + addAnalysisDatesToBuilder(cliArgumentsBuilder, results); - boolean isAutomaticallyLaunching = reportFolderPath != null; - if (isAutomaticallyLaunching && !reportFolderPath.equals(EMPTY_PATH) && !isViewModeOnly) { - logger.info(String.format("Ignoring argument '%s' for --view.", reportFolderPath.toString())); - } - cliArgumentsBuilder.isAutomaticallyLaunching(isAutomaticallyLaunching); + boolean isViewModeOnly = reportFolderPath != null + && !reportFolderPath.equals(EMPTY_PATH) + && configFolderPath.equals(DEFAULT_CONFIG_PATH) + && locations == null; + cliArgumentsBuilder.isViewModeOnly(isViewModeOnly); + boolean isAutomaticallyLaunching = reportFolderPath != null; + if (isAutomaticallyLaunching && !reportFolderPath.equals(EMPTY_PATH) && !isViewModeOnly) { + logger.info(String.format("Ignoring argument '%s' for --view.", reportFolderPath.toString())); + } + cliArgumentsBuilder.isAutomaticallyLaunching(isAutomaticallyLaunching); - boolean shouldPerformFreshCloning = isTestMode - ? results.get(FRESH_CLONING_FLAG[0]) - : DEFAULT_SHOULD_FRESH_CLONE; - cliArgumentsBuilder.isFreshClonePerformed(shouldPerformFreshCloning); - return cliArgumentsBuilder.build(); - } catch (HelpScreenException hse) { - throw hse; - } catch (ArgumentParserException ape) { - throw new ParseException(getArgumentParser().formatUsage() + ape.getMessage() + "\n"); - } + boolean shouldPerformFreshCloning = isTestMode + ? results.get(FRESH_CLONING_FLAG[0]) + : DEFAULT_SHOULD_FRESH_CLONE; + cliArgumentsBuilder.isFreshClonePerformed(shouldPerformFreshCloning); + + return cliArgumentsBuilder.build(); } /**