<dev>
1. 修改逻辑
| package com.zorkdata.desensitization.utils; | ||
| import com.alibaba.fastjson.JSON; | ||
| import com.zorkdata.desensitization.constans.GeneralConstants; | ||
| import com.zorkdata.desensitization.exception.ZorkException; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.flink.api.java.utils.ParameterTool; | ||
| import java.io.*; | ||
| import java.util.*; | ||
| /** | ||
| * @author: LiaoMingtao | ||
| * @date: 2020/8/7 | ||
| */ | ||
| @Slf4j | ||
| public class PropertiesUtil { | ||
| private static final int DEFAULT_PARAMS_MAP_LENGTH = 10; | ||
|
||
| private static final String REGULAR = "regular"; | ||
| public static void main(String[] args) { | ||
| List<String> propertiesContentList = PropertiesUtil.getPropertiesContentList("/regular"); | ||
| System.out.println(JSON.toJSONString(propertiesContentList)); | ||
|
||
| Map<String, String> propertiesMap = getPropertiesMap(propertiesContentList); | ||
| System.out.println(JSON.toJSONString(propertiesMap)); | ||
|
||
| } | ||
| /** | ||
| * 获取配置文件map | ||
| * | ||
| * @param args 入参参数 | ||
| * @return 配置文件map | ||
| */ | ||
| public static Map<String, String> getPropertiesMap(String[] args) throws ZorkException { | ||
| String configPath = null; | ||
| ParameterTool parameterTool = ParameterTool.fromArgs(args); | ||
| configPath = parameterTool.get(REGULAR); | ||
| log.info("read config path is {}", configPath); | ||
| List<String> propertiesContentList = PropertiesUtil.getPropertiesContentList(configPath); | ||
| Map<String, String> confMap = PropertiesUtil.getPropertiesMap(propertiesContentList); | ||
| if (confMap.isEmpty()) { | ||
| log.error("配置文件regular不存在,系统退出"); | ||
| throw new ZorkException("配置文件regular不存在,系统退出"); | ||
| } | ||
| return confMap; | ||
| } | ||
| /** | ||
| * 通过配置文件每行字符串获取文件所有内容 | ||
| * | ||
| * @param contentList 文件内容 | ||
| * @return 键值对map | ||
| */ | ||
| public static Map<String, String> getPropertiesMap(List<String> contentList) { | ||
| Map<String, String> map = new HashMap<>(GeneralConstants.MAXIMUM_CAPACITY); | ||
| if (null == contentList) { | ||
| return map; | ||
| } | ||
| for (String item : contentList) { | ||
| if (item.contains(GeneralConstants.EQUAL_SIGN)) { | ||
| int start = item.indexOf(GeneralConstants.EQUAL_SIGN); | ||
| String key = item.substring(0, start); | ||
| String value = GeneralConstants.EMPTY_STR; | ||
| if (item.length() > start) { | ||
| value = item.substring(start + 1); | ||
| } | ||
| map.put(key, value); | ||
| } | ||
| } | ||
| return map; | ||
| } | ||
| /** | ||
| * 按照行读取文件 | ||
| * | ||
| * @param propertiesFileName 文件路径 | ||
| * @return list,每行为一项 | ||
| */ | ||
| public static List<String> getPropertiesContentList(String propertiesFileName) { | ||
| InputStream inputStream = null; | ||
| List<String> resultList = new ArrayList<>(); | ||
| try { | ||
|
||
| inputStream = new FileInputStream(propertiesFileName); | ||
| //存放读的字节,就是读的结果 | ||
| BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | ||
| String str = null; | ||
| while ((str = bufferedReader.readLine()) != null) { | ||
| resultList.add(str); | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } finally { | ||
| if (inputStream != null) { | ||
| try { | ||
| inputStream.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } | ||
| } | ||
| } | ||
| return resultList; | ||
| } | ||
| /** | ||
| * 读取配置文件的所有内容 | ||
| * | ||
| * @param propertiesFileName 配置文件名称 | ||
| * @return 配置文件所有内容 | ||
| */ | ||
| public static String getPropertiesContent(String propertiesFileName) { | ||
| InputStream inputStream = null; | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
| try { | ||
| inputStream = PropertiesUtil.class.getResourceAsStream(propertiesFileName); | ||
| //存放读的字节,就是读的结果 | ||
| int result = -1; | ||
| while ((result = inputStream.read()) != -1) { | ||
| stringBuilder.append((char) result); | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } finally { | ||
| if (inputStream != null) { | ||
| try { | ||
| inputStream.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } | ||
| } | ||
| } | ||
| return stringBuilder.toString(); | ||
| } | ||
| /** | ||
| * 根据文件名获取该properties对象 | ||
| * | ||
| * @param propertiesFileName 配置文件名称 | ||
| * @return properties | ||
| */ | ||
| public static Properties getProperties(String propertiesFileName) { | ||
| Properties properties = new Properties(); | ||
| InputStream inputStream = null; | ||
| try { | ||
| inputStream = PropertiesUtil.class.getResourceAsStream(propertiesFileName); | ||
| properties.load(inputStream); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } finally { | ||
| if (inputStream != null) { | ||
| try { | ||
| inputStream.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } | ||
| } | ||
| } | ||
| return properties; | ||
| } | ||
| } | ||
-
SonarQube analysis reported 121 issues
-
🚫 22 critical -
⚠ 74 major -
🔽 24 minor -
ℹ 1 info
Watch the comments in this conversation to review them.
Top 30 extra issues
Note: The following issues were found on lines that were not modified in the commit. Because these issues can't be reported as line comments, they are summarized here:
-
🚫 Add a default case to this switch.📘 -
🚫 switch中每个case需要通过break/return等来终止📘 -
🚫 switch块缺少default语句📘 -
🚫 Define a constant instead of duplicating this literal " {\n" 11 times.📘 -
🚫 [Define a constant instead of duplicating this literal " "type": \n" 11 times.📘 -
🚫 Define a constant instead of duplicating this literal " "string",\n" 6 times.📘 -
🚫 Define a constant instead of duplicating this literal " "null"\n" 6 times.📘 -
🚫 [Define a constant instead of duplicating this literal " ]\n" 11 times.](https://git.zorkdata.com/liaomingtao/transaction_log_desensitization/blob/ec3e44b8900e424fb2958a192bbaffd9dfdf795f/src/main/java/com/zorkdata/desensitization/avro/AvroSchemaDef.java#L23)📘 -
🚫 Define a constant instead of duplicating this literal " },\n" 9 times.📘 -
🚫 Define a constant instead of duplicating this literal " "null",\n" 5 times.📘 -
🚫 Define a constant instead of duplicating this literal " {\n" 5 times.📘 -
🚫 Define a constant instead of duplicating this literal " "type": "map",\n" 5 times.📘 -
🚫 Define a constant instead of duplicating this literal " "values": "string"\n" 3 times.📘 -
🚫 Define a constant instead of duplicating this literal " }\n" 5 times.📘 -
🚫 Define a constant instead of duplicating this literal "序列化失败" 13 times.📘 -
🚫 Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.📘 -
🚫 Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.📘 -
🚫 Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.📘 -
🚫 Change this "try" to a try-with-resources. (sonar.java.source not set. Assuming 7 or greater.)📘 -
🚫 Refactor this code to not throw exceptions in finally blocks.📘 -
🚫 Refactor this code to not throw exceptions in finally blocks.📘 -
⚠ This block of commented-out lines of code should be removed.📘 -
⚠ 及时清理不再使用的代码段或配置信息。📘 -
⚠ Replace this use of System.out or System.err by a logger.📘 -
⚠ Replace this use of System.out or System.err by a logger.📘 -
⚠ String contains no format specifiers.📘 -
⚠ Replace this use of System.out or System.err by a logger.📘 -
⚠ Rename "jsonObject" which hides the field declared at line 39.📘 -
⚠ Remove this expression which always evaluates to "true"📘 -
⚠ Remove this expression which always evaluates to "true"📘
- ... 81 more
-