Adrian Seungjin Lee
Adrian Seungjin Lee

Reputation: 1666

compile java class of protocol buffer has compile time error

I'm using protocol buffer 2.6.1 with Java 1.7.0.71.

and compiled simple test protocol buffer file.

option java_package = "my.sample";
option java_outer_classname = "Sum";
option java_generic_services = true;
option java_generate_equals_and_hash = true;
option optimize_for = SPEED;
message SumRequest {
    required string family = 1;
    required string column = 2;
} 

message SumResponse {
   required int64 sum = 1 [default = 0];
}

service SumService {
  rpc getSum(SumRequest)
    returns (SumResponse);
}

But in code below, memoizedHashCode is declared nowhere, so it throws compile error.

@java.lang.Override
public int hashCode() {
  if (memoizedHashCode != 0) {
    return memoizedHashCode;
  }
  int hash = 41;
  hash = (19 * hash) + getDescriptorForType().hashCode();
  if (hasFamily()) {
    hash = (37 * hash) + FAMILY_FIELD_NUMBER;
    hash = (53 * hash) + getFamily().hashCode();
  }
  if (hasColumn()) {
    hash = (37 * hash) + COLUMN_FIELD_NUMBER;
    hash = (53 * hash) + getColumn().hashCode();
  }
  hash = (29 * hash) + getUnknownFields().hashCode();
  memoizedHashCode = hash;
  return hash;
}

I saw online that adding

private int memoizedHashCode = 0

solved the problem, but this is I think just workaround.

Why is this happening?

Upvotes: 0

Views: 342

Answers (1)

Kenton Varda
Kenton Varda

Reputation: 45326

memoizedHashCode is defined in the base class AbstractMessageLite, which is part of the protobuf library.

You need to make sure that the version of protoc that you are using to generate the code exactly matches the version of libprotobuf.jar that you are bringing into your program. If the versions do not match, you can see the error you describe, as well as other errors.

Upvotes: 2

Related Questions