为什么gradle任务方法名称中的大小写?

时间:2016-02-10 23:37:14

标签: gradle groovy build.gradle

this question中,我感到困惑,因为我认为我们可以将参数传递给没有括号的方法。实际上,您可以将参数作为逗号分隔列表传递给类似的方法:

task ListOfStrings(type: ExampleTask) {
    //TheList 'one', 'two', 'three' // doesn't work
    theList 'one', 'two', 'three'
}
public class ExampleTask extends DefaultTask {
    //public void TheList(Object... theStrings) {
    //    theStrings.each { println it }
    //}
    public void theList(Object... theStrings) {
        theStrings.each { println it }
    }
}

上面的代码有效,因为方法名称是 camelCase 。当使用 TitleCase 的方法名称(上面已注释掉)时,gradle会抛出错误:

  build file '/tmp/build.gradle': 16: unexpected token: one @ line 16, column 13.
         TheList 'one', 'two', 'three'
                 ^

SO ,问题是,“为什么方法名称的情况很重要?” 总之,导致这种行为的原因是什么?这是一个惯例吗?如果是这样,它在哪里记录?

1 个答案:

答案 0 :(得分:6)

这只是Groovy编译器将任何大写符号视为类引用,而不是方法。这里有一个含糊不清的地方,您可以通过以下方式解决:

  • 使用括号Foo('one', 'two')

  • 限定方法名称this.Foo 'one', 'two'

一般来说,惯例是类是大写的而方法不是。因为Groovy是一种动态语言,所以编译器在很大程度上依赖于这些约定。

相关问题