【问题标题】:Apache Avro compile failing due to generated variables missing dollar signs由于生成的变量缺少美元符号,Apache Avro 编译失败
【发布时间】:2022-08-24 20:38:29
【问题描述】:

我正在设置一个 Apache Avro 模块,用于将消息序列化和反序列化到 Kafka。

我尝试了各种 Gradle 插件,它们都导致不同的错误。

org.betterplugin.avro 插件似乎最接近我,因为它生成 Java 和协议文件。但是,由于其中一个生成的变量缺少美元符号,其中一个生成的 Java 文件有错误。

build.gradle:

plugins {
    id \"org.betterplugin.avro\" version \"0.19.2-SNAPSHOT\"

    // Error: Unable to find resource \'/org/apache/avro/compiler/specific/templates/java/classic/enum.vm\'
    // id \"com.bakdata.avro\" version \"1.0.1\"

    // Error: Could not find method generateAvroProtocol()
    // id \"com.github.davidmc24.gradle.plugin.avro-base\" version \"1.3.0\"

    // Error: property \'outputDir\' is missing an input or output annotation.
    // id \"com.commercehub.gradle.plugin.avro\" version \"0.99.99\"
}

group = \'com.example\'
description = \'AVRO Library\'

dependencies {
    implementation \"org.apache.avro:avro:1.11.0\"
}

generateAvroProtocol {
    source(\"src/main/resources/avro\")
    outputDir = file(\"build/generated-main-avro-protocol\")
}

generateAvroJava {
    source(\"src/main/resources/avro\")
    outputDir = file(\"build/generated-main-avro-java\")
}

结果如下所示:


  // Used by DatumReader.  Applications should not call.
  @SuppressWarnings(value=\"unchecked\")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
    case 0: EXAMPLE_A = value != null ? value$.toString() : null; break;
    case 1: EXAMPLE_B = value != null ? value$.toString() : null; break;
    case 2: EXAMPLE_C = value != null ? value$.toString() : null; break;
    default: throw new org.apache.avro.AvroRuntimeException(\"Bad index\");
    }
  }

value$ 参数在没有美元符号的空检查中被引用,它无法使用cannot find symbol variable value 编译。

这是从avro-compiler\ 的record.vm 模板生成的:

  // Used by DatumReader.  Applications should not call.
  @SuppressWarnings(value=\"unchecked\")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
#set ($i = 0)
#foreach ($field in $schema.getFields())
    case $i: ${this.mangle($field.name(), $schema.isError())} = #if(${this.javaType($field.schema())} != \"java.lang.Object\" && ${this.javaType($field.schema())} != \"java.lang.String\")(${this.javaType($field.schema())})#{end}value$#if(${this.javaType($field.schema())} == \"java.lang.String\") != null ? value$.toString() : null#{end}; break;
#set ($i = $i + 1)
#end
    default: throw new IndexOutOfBoundsException(\"Invalid index: \" + field$);
    }
  }

该模板使用美元符号,所以我不确定这是如何被遗漏的。

  • 我有一个临时解决方法,允许通过执行一系列 Gradle 任务来编译构建,这些任务使用过滤器对文件进行字符串编辑。这是一个非常可怕的黑客攻击。 filter { String line -> line.replace(\" = value \", \" = value\\$ \").replace(\")value;\", \")value\\$;\") }

标签: java avro velocity gradle-plugin


【解决方案1】:

如果我使用插件名称为com.github.davidmc24.gradle.plugin.avro 而不是com.github.davidmc24.gradle.plugin.avro-base,我可以生成java 和协议文件

构建.gradle:

plugins {
    id 'java'
    id "com.github.davidmc24.gradle.plugin.avro" version "1.3.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.apache.avro:avro:1.11.0"
}

generateAvroJava {
    source("src/main/resources/avro")
    include("**/*.avsc")
    outputDir = file("build/generated-main-avro-java")
}

generateAvroProtocol {
    source("src/main/resources/avro")
    include("**/*.avdl")
    outputDir = file("build/generated-main-avro-java")
}

生成的avpr:

{
  "protocol" : "ExampleMessage",
  "namespace" : "example.avro",
  "types" : [ {
    "type" : "record",
    "name" : "ExampleMessage",
    "fields" : [ {
      "name" : "body",
      "type" : "string"
    } ]
  } ],
  "messages" : { }
}

生成的put方法:

  // Used by DatumReader.  Applications should not call.
  @SuppressWarnings(value="unchecked")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
    case 0: body = value$ != null ? value$.toString() : null; break;
    default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
    }
  }

$ 符号在生成的 java 文件中是正确的,这些文件正在成功编译到类文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多