• 用枚举类解决Switch-Case过长的sonar问题Reduce the number of switch cases from 38 to at most 30.


    【问题】Switch-Case过长的sonar问题Reduce the number of switch cases from 38 to at most 30.

     1                         switch (m.get("操作").toString()) {
     2                             case "启动应用":
     3                                 builder.append("        BOperate.open();
    ");
     4                                 break;
     5                             case "点击":
     6                                 builder.append("        BOperate.click("" + replaceQuotation(m.get("Xpath").toString().trim()) + "",true);
    ");
     7                                 break;
     8                             case "输入":
     9                                 builder.append("        BOperate.type("" + replaceQuotation(m.get("Xpath").toString().trim()) + ""," + paramStr(m.get("参数1")) + ");
    ");
    10                                 break;
    11                             case "获取文本":
    12                                 builder.append("        " + valStr(paramStr(m.get("参数1"))) + " = BOperate.getText("" + replaceQuotation(m.get("Xpath").toString().trim()) + "",true);
    ");
    13                                 break;

    【解决】

    引用处:

    1                         //sonar问题:Reduce the number of switch cases from 38 to at most 30.
    2                         // 解决方法:操作的关键字存到枚举类。遍历枚举类,对应的枚举类方法做操作处理
    3                         String operationStr = m.get("操作").toString();
    4                         String builderAppendStr = Operation.NULL.getInstance(operationStr).builderAppendStr();
    5                         builder.append(builderAppendStr);

    添加的枚举类(做后续扩展):其中的变量在原代码引用处保存到静态变量,在枚举类调用

     1 public enum Operation {
     2         NULL("") {
     3             @Override
     4             public String builderAppendStr() {
     5                 return null;
     6             }
     7         },
     8         OPEN("启动应用") {
     9             @Override
    10             public String builderAppendStr() {
    11                 return "        BOperate.open();
    ";
    12             }
    13         },
    14         CLICK("点击") {
    15             @Override
    16             public String builderAppendStr() {
    17                 return "        BOperate.click("" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + "",true);
    ";
    18             }
    19         },
    20         TYPE("输入") {
    21             @Override
    22             public String builderAppendStr() {
    23                 return "        BOperate.type("" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + ""," + paramStr(sTCConvertMap.get("参数1")) + ");
    ";
    24             }
    25         },
    26         GET_TEXT("获取文本") {
    27             @Override
    28             public String builderAppendStr() {
    29                 return "        " + valStr(paramStr(sTCConvertMap.get("参数1"))) + " = BOperate.getText("" + replaceQuotation(sTCConvertMap.get("Xpath").toString().trim()) + "",true);
    ";
    30             }
    31         }
    32 
    33         ;
    34 
    35 
    36         Operation(String operationName) {
    37             this.operationName = operationName;
    38         }
    39 
    40         private String operationName;
    41 
    42         public String getOperationName() {
    43             return operationName;
    44         }
    45 
    46         public Operation getInstance(String operationName) {
    47             for (Operation operation : Operation.values()) {
    48                 if (operationName.equals(operation.getOperationName())) {
    49                     return operation;
    50                 }
    51             }
    52             return Operation.NULL;
    53         }
    54 
    55         public abstract String builderAppendStr();
    56     }
  • 相关阅读:
    SignalR
    log4net
    nginx
    mongodb
    uni-app监听软键盘是否弹起,ios不支持
    js插件开发兼容commonJS、AMD、CDM的UMD写法
    uni-app 实现后端返回的图片文件流转base64
    input输入框限制只能输入数字和一个小数点
    Uni-app工程转vue-cli工程步骤
    H5 移动端开发注意事项
  • 原文地址:https://www.cnblogs.com/hsqzggg/p/10484147.html
Copyright © 2020-2023  润新知