public void verifyParam() { switch (this.actionType) { case…
- User
public void verifyParam() { switch (this.actionType) { case ModelDeleteType.DELETE_OBSERVE.getValue(): if (CollectionUtils.isBlank(observeIds)) { throw new BusinessException("请输入正确的observeIds:" + observeIds); } break; case ModelDeleteType.DELETE_ANALYSE.getValue(): if (CollectionUtils.isBlank(analyseIds)) { throw new BusinessException("请输入正确的analyseIds:" + analyseIds); } break; case ModelDeleteType.DELETE_STRATEGY.getValue(): if (CollectionUtils.isBlank(strategyIds)) { throw new BusinessException("请输入正确的strategyIds:" + strategyIds); } break; case ModelDeleteType.DELETE_BASE_INTENT.getValue(): if (CollectionUtils.isBlank(baseIntentIds)) { throw new BusinessException("请输入正确的baseIntentIds:" + baseIntentIds); } break; default: throw new BusinessException("请输入正确的actionType(DELETE_BASE_INTENT、DELETE_STRATEGY 、DELETE_ANALYSE、DELETE_OBSERVE): " + actionType); } }
public enum ModelDeleteType { DELETE_BASE_INTENT("DELETE_BASE_INTENT"), DELETE_STRATEGY("DELETE_STRATEGY"), DELETE_ANALYSE("DELETE_ANALYSE"), DELETE_OBSERVE("DELETE_OBSERVE");
private final String value; ModelDeleteType(String value) { this.value = value; } public String getValue() { return value; } public static List<String> deleteTypes = Arrays.stream(ModelDeleteType.values()) .map(ModelDeleteType::getValue) .collect(Collectors.toList());}
这个代码出现 Constant expression required
- Assistant
异常的原因是在switch语句中,case后面需要的是一个常量表达式,而不是一个变量或者方法调用的返回值。
解决方法是将this.actionType改为一个常量值,如下所示:
public void verifyParam() { String actionTypeValue = this.actionType; // 将this.actionType赋给一个新变量 switch (actionTypeValue) { case ModelDeleteType.DELETE_OBSERVE.getValue(): if (CollectionUtils.isBlank(observeIds)) { throw new BusinessException("请输入正确的observeIds:" + observeIds); } break; case ModelDeleteType.DELETE_ANALYSE.getValue(): if (CollectionUtils.isBlank(analyseIds)) { throw new BusinessException("请输入正确的analyseIds:" + analyseIds); } break; case ModelDeleteType.DELETE_STRATEGY.getValue(): if (CollectionUtils.isBlank(strategyIds)) { throw new BusinessException("请输入正确的strategyIds:" + strategyIds); } break; case ModelDeleteType.DELETE_BASE_INTENT.getValue(): if (CollectionUtils.isBlank(baseIntentIds)) { throw new BusinessException("请输入正确的baseIntentIds:" + baseIntentIds); } break; default: throw new BusinessException("请输入正确的actionType(DELETE_BASE_INTENT、DELETE_STRATEGY 、DELETE_ANALYSE、DELETE_OBSERVE): " + actionTypeValue); } } - User
还是出现 @Contract(pure = true)
public String getValue()com.vip.vman.enums.ModelDe 这个问题
- Assistant
I'm sorry for the oversight. The issue is that the
getValue()method in theModelDeleteTypeenum is not a constant expression, which is required in a switch-case statement.To resolve this issue, you can directly use the enum values in the switch-case statement without calling the
getValue()method. Here's how you can modify theverifyParam()method:public void verifyParam() { switch (this.actionType) { case DELETE_OBSERVE: if (CollectionUtils.isBlank(observeIds)) { throw new BusinessException("请输入正确的observeIds:" + observeIds); } break; case DELETE_ANALYSE: if (CollectionUtils.isBlank(analyseIds)) { throw new BusinessException("请输入正确的analyseIds:" + analyseIds); } break; case DELETE_STRATEGY: if (CollectionUtils.isBlank(strategyIds)) { throw new BusinessException("请输入正确的strategyIds:" + strategyIds); } break; case DELETE_BASE_INTENT: if (CollectionUtils.isBlank(baseIntentIds)) { throw new BusinessException("请输入正确的baseIntentIds:" + baseIntentIds); } break; default: throw new BusinessException("请输入正确的actionType(DELETE_BASE_INTENT、DELETE_STRATEGY、DELETE_ANALYSE、DELETE_OBSERVE): " + actionType); } } - User
不行 DELETE_BASE_INTENT 是枚举类的一个值而言
- Assistant
如果
DELETE_BASE_INTENT等是枚举类的一个值,您可以参考以下代码进行修改:public void verifyParam() { switch (this.actionType) { case DELETE_OBSERVE: if (CollectionUtils.isBlank(observeIds)) { throw new BusinessException("请输入正确的observeIds:" + observeIds); } break; case DELETE_ANALYSE: if (CollectionUtils.isBlank(analyseIds)) { throw new BusinessException("请输入正确的analyseIds:" + analyseIds); } break; case DELETE_STRATEGY: if (CollectionUtils.isBlank(strategyIds)) { throw new BusinessException("请输入正确的strategyIds:" + strategyIds); } break; case DELETE_BASE_INTENT: if (CollectionUtils.isBlank(baseIntentIds)) { throw new BusinessException("请输入正确的baseIntentIds:" + baseIntentIds); } break; default: throw new BusinessException("请输入正确的actionType(DELETE_BASE_INTENT、DELETE_STRATEGY、DELETE_ANALYSE、DELETE_OBSERVE): " + actionType); } }这样切换案例语句将直接使用
DELETE_OBSERVE等枚举值进行比较,不再需要调用getValue()方法。这应该解决了Constant expression required问题。