From f153bf38dd18fea35c42c7cf3459a38b41ea410b Mon Sep 17 00:00:00 2001 From: xsz233 Date: Sat, 24 Sep 2022 09:47:40 +0800 Subject: [PATCH] fixed ac43892 from https://gitee.com/xsz233/developtools_packing_tool/pulls/297 IssueNo: I5SW2M Descriptioin: handle exception Signed-off-by: xsz233 --- adapter/ohos/JsonUtil.java | 81 ++++++++++++++++++-------------- adapter/ohos/ModuleJsonUtil.java | 21 +++++---- 2 files changed, 57 insertions(+), 45 deletions(-) diff --git a/adapter/ohos/JsonUtil.java b/adapter/ohos/JsonUtil.java index d48dbfca..099a34cf 100644 --- a/adapter/ohos/JsonUtil.java +++ b/adapter/ohos/JsonUtil.java @@ -123,39 +123,44 @@ public class JsonUtil { LOG.info("Uncompress::parseShellVersionInfoToAppInfo: is not a multi framewok bundle."); return false; } + try { + JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); + if (jsonObject == null) { + LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null"); + return false; + } - JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); - if (jsonObject == null) { - LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null"); - return false; - } - - JSONObject summaryJson = jsonObject.getJSONObject(SUMMARY); - if (summaryJson == null) { - LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null"); - return false; - } - JSONObject appJson = summaryJson.getJSONObject(APP); - if (appJson == null) { - LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: app is null"); - return false; - } - JSONObject versionJson = appJson.getJSONObject(VERSION); - if (versionJson == null) { - LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: version is null"); - return false; - } + JSONObject summaryJson = jsonObject.getJSONObject(SUMMARY); + if (summaryJson == null) { + LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null"); + return false; + } + JSONObject appJson = summaryJson.getJSONObject(APP); + if (appJson == null) { + LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: app is null"); + return false; + } + JSONObject versionJson = appJson.getJSONObject(VERSION); + if (versionJson == null) { + LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: version is null"); + return false; + } - if (!versionJson.containsKey(LEGACY_VERSION_CODE) || !versionJson.containsKey(LEGACY_VERSION_NAME)) { - LOG.error("Uncompress::parseShellVersionInfoToAppInfo no legacy version info."); + if (!versionJson.containsKey(LEGACY_VERSION_CODE) || !versionJson.containsKey(LEGACY_VERSION_NAME)) { + LOG.error("Uncompress::parseShellVersionInfoToAppInfo no legacy version info."); + return false; + } + appInfo.setShellVersionCode(versionJson.getString(LEGACY_VERSION_CODE)); + appInfo.setShellVersionName(versionJson.getString(LEGACY_VERSION_NAME)); + return true; + } catch(RuntimeException msg) { + LOG.error("parseShellVersionInfoToAppInfo exception"); return false; } - appInfo.setShellVersionCode(versionJson.getString(LEGACY_VERSION_CODE)); - appInfo.setShellVersionName(versionJson.getString(LEGACY_VERSION_NAME)); - return true; } - private static void parseDeviceTypeToHapInfo(String packInfoJsonStr, HapInfo hapInfo, String hapName) { + private static void parseDeviceTypeToHapInfo( + String packInfoJsonStr, HapInfo hapInfo, String hapName) throws BundleException { LOG.info("Uncompress::parseDeviceTypeToHapInfo: begin"); JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); if (jsonObject == null || !jsonObject.containsKey("packages")) { @@ -611,7 +616,7 @@ public class JsonUtil { * @param hapJson hapJson json * @param hapInfo hapInfo json object */ - private static void getDistro(JSONObject hapJson, HapInfo hapInfo) { + private static void getDistro(JSONObject hapJson, HapInfo hapInfo) throws BundleException { JSONObject distroObj = hapJson.getJSONObject("distro"); Distro distro = new Distro(); distro.moduleName = getJsonString(distroObj, "moduleName"); @@ -641,7 +646,7 @@ public class JsonUtil { * @param metaDataJson meta data json * @return the parseMetaData result */ - private static MetaData parseMetaData(JSONObject metaDataJson) { + private static MetaData parseMetaData(JSONObject metaDataJson) throws BundleException { MetaData metaData = new MetaData(); if (metaDataJson == null) { LOG.error("Uncompress::parseMetaData : metaDataJson is null"); @@ -1017,7 +1022,8 @@ public class JsonUtil { * @param profileJsons is the profile map * @return the pages result */ - static List parseModulePages(JSONObject moduleJson, HashMap profileJsons) { + static List parseModulePages( + JSONObject moduleJson, HashMap profileJsons) throws BundleException { List pages = new ArrayList<>(); String pageFile = getJsonString(moduleJson, "pages"); pageFile = pageFile.replace(PROFILE, ""); @@ -1292,7 +1298,8 @@ public class JsonUtil { * @param extensionAbilityInfos is the list of extensionAbility. * @return the List result */ - static List parseModuleCommonEvents(List extensionAbilityInfos) { + static List parseModuleCommonEvents( + List extensionAbilityInfos) throws BundleException { List allCommonEvent = new ArrayList<>(); if (extensionAbilityInfos.isEmpty()) { return allCommonEvent; @@ -1311,7 +1318,8 @@ public class JsonUtil { * @param moduleMetadataInfos is the list of ModuleMetadataInfo * @return the List result */ - static List parseCommoneventsInMetadata(List moduleMetadataInfos) { + static List parseCommoneventsInMetadata( + List moduleMetadataInfos) throws BundleException { List commonEvents = new ArrayList<>(); // find common events and parse in metadata for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { @@ -1330,7 +1338,8 @@ public class JsonUtil { * @param jsonObject is the json objects of commonevent. * @return the List result */ - static List parseModuleCommonEvents(JSONObject jsonObject) { + static List parseModuleCommonEvents( + JSONObject jsonObject) throws BundleException { List commonEvents = new ArrayList<>(); JSONArray commonEventObjs = jsonObject.getJSONArray("commonEvents"); for (int i = 0; i < commonEventObjs.size(); ++i) { @@ -1742,7 +1751,7 @@ public class JsonUtil { return reqPermissions; } - private static UsedScene parseModuleUsedScene(JSONObject usedSceneObj) { + private static UsedScene parseModuleUsedScene(JSONObject usedSceneObj) throws BundleException { UsedScene usedScene = new UsedScene(); if (usedSceneObj.containsKey(ABILITIES)) { usedScene.ability = JSON.parseArray(getJsonString(usedSceneObj, ABILITIES), String.class); @@ -1760,7 +1769,7 @@ public class JsonUtil { * @param key value key * @return the result */ - private static String getJsonString(JSONObject jsonObject, String key) { + private static String getJsonString(JSONObject jsonObject, String key) throws BundleException { String value = ""; if (jsonObject != null && jsonObject.containsKey(key)) { value = jsonObject.get(key).toString(); @@ -1776,7 +1785,7 @@ public class JsonUtil { * @param defaultValue the default value * @return the result */ - private static String getJsonString(JSONObject jsonObject, String key, String defaultValue) { + private static String getJsonString(JSONObject jsonObject, String key, String defaultValue) throws BundleException { String value = defaultValue; if (jsonObject != null && jsonObject.containsKey(key)) { value = jsonObject.get(key).toString(); diff --git a/adapter/ohos/ModuleJsonUtil.java b/adapter/ohos/ModuleJsonUtil.java index 62fb8130..0fe04710 100644 --- a/adapter/ohos/ModuleJsonUtil.java +++ b/adapter/ohos/ModuleJsonUtil.java @@ -407,7 +407,8 @@ class ModuleJsonUtil { * @param srcAppObj is the src pack.info app object * @return the result */ - public static boolean verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) { + public static boolean verifyAppInPackInfo(JSONObject finalAppObj, JSONObject srcAppObj) + throws BundleException { if (finalAppObj == null || srcAppObj == null) { LOG.error("ModuleJsonUtil:verifyAppInPackInfo input null json object!"); return false; @@ -485,7 +486,8 @@ class ModuleJsonUtil { * @param srcPackageObs is the src pack.info objects * @return the result */ - public static boolean verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs) { + public static boolean verifyPackageName(JSONArray finalPackageObs, JSONArray srcPackageObs) + throws BundleException { if (finalPackageObs == null || finalPackageObs.isEmpty() || srcPackageObs == null || srcPackageObs.isEmpty()) { LOG.error("ModuleJsonUtil:verifyPackageName pack.info has empty packages!"); return false; @@ -736,7 +738,7 @@ class ModuleJsonUtil { if (type != null && ENTRY.equals(type)) { deviceTypes = getDeviceTypesFromStageModule(moduleObj); } - + return deviceTypes; } @@ -828,7 +830,8 @@ class ModuleJsonUtil { * @param moduleMetadataInfos all metadata of module * @return DistroFilter is the result of parsed distroFilter */ - public static DistroFilter parseStageDistroFilter(List moduleMetadataInfos) { + public static DistroFilter parseStageDistroFilter( + List moduleMetadataInfos) throws BundleException { DistroFilter distroFilter = new DistroFilter(); for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { if (moduleMetadataInfo.resource.isEmpty()) { @@ -905,7 +908,7 @@ class ModuleJsonUtil { * @param jsonString Json string of config.json * @return the ModuleMetadataInfo result */ - public static DistroFilter parseFADistroFilter(String jsonString) { + public static DistroFilter parseFADistroFilter(String jsonString) throws BundleException { DistroFilter distroFilter = new DistroFilter(); JSONObject jsonObj = JSON.parseObject(jsonString); if (jsonObj.containsKey(MODULE)) { @@ -923,7 +926,7 @@ class ModuleJsonUtil { * * @param jsonString is the json String of module.json or config.json */ - public static List parseDeviceType(String jsonString) { + public static List parseDeviceType(String jsonString) throws BundleException { JSONObject jsonObj = JSON.parseObject(jsonString); List deviceType = new ArrayList<>(); if (jsonObj.containsKey(MODULE)) { @@ -945,7 +948,7 @@ class ModuleJsonUtil { * @param jsonString is the json String of module.json or config.json * @return ability names */ - public static List parseAbilityNames(String jsonString) { + public static List parseAbilityNames(String jsonString) throws BundleException { JSONObject jsonObj = JSON.parseObject(jsonString); List abilityNames = new ArrayList<>(); JSONObject moduleObj = jsonObj.getJSONObject(MODULE); @@ -994,7 +997,7 @@ class ModuleJsonUtil { * @param jsonString is the json String of module.json or config.json * @return is entry */ - public static String parseStageIsEntry(String jsonString) { + public static String parseStageIsEntry(String jsonString) throws BundleException { JSONObject jsonObj = JSON.parseObject(jsonString); if (jsonObj.containsKey(MODULE)) { JSONObject moduleObj = jsonObj.getJSONObject(MODULE); @@ -1011,7 +1014,7 @@ class ModuleJsonUtil { * @param jsonString is the json String of module.json or config.json * @return is entry */ - public static String parseFAIsEntry(String jsonString) { + public static String parseFAIsEntry(String jsonString) throws BundleException { JSONObject jsonObj = JSON.parseObject(jsonString); if (jsonObj.containsKey(MODULE)) { JSONObject moduleObj = jsonObj.getJSONObject(MODULE); -- Gitee