AutoProtoSchemaBuilder 不生成原型文件

AutoProtoSchemaBuilder is not generating proto file

根据@Ryan Emerson 的建议更新了我的代码,但我仍然没有看到任何自动生成的 Impl 文件和原型文件

@AutoProtoSchemaBuilder(
        includeClasses = { Book.class,  Author.class },
        schemaFileName = "library.proto",
        schemaFilePath = "proto/")
        interface DummyInitializer extends SerializationContextInitializer {


}

Author.class

public class Author {
    private final String name;
    private final String surname;

    @ProtoFactory
    public Author(String name, String surname) {
        this.name = (String)Objects.requireNonNull(name);
        this.surname = (String)Objects.requireNonNull(surname);
    }

    @ProtoField(
        number = 1
    )
    public String getName() {
        return this.name;
    }

    @ProtoField(
        number = 2
    )
    public String getSurname() {
        return this.surname;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            Author author = (Author)o;
            return this.name.equals(author.name) && this.surname.equals(author.surname);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.name, this.surname});
    }
}

Book.class

public class Book {
    private final String title;
    private final String description;
    private final int publicationYear;
    private final Set<Author> authors;

    @ProtoFactory
    public Book(String title, String description, int publicationYear, Set<Author> authors) {
        this.title = (String)Objects.requireNonNull(title);
        this.description = (String)Objects.requireNonNull(description);
        this.publicationYear = publicationYear;
        this.authors = (Set)Objects.requireNonNull(authors);
    }

    @ProtoField(
        number = 1
    )
    public String getTitle() {
        return this.title;
    }

    @ProtoField(
        number = 2
    )
    public String getDescription() {
        return this.description;
    }

    @ProtoField(
        number = 3,
        defaultValue = "-1"
    )
    public int getPublicationYear() {
        return this.publicationYear;
    }

    @ProtoField(
        number = 4
    )
    public Set<Author> getAuthors() {
        return this.authors;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o != null && this.getClass() == o.getClass()) {
            Book book = (Book)o;
            return this.publicationYear == book.publicationYear && this.title.equals(book.title) && this.description.equals(book.description) && this.authors.equals(book.authors);
        } else {
            return false;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{this.title, this.description, this.publicationYear, this.authors});
    }
}

context-initialzer class 具有覆盖方法

import org.infinispan.protostream.SerializationContext;

import java.io.UncheckedIOException;

public class contextInitializer implements DummyInitializer {
    @Override
    public String getProtoFileName() {
        return null;
    }

    @Override
    public String getProtoFile() throws UncheckedIOException {
        return null;
    }

    @Override
    public void registerSchema(SerializationContext serCtx) {

    }

    @Override
    public void registerMarshallers(SerializationContext serCtx) {

    }
}

然后实例化context-initializer的ClassA

public class classA {


  DummyInitializer myInterface= new contextInitializer();



    //Create a new cache instance


    public void startCache() {
        {

          try {
            manager = new DefaultCacheManager("src/main/resources/infinispan.xml");
            GlobalConfigurationBuilder builder= new GlobalConfigurationBuilder();
            builder.serialization().addContextInitializers(myInterface);
            System.out.println("------------------>"+ builder.serialization().addContextInitializers(myInterface));
            cache = manager.getCache();
            System.out.println(cache.getName()+" is initialized ");
          } catch (IOException e) {
            throw new IllegalArgumentException("Failed to initialize cache due to IO error",e);
          }
        }

        }

行家

                <dependency>
                <groupId>org.infinispan</groupId>
                <artifactId>infinispan-bom</artifactId>
                <version>${infinispan.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.infinispan.protostream</groupId>
                <artifactId>protostream-processor</artifactId>
                <scope>provided</scope>
            </dependency>

我仍然没有看到任何自动生成的原型文件。谁能告诉我我做错了什么?

你不是说使用了哪个构建系统。行家也许?您是否将 protostream 注释处理器添加为依赖项?对这些问题有一个明确的答案将有助于解决代码生成问题。之后,我们仍然需要找出谁应该初始化那个 dummyInitializer 字段。

您还需要添加 org.infinispan.protostream:protostream-processor 工件作为依赖项才能生成代码:

<dependency>
    <groupId>org.infinispan.protostream</groupId>
    <artifactId>protostream-processor</artifactId>
    <version>4.3.2.Final</version>
</dependency>

一旦存在,将生成一个 DummyInitializerImpl.java class,自动为 BookAuthor classes 注册原型文件和编组器.请记住,这些 classes 还必须具有 protostream 注释,以便生成模式和编组器。请参阅 documentation 以获取代码示例。

您当前的代码有两个问题:

  1. 您提供了 DummyInitializerImpl class,但 @AutoProtoSchemaBuilder 应该生成 DummyInitializerImpl
  2. 在您的 DummyInitializerImpl 中,您正在尝试为 BookAuthor 类型注册 Infinispan UUIDMarshaller。这不会起作用,因为编组器是为 java UUID class.
  3. 设计的

我怀疑这两个问题是由于对代码生成的工作方式的误解造成的。如果您只需要 AuthorBook class 的 SerializationContextInitializer,则没有必要手动创建 DummyInitializerImpl,而且您绝对不需要利用 UUIDMarshaller。