我需要在 jOOQ 生成的每个类的顶部和底部放置 // @formatter:on// @formatter:off 注释。我创建了一个 JavaGenerator 类,如下所示:

package ch.acme.backend.server.metamodel

import org.jooq.codegen.JavaGenerator
import org.jooq.codegen.JavaWriter
import org.jooq.meta.ArrayDefinition
import org.jooq.meta.EnumDefinition
import org.jooq.meta.PackageDefinition
import org.jooq.meta.RoutineDefinition
import org.jooq.meta.SchemaDefinition
import org.jooq.meta.TableDefinition
import org.jooq.meta.UDTDefinition

class NoFormatJavaGenerator : JavaGenerator() {

    override fun generateRecordClassJavadoc(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateArrayClassJavadoc(arrayDefinition: ArrayDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateDaoClassJavadoc(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateEnumClassJavadoc(enum: EnumDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateInterfaceClassJavadoc(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generatePackageClassJavadoc(pkg: PackageDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generatePojoClassJavadoc(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateRoutineClassJavadoc(routine: RoutineDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateSchemaClassJavadoc(schema: SchemaDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateTableClassJavadoc(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateUDTClassJavadoc(udt: UDTDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateUDTRecordClassJavadoc(udt: UDTDefinition, out: JavaWriter) {
        out.println("// @formatter:off")
    }

    override fun generateRecordClassFooter(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }

    override fun generateArrayClassFooter(arrayDefinition: ArrayDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateDaoClassFooter(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateEnumClassFooter(enum: EnumDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateInterfaceClassFooter(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generatePackageClassFooter(pkg: PackageDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generatePojoClassFooter(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateRoutineClassFooter(routine: RoutineDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateSchemaClassFooter(schema: SchemaDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }


    override fun generateTableClassFooter(table: TableDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }

    override fun generateUDTClassFooter(udt: UDTDefinition, out: JavaWriter) {
        out.println("// @formatter:on")

    }

    override fun generateUDTRecordClassFooter(udt: UDTDefinition, out: JavaWriter) {
        out.println("// @formatter:on")
    }

}

这适用于大多数文件,但“DefaultCatalog”、“Indexes”、“Keys”、“Public”和“Tables”不受此影响。我怎样才能添加这些?

最佳答案

扩展 JavaGenerator
您可以扩展 org.jooq.codegen.JavaGenerator.newJavaWriter(File) 方法并将返回的实现替换为您自己的扩展,该扩展将这些注释附加到所有 jOOQ 生成的文件中。

使用后处理器

但是,我认为使用 com.google.code.maven-replacer-plugin 之类的东西对所有生成的文件进行后处理会简单得多,因为您对文件的特定添加并不真正需要了解有关 Java 代码内部结构的任何信息。您可以在开头添加 // @formatter:off,然后在每个生成的 // @formatter:on 文件的最后添加 *.java。这也适用于不是由 jOOQ 生成的文件,而是由其他一些工具生成的。

关于kotlin - 为 jooq 生成的所有类插入注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53030816/

10-12 05:44