jhunters / jprotobuf

A useful utility library for java programmer using google protobuf
Apache License 2.0
879 stars 281 forks source link

Date字段出错 #135

Closed maoxianfly closed 3 years ago

maoxianfly commented 4 years ago

Exception in thread "main" java.lang.IllegalStateException: Compilation failed. class: sample.entity.Foo$$JProtoBufClass, diagnostics: [Foo$$JProtoBufClass.java:135: 错误: 不兼容的类型: long无法转换为java.util.Date ret.setDate(input.readInt64()) ^, 注: 某些消息已经过简化; 请使用 -Xdiags:verbose 重新编译以获得完整输出] at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.doCompile(JdkCompiler.java:201) at com.baidu.bjf.remoting.protobuf.utils.compiler.AbstractCompiler.compile(AbstractCompiler.java:43) at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:408) at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:311) at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:276) at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:249)

jhunters commented 4 years ago

能贴一下使用方式吗? date格式 需要 FiledType指定Date类型 使用示例: https://github.com/jhunters/jprotobuf/tree/master/src/test/java/com/baidu/bjf/remoting/protobuf/v3/date

maoxianfly commented 4 years ago

@jhunters

`@ProtobufClass public class Foo {

private int id;

@Protobuf(fieldType = FieldType.DATE)
private Date date;

@Protobuf(fieldType = FieldType.OBJECT)
private Timestamp timeStamp;

private String hehe;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getHehe() {
    return hehe;
}

public void setHehe(String hehe) {
    this.hehe = hehe;
}

public Timestamp getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(Timestamp timeStamp) {
    this.timeStamp = timeStamp;
}

} `

public static void main(String[] args) throws IOException {

    Foo foo = new Foo();
    foo.setDate(new Date());

    byte[] encode = encode(foo, Foo.class);
    Foo decode = decode(encode, Foo.class);

    System.out.println(decode.getDate().getTime());

}

public static  <T> byte[] encode(T netMsgBase, Class<T> clazz) throws IOException {
    Codec<T> agentInfoCodec = ProtobufProxy.create(clazz, false);
    byte[] encode = agentInfoCodec.encode(netMsgBase);
    return encode;
}

public static  <T> T decode(byte[] bytes, Class<T> clazz) throws IOException {
    Codec<T> agentInfoCodec = ProtobufProxy.create(clazz, false);
    return agentInfoCodec.decode(bytes);
}
maoxianfly commented 4 years ago

jdk1.8.0_181 windows 10 jprotobuf 2.4.1

zhuyuanfox commented 4 years ago

我也遇到了,Date类型,看了测试用例,发现只能用public 像上面用private 修饰后,就会出现上面错误。

jhunters commented 4 years ago

fixed 2.4.2