From a162cc5d3fc6ed167ce36de2a71cdb3a32ca9964 Mon Sep 17 00:00:00 2001 From: Taewan Kim Date: Mon, 3 Jun 2024 08:28:37 +0900 Subject: [PATCH] chore: Add javadoc comments and small fix for Gson Signed-off-by: Taewan Kim --- .../scanner/LPVSScanossDetectService.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java index 1936d029..c076dc6a 100644 --- a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java @@ -146,12 +146,11 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception { public List checkLicenses(LPVSQueue webhookConfig) { List detectedFiles = new ArrayList<>(); try { - Gson gson = new Gson(); Reader reader = getReader(webhookConfig); // convert JSON file to map Map> map = - gson.fromJson( + new Gson().fromJson( reader, new TypeToken>>() {}.getType()); if (null == map) { log.error("Error parsing Json File"); @@ -166,7 +165,7 @@ public List checkLicenses(LPVSQueue webhookConfig) { file.setFilePath(entry.getKey().toString()); String content = buildContent(entry); - ScanossJsonStructure object = getScanossJsonStructure(gson, content, file); + ScanossJsonStructure object = getScanossJsonStructure(content, file); Set licenses = buildLicenseSet(object); file.setLicenses(new HashSet<>(licenses)); if (!file.getLicenses().isEmpty()) detectedFiles.add(file); @@ -181,6 +180,12 @@ public List checkLicenses(LPVSQueue webhookConfig) { return detectedFiles; } + /** + * Builds a set of LPVSLicense objects detected by Scanoss in the given ScanossJsonStructure structure. + * + * @param object The ScanossJsonStructure containing information about the licenses detected by Scanoss. + * @return A set of LPVSLicense objects representing the detected licenses with additional metadata. + */ private Set buildLicenseSet(ScanossJsonStructure object) { if (object.licenses == null) { return new HashSet<>(); @@ -207,8 +212,15 @@ private Set buildLicenseSet(ScanossJsonStructure object) { return licenses; } - private ScanossJsonStructure getScanossJsonStructure(Gson gson, String content, LPVSFile file) { - ScanossJsonStructure object = gson.fromJson(content, ScanossJsonStructure.class); + /** + * Parses the content returned by Scanoss and populates the given LPVSFile entity with the relevant information. + * + * @param content The string content returned by Scanoss. + * @param file The LPVSFile entity to be populated with the parsed information. + * @return The parsed ScanossJsonStructure object containing the extracted information. + */ + private ScanossJsonStructure getScanossJsonStructure(String content, LPVSFile file) { + ScanossJsonStructure object = new Gson().fromJson(content, ScanossJsonStructure.class); if (object.id != null) file.setSnippetType(object.id); if (object.matched != null) file.setSnippetMatch(object.matched); if (object.lines != null) file.setMatchedLines(object.lines); @@ -222,6 +234,13 @@ private ScanossJsonStructure getScanossJsonStructure(Gson gson, String content, return object; } + /** + * Creates a new BufferedReader object for reading the JSON file containing the scan results. + * + * @param webhookConfig The LPVSQueue representing the GitHub webhook configuration. + * @return A BufferedReader object for reading the JSON file. + * @throws IOException If an error occurs while creating the BufferedReader object. + */ private static Reader getReader(LPVSQueue webhookConfig) throws IOException { String fileName = null; if (webhookConfig.getHeadCommitSHA() == null @@ -243,6 +262,12 @@ private static Reader getReader(LPVSQueue webhookConfig) throws IOException { + ".json")); } + /** + * Builds the content string for the given Map.Entry object. + * + * @param entry The Map.Entry object containing the information to be included in the content string. + * @return The constructed content string. + */ private static String buildContent(Map.Entry> entry) { String content = entry.getValue() @@ -267,8 +292,7 @@ private static String buildContent(Map.Entry> entry) { if (content.endsWith("}")) { content = content.substring(0, content.length() - 1) + "\"}"; } - content = content.replaceAll("}\"}", "\"}}"); - return content; + return content.replaceAll("}\"}", "\"}}"); } /**