我正在使用JCodemodel动态生成java类。下面是创建开关语句的代码,其默认情况是抛出异常。
JSwitch valueswitch;
AbstractJClass exception = ref(IllegalArgumentException.class);
valueswitch._default()
.body()
._throw(JExpr._new(exception));生成的类如下所示
public static Example switchCode(String code) {
switch (code) {
case "1":
{
return A;
}
default:
{
throw new IllegalArgumentException();
}
}
}现在,我想向抛出的异常添加一条消息,如
throw new IllegalArgumentException("Invalid code "+ code);如何在JCodemodel中实现这一点。任何帮助都将不胜感激。
发布于 2017-02-27 19:35:12
只需将语句添加到异常构造函数:
valueswitch._default()
.body()
._throw(JExpr._new(exception)
.arg(
JOp.plus(JExpr.lit("Invalid code "), codeParam)
));https://stackoverflow.com/questions/42444827
复制相似问题