Java 用protobuf自动生成的枚举替换字符串枚举

Java 用protobuf自动生成的枚举替换字符串枚举,java,string,enums,protocol-buffers,Java,String,Enums,Protocol Buffers,我的Enum原始java代码是: public enum CarModel { NOMODEL("NOMODEL"); X("X"), XS("XS"), XSI("XS-I"); //NOTE the special - character. Can't be declared XS-I XSI2("XS-I.2"); //NOTE the special . character. Can't be declared XS-I.2 private

我的Enum原始java代码是:

public enum CarModel {
    NOMODEL("NOMODEL");
    X("X"),
    XS("XS"),
    XSI("XS-I"); //NOTE the special - character. Can't be declared XS-I
    XSI2("XS-I.2"); //NOTE the special . character. Can't be declared XS-I.2
    private final String carModel;
    CarModel(String carModel) { 
        this.carModel = carModel;
    }

    public String getCarModel() { return carModel; }

    public static CarModel fromString(String text) {
        if (text != null) {
            for (CarModel c : CarModel.values()) {
                if (text.equals(c.carModel)) {
                    return c;
                }
            }
        }
        return NOMODEL; //default
    }
}
现在,如果我使用protobuf,我会得到.proto文件:

enum CarModel {
    NOMODEL = 0;
    X = 1;
    XS = 2;
    XSI = 3;
    XSI2 = 4;
}
从我的示例中,我知道我可以调用protoc生成的枚举并删除我自己的类(从而避免重复的值定义),但我仍然需要在某个地方(包装器类?包装器枚举类?)定义另一个
fromString()
方法,该方法将为每个枚举返回正确的字符串。我该怎么做

编辑: 我如何实现以下内容:

String carModel=carModel.XSI.toString()
这将返回“XS-I”

以及:


您可以使用Protobuf实现这一点

现在,在Java中,您可以执行以下操作:

String name =
    CarModel.XSI.getValueDescriptor()
    .getOptions().getExtension(MyProto.carName);
assert name.equals("XS-I");

(稍微向下滚动到“自定义选项”部分。)

我需要另一个方向:String model=CarModel.XSI.toString();但是能够告诉它XSI翻译成“XS-I”。还有下面的CarModel CarModel=CarModel.fromString(“XS-I”);我已经尝试了您的代码,但protoc似乎没有创建我需要调用的getProto()方法,以便将ValueDescriptor转换为扩展。有什么想法吗?还有一件事-当查看生成的java代码时,“XS-I”和“XS-I.2”字符串出现的唯一位置是最后一个字段,该字段定义为:“static{java.lang.String[]descriptorData={”基本上保留了我的.proto sourceOops,对不起,这个方法叫做
toProto
,而不是
getProto
。我会编辑我的答案。是的,注释都会进入描述符数据。我给出的代码实际上会从那里获取它们。toProto确实存在,但仍然没有getExtension…有什么想法吗?对不起,我忘了扩展挂断了选项proto,而不是描述符proto。所以我想您需要
getOptions()
而不是
toProto()
。再次更新示例。
CarModel carmodel =  Enum.valueOf(CarModel.class, "XS")
CarModel carmodel =  CarModel.valueOf("XS");
import "google/protobuf/descriptor.proto";

option java_outer_classname = "MyProto";
// By default, the "outer classname" is based on the proto file name.
// I'm declaring it explicitly here because I use it in the example
// code below.  Note that even if you use the java_multiple_files
// option, you will need to use the outer classname in order to
// reference the extension since it is not declared in a class.

extend google.protobuf.EnumValueOptions {
  optional string car_name = 50000;
  // Be sure to read the docs about choosing the number here.
}

enum CarModel {
  NOMODEL = 0 [(car_name) = "NOMODEL"];
  X = 1 [(car_name) = "X"];
  XS = 2 [(car_name) = "XS"];
  XSI = 3 [(car_name) = "XS-I"];
  XSI2 = 4 [(car_name) = "XS-I.2"];
}
String name =
    CarModel.XSI.getValueDescriptor()
    .getOptions().getExtension(MyProto.carName);
assert name.equals("XS-I");