如何在 Groovy AST 转换中创建 void return 语句?
How can I create a void return statement in a Groovy AST transformation?
我正在构建一个生成 void
方法的 AST 转换。我想检查传入的值是否已经等于另一个值,如果是,则提前退出。代码通常如下所示:
if(param.is existing) {
return
}
ReturnStatement
class 有一个 属性 returningNullOrVoid
检查 return 表达式是否是 null
,所以我尝试了显而易见的方法:
ifS(sameX(paramEx, existingEx), returnS(constX(null))
编译转换后的 class:
时出现异常
BUG! exception in phase 'instruction selection' in source unit 'Annotated.groovy' Unexpected return statement at -1:-1 return null
如何为提前退出插入 return 语句?
ReturnStatement
class 有一个名为 RETURN_NULL_OR_VOID
:
的常量
/**
* Only used for synthetic return statements emitted by the compiler.
* For comparisons use isReturningNullOrVoid() instead.
*/
public static final ReturnStatement RETURN_NULL_OR_VOID = new ReturnStatement(ConstantExpression.NULL);
Groovy 编译器检查此特定实例以生成 void return;
。当创建包含 return 语句的 AST 语句块时,您的语句 是 "a synthetic return statement emitted by the compiler",您应该使用该常量:
ifS(sameX(paramEx, existingEx), ReturnStatement.RETURN_NULL_OR_VOID)
我正在构建一个生成 void
方法的 AST 转换。我想检查传入的值是否已经等于另一个值,如果是,则提前退出。代码通常如下所示:
if(param.is existing) {
return
}
ReturnStatement
class 有一个 属性 returningNullOrVoid
检查 return 表达式是否是 null
,所以我尝试了显而易见的方法:
ifS(sameX(paramEx, existingEx), returnS(constX(null))
编译转换后的 class:
时出现异常BUG! exception in phase 'instruction selection' in source unit 'Annotated.groovy' Unexpected return statement at -1:-1 return null
如何为提前退出插入 return 语句?
ReturnStatement
class 有一个名为 RETURN_NULL_OR_VOID
:
/**
* Only used for synthetic return statements emitted by the compiler.
* For comparisons use isReturningNullOrVoid() instead.
*/
public static final ReturnStatement RETURN_NULL_OR_VOID = new ReturnStatement(ConstantExpression.NULL);
Groovy 编译器检查此特定实例以生成 void return;
。当创建包含 return 语句的 AST 语句块时,您的语句 是 "a synthetic return statement emitted by the compiler",您应该使用该常量:
ifS(sameX(paramEx, existingEx), ReturnStatement.RETURN_NULL_OR_VOID)