protostream icon indicating copy to clipboard operation
protostream copied to clipboard

Compilation error "@ProtoAdapter can only be applied to classes" on enum

Open poschi3 opened this issue 3 years ago • 5 comments

Hi, I wrote a ProtoAdapter for an Enum and get a compilation error:

[ERROR] /home/user/.../caching/src/main/java/com/example/ColorEnumAdapter.java:[7,8] @ProtoAdapter can only be applied to classes.

But documentation says

@ProtoAdapter is a marshalling adapter for a class or enum that you cannot annotate directly.

I also found an Unit Test in source code doing this. My example files are adopted from there:

ColorEnum.java

package com.example;

public enum ColorEnum {
    RED, GREEN, BLUE
}

ColorEnumAdapter.java

package com.example;

import org.infinispan.protostream.annotations.ProtoAdapter;
import org.infinispan.protostream.annotations.ProtoEnumValue;

@ProtoAdapter(ColorEnum.class)
public enum ColorEnumAdapter {

    @ProtoEnumValue(number = 0, name = "red")
    RED,

    @ProtoEnumValue(number = 1, name = "green")
    GREEN,

    @ProtoEnumValue(number = 2, name = "blue")
    BLUE
}

Can you help me to find out what I'm doing wrong?

poschi3 avatar Nov 09 '22 11:11 poschi3

I am facing the same issue

seravat avatar Feb 22 '23 16:02 seravat

Hi @poschi3 and @seravat. Are you using the ColorEnumAdapter to generate the schema with an @AutoProtoSchemaBuilder? E.g.:

@AutoProtoSchemaBuilder(includeClasses = { Developer.class, ColorAdapter.class },
   schemaFileName = "developer.proto", schemaFilePath = "proto")
public interface DeveloperSchema extends GeneratedSchema {
   DeveloperSchema INSTANCE = new DeveloperSchemaImpl();
}

Could you share the code that uses the adapter please? Thank you Fabio

fax4ever avatar Feb 23 '23 13:02 fax4ever

So, I found that in the class annotated with @AutoProtoSchemaBuilder you cannot use @AutoProtoSchemaBuilder(basePackage=xxx) with enums you have to use @AutoProtoSchemaBuilder(includeClasses = { xxx.class, yyy.class }) for it to work

seravat avatar Feb 23 '23 13:02 seravat

going to check the case thank you

fax4ever avatar Feb 23 '23 13:02 fax4ever

you definitely right, I opened the issue https://issues.redhat.com/browse/IPROTO-261

fax4ever avatar Feb 23 '23 14:02 fax4ever