Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

目前发现使用proto文件的属性使用java关键字时,无法编译java class #180

Open
GiftNeededMore opened this issue Jun 24, 2022 · 1 comment

Comments

@GiftNeededMore
Copy link

当proto文件的属性使用java关键字会导致编译失败,例如执行以下单元测试类:
@test
public void test() throws IOException {
String protoCotent = "package mypackage.test; "
+ "option java_package = "com.baidu.bjf.remoting.protobuf.simplestring";"
+ "option java_outer_classname = "StringTypeClass"; " + "message StringMessage { "
+ " required string switch = 1 [default = "hello"]; }";
IDLProxyObject object = ProtobufIDLProxy.createSingle(protoCotent);

    // 动态设置字段值
    object.put("switch", "hello你好");
    // protobuf 序列化
    byte[] bb = object.encode();

    // protobuf 反序列化
    IDLProxyObject newObject = object.decode(bb);
    Assert.assertEquals("hello你好", newObject.get("switch"));
}

由于“StringMessage” class的属性是“switch”,导致执行失败,失败信息如下:

java.lang.IllegalStateException: Compilation failed. class: com.baidu.bjf.remoting.protobuf.simplestring.StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4, diagnostics: [StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:12: 错误: 需要<标识符>
public String switch="hello";
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:12: 错误: 非法的类型开始
public String switch="hello";
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:12: 错误: 需要<标识符>
public String switch="hello";
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:13: 错误: 需要<标识符>
public void setSwitch(String switch) {
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:13: 错误: 需要';'
public void setSwitch(String switch) {
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:14: 错误: 需要<标识符>
this.switch=switch;
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:14: 错误: 不是语句
this.switch=switch;
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:14: 错误: 非法的表达式开始
this.switch=switch;
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:14: 错误: 需要';'
this.switch=switch;
^, StringMessageJProtoBufProtoClass3ef734f4c1d84189a4e37c427df3d8a4.java:17: 错误: 需要<标识符>
return this.switch;
^]

在使用官方的protobuf包时,官方会将关键字重命名,例如“switch”会命名为“switch_”;

@jhunters
Copy link
Owner

感谢反馈, 后续跟进一下官方的处理办法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants