groovy.lang.MissingMethodException: 使用 Groovy 默认 Builder 注释时没有方法异常的签名

groovy.lang.MissingMethodException: No signature of method exception when using Groovy default Builder annotation

我有以下枚举、特征和 class。

enum FileFormat {
    V2, V3
}

trait FileSet {
    int fileSetId
    List<DataFile> srcFiles = Collections.emptyList()
    boolean header = false
    boolean mixedPack = false
    FileFormat format

    List<String> getSrcFileNames() {
        srcFiles.collect { it -> it.getSrcFileName() }
    }

    int getFileCount() {
        srcFiles.size()
    }

    abstract boolean isValid()

    def addFile(HeaderFileType hdrType) {
        def f = DataFile()
    }
}

@Builder(builderMethodName = "builder", buildMethodName = "build", prefix = "with", excludes = "srcFileNames, valid, fileCount")
class VolumeFileSet implements FileSet {

    @Override
    boolean isValid() {
        //TODO implement based on VolumeFileSet validation rules
        return true
    }
}

当我尝试使用生成器设置 format 枚举时,出现错误

groovy.lang.MissingMethodException: No signature of method: static com.tccc.bia.testdrive.nsr.VolumeFileSet.witFormat() is applicable for argument types: (com.tccc.bia.testdrive.nsr.FileFormat) values: [V3]
Possible solutions: setFormat(com.tccc.bia.testdrive.nsr.FileFormat), getFormat()

这是测试

class TestSpec extends Specification {

    def setupSpec() {
        def volumeFileSet = VolumeFileSet
                .builder()
                .withHeader(true)
                .withMixedPack(true)
                .witFormat(FileFormat.V3) //ERROR here
                .build()
    }
}

您拼错了方法名称。

应该是withFormat(FileFormat.V3),不是witFormat

更正后,代码可以正常编译和运行。