Groovy AST INSTRUCTION_SELECTION阶段与SEMANTIC_ANALYSIS

时间:2018-12-21 05:47:45

标签: groovy

这段代码在SEMANTIC_ANALYSIS阶段对我的访问者有效,但在INSTRUCTION_SELECTION阶段不起作用。看来我无法使用构造函数调用。如果我将构造函数改为Arrays.asList(),似乎可以编译。感谢您的帮助

val codeBlock = GeneralUtils.block()

// Declare the futures list
val arrayListNode = ClassHelper.make(ArrayList::class.java)
val variable = "myVariable"
val variableDeclaration = GeneralUtils.varX(variable, arrayListNode)
val asListExpression = GeneralUtils.ctorX(arrayListNode)
val variableStmt = GeneralUtils.declS(variableDeclaration, asListExpression)
codeBlock.addStatement(variableStmt)

//
// Set the codeBlock back to the closure
closure.code = codeBlock

在指令选择阶段,我得到的错误是

java.lang.ArrayIndexOutOfBoundsException: size==0
at org.codehaus.groovy.classgen.asm.OperandStack.getTopOperand(OperandStack.java:672)
at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateEqual(BinaryExpressionHelper.java:318)
at org.codehaus.groovy.classgen.asm.sc.StaticTypesBinaryExpressionMultiTypeDispatcher.evaluateEqual(StaticTypesBinaryExpressionMultiTypeDispatcher.java:142)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitDeclarationExpression(AsmClassGenerator.java:637)
at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89)

1 个答案:

答案 0 :(得分:0)

此代码似乎有效。

// Declare the futures list
val arrayListNode = ClassHelper.make(ArrayList::class.java)
val variable = "myVariable"
val variableDeclaration = GeneralUtils.varX(variable, arrayListNode)
val asListExpression = GeneralUtils.ctorX(arrayListNode)

// Look for the arraylist constructor in the node
val arrayNodeConstructorMethod = arrayListNode.getDeclaredConstructor(arrayOf())
asListExpression.setNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, arrayNodeConstructorMethod)

val variableStmt = GeneralUtils.declS(variableDeclaration, asListExpression)
codeBlock.addStatement(variableStmt)

//
// Set the codeBlock back to the closure
closure.code = codeBlock