首页 > 解决方案 > Groovy 'with' 方法的意外行为 - 变量分配静默失败

问题描述

我有以下代码:

import groovy.transform.ToString
@ToString(includeNames = true)
class Simple {
  String creditPoints
}
Simple simple = new Simple()
simple.with {
    creditPoints : "288"
}
println simple

显然,我在这里犯了一个错误creditPoints : "288"。它应该是creditPoints = "288"

我预计 Groovy 在运行时会失败,说我犯了一个错误,我应该使用creditPoints = "288"它,但显然它没有。

既然它没有失败,那么 Groovy 对我创建的闭包做了什么?

标签: groovyclosures

解决方案


从 Groovy 编译器的角度来看,闭包代码中没有错误。编译器将creditPoints : "288"其视为标记语句,这是 Groovy 编程语言中的合法构造。正如文档所说,标签语句不会向生成的字节码添加任何内容,但它可以用于例如 AST 转换(Spock 框架大量使用它)。

如果您将代码更准确地格式化为标签语句用例,它将变得更加清晰易懂,例如

class Simple {
    String creditPoints

    static void main(String[] args) {

        Simple simple = new Simple()
        simple.with {
            creditPoints:
            "288"
        }
        println simple
    }
}

(注意:我将您的脚本放在main方法主体中,以便在下一节中向您展示其字节码表示。)

现在我们知道编译器是如何看待这个构造的,让我们来看看最终的字节码是什么样子的。为此,我们将反编译该.class文件(我为此使用 IntelliJ IDEA - 您只需.class在 IDEA 中打开文件,它就会为您反编译):

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import groovy.lang.Closure;
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import groovy.transform.ToString;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.GeneratedClosure;
import org.codehaus.groovy.runtime.InvokerHelper;

@ToString
public class Simple implements GroovyObject {
    private String creditPoints;

    public Simple() {
        MetaClass var1 = this.$getStaticMetaClass();
        this.metaClass = var1;
    }

    public static void main(String... args) {
        Simple simple = new Simple();

        class _main_closure1 extends Closure implements GeneratedClosure {
            public _main_closure1(Object _outerInstance, Object _thisObject) {
                super(_outerInstance, _thisObject);
            }

            public Object doCall(Object it) {
                return "288";
            }

            public Object call(Object args) {
                return this.doCall(args);
            }

            public Object call() {
                return this.doCall((Object)null);
            }

            public Object doCall() {
                return this.doCall((Object)null);
            }
        }

        DefaultGroovyMethods.with(simple, new _main_closure1(Simple.class, Simple.class));
        DefaultGroovyMethods.println(Simple.class, simple);
        Object var10000 = null;
    }

    public String toString() {
        StringBuilder _result = new StringBuilder();
        Boolean $toStringFirst = Boolean.TRUE;
        _result.append("Simple(");
        if ($toStringFirst == null ? false : $toStringFirst) {
            Boolean var3 = Boolean.FALSE;
        } else {
            _result.append(", ");
        }

        if (this.getCreditPoints() == this) {
            _result.append("(this)");
        } else {
            _result.append(InvokerHelper.toString(this.getCreditPoints()));
        }

        _result.append(")");
        return _result.toString();
    }

    public String getCreditPoints() {
        return this.creditPoints;
    }

    public void setCreditPoints(String var1) {
        this.creditPoints = var1;
    }
}

如您所见,与with方法一起使用的闭包表示为内部_main_closure1类。这个类扩展Closure了类,它实现了GeneratedClosure接口。闭包的主体封装在public Object doCall(Object it)方法中。该方法只返回"288"字符串,这是预期的——闭包的最后一条语句默认成为返回语句。生成的字节码中没有标签语句,这也是预期的,因为标签在CANONICALIZATIONGroovy 编译器阶段被剥离。


推荐阅读