AutoProtoSchemaBuilder is not generating proto file

Viewed 550

Updated my code as per @Ryan Emerson suggestion but still i don't see any auto-generation of Impl file and proto file

@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 with over-ride methods

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) {

    }
}

Then ClassA that instantiates context-initializer

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);
          }
        }

        }

Maven

                <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>

I am still not seeing any auto-generated proto file. Can someone tell me what am i doing wrong ?

2 Answers

You also need to add the org.infinispan.protostream:protostream-processor artifact as a dependency in order for code to be generated:

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

Once that's present, a DummyInitializerImpl.java class will be generated that automatically registers the proto file and marshallers for the Book and Author classes. Remember that these classes must also have protostream annotations in order for the schema and marshallers to be generated. Please see the documentation for code examples.

There are two issues with your current code:

  1. You have provided a DummyInitializerImpl class, but that is what should be generated by @AutoProtoSchemaBuilder.
  2. In your DummyInitializerImpl you're trying to register the Infinispan UUIDMarshaller for the Book and Author types. This won't work as that marshaller is designed for the java UUID class.

I suspect that the two issues are due to a missunderstanding of how the code generation works. If you just required a SerializationContextInitializer for the Author and Book classes, it's not necessary to create the DummyInitializerImpl manually and you definitely don't need to utilise the UUIDMarshaller.

You are not saying which build system is used. maven maybe ? Did you add the protostream annotation processor as a dependency? Having a definite answer to these questions would help unriddle the issue of code generation. And after, we still need to find out who is supposed to initialize that dummyInitializer field.

Related