From 27c8a858f2abd89ae7c1b830025fbe91bd6da90d Mon Sep 17 00:00:00 2001 From: z30034863 Date: Wed, 7 May 2025 22:16:35 +0800 Subject: [PATCH] fix CleanCode warn 20250507 Signed-off-by: z30034863 --- adapter/ohos/Compressor.java | 81 ++++++++++++++++---------------- adapter/ohos/ModuleJsonUtil.java | 3 +- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/adapter/ohos/Compressor.java b/adapter/ohos/Compressor.java index e4a74a8f..8b0ce8c4 100644 --- a/adapter/ohos/Compressor.java +++ b/adapter/ohos/Compressor.java @@ -545,51 +545,50 @@ public class Compressor { LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString(errMsg)); throw new BundleException("Set generate build hash failed for --json-path file does not exist."); } - InputStream json = null; - BufferedWriter bw = null; - try { - json = new FileInputStream(file); - JSONObject jsonObject = JSON.parseObject(json, JSONObject.class); - if (!jsonObject.containsKey(APP) || !jsonObject.containsKey(MODULE)) { - LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString("Parse --json-path file is invalid.")); - throw new BundleException("The --json-path file is invalid."); - } - JSONObject appJson = jsonObject.getJSONObject(APP); - JSONObject moduleJson = jsonObject.getJSONObject(MODULE); - if (appJson.containsKey(GENERATE_BUILD_HASH) && appJson.getBoolean(GENERATE_BUILD_HASH)) { - utility.setGenerateBuildHash(true); - } else { - if (moduleJson.containsKey(GENERATE_BUILD_HASH) && moduleJson.getBoolean(GENERATE_BUILD_HASH)) { - utility.setGenerateBuildHash(true); - } - } - appJson.remove(GENERATE_BUILD_HASH); - moduleJson.remove(GENERATE_BUILD_HASH); + // 1. 解析 JSON + JSONObject jsonObject; + try (InputStream json = Files.newInputStream(file.toPath())) { + jsonObject = JSON.parseObject(json, JSONObject.class); + } catch (IOException | JSONException e) { + LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString( + "Failed to read JSON file: " + e.getMessage())); + throw new BundleException("Failed to read JSON file"); + } + //2. 检查必要字段 + if (jsonObject == null || !jsonObject.containsKey(APP) || !jsonObject.containsKey(MODULE)) { + LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString("Parse --json-path file is invalid.")); + throw new BundleException("The --json-path file is invalid."); + } + //3. 处理 JSON 数据 + processBuildHashFlags(utility, jsonObject); + //4. 写入修改后的 JSON + writeJsonFile(utility.getJsonPath(), jsonObject); + } + + private static void processBuildHashFlags(Utility utility, JSONObject jsonObject) { + JSONObject appJson = jsonObject.getJSONObject(APP); + JSONObject moduleJson = jsonObject.getJSONObject(MODULE); + if (appJson.containsKey(GENERATE_BUILD_HASH) && appJson.getBoolean(GENERATE_BUILD_HASH)) { + utility.setGenerateBuildHash(true); + } else if (moduleJson.containsKey(GENERATE_BUILD_HASH) && moduleJson.getBoolean(GENERATE_BUILD_HASH)) { + utility.setGenerateBuildHash(true); + } + appJson.remove(GENERATE_BUILD_HASH); + moduleJson.remove(GENERATE_BUILD_HASH); + } + + private static void writeJsonFile(String filePath, JSONObject jsonObject) throws BundleException { + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + Files.newOutputStream(Paths.get(filePath)), StandardCharsets.UTF_8))) { String pretty = JSON.toJSONString(jsonObject, new SerializerFeature[]{ - SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, + SerializerFeature.PrettyFormat, + SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat}); - bw = new BufferedWriter(new OutputStreamWriter( - new FileOutputStream(utility.getJsonPath()), StandardCharsets.UTF_8)); bw.write(pretty); - } catch (BundleException exception) { - LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString("Set generate build hash exist BundleException: " + exception.getMessage())); - throw new BundleException("Compressor::setGenerateBuildHash failed."); - } catch (NullPointerException | IOException | JSONException e) { + } catch (IOException e) { LOG.error(PackingToolErrMsg.SET_GENERATE_BUILD_HASH.toString( - "json data err, exist Exception(NullPointerException | IOException | JSONException): " + e.getMessage())); - throw new BundleException("Set generate build hash failed, json data err."); - } finally { - FileUtils.closeStream(json); - if (bw != null) { - try { - bw.flush(); - bw.close(); - } catch (IOException e) { - LOG.error(PackingToolErrMsg.IO_EXCEPTION.toString( - "Set generate build hash exist IOException: " + e.getMessage())); - throw new BundleException("Set generate build hash failed."); - } - } + "Failed to write JSON file: " + e.getMessage())); + throw new BundleException("Failed to write JSON file"); } } diff --git a/adapter/ohos/ModuleJsonUtil.java b/adapter/ohos/ModuleJsonUtil.java index 1ea7c04f..f5c0d12d 100644 --- a/adapter/ohos/ModuleJsonUtil.java +++ b/adapter/ohos/ModuleJsonUtil.java @@ -1964,7 +1964,8 @@ class ModuleJsonUtil { boolean installationFree = getJsonBooleanValue(moduleObj, INSTALLATION_FREE, false); if (!appObj.containsKey(BUNDLE_TYPE)) { if (installationFree) { - String errMsg = "The app.json5 file configuration does not match the 'installationFree' setting of true."; + String errMsg = "The app.json5 file configuration" + + " does not match the 'installationFree' setting of true."; String solution = "Add the 'bundleType' field to the app.json5 file and set it atomicService."; LOG.error(PackingToolErrMsg.CHECK_ATOMIC_SERVICE_INSTALLATION_FREE_FAILED.toString(errMsg, solution)); return false; -- Gitee