Errors in java generated HashCode() method

434 views
Skip to first unread message

Tristan Soriano

unread,
Apr 16, 2015, 4:48:28 PM4/16/15
to prot...@googlegroups.com

Hi,
I am new to protobufers and I have some issues with the generated java code.
I am successfully generating a java class from the following .proto file :

////////////////////////////////////////////////////////////////////////////////////////////////////////
option java_package = "org.myname.hbase.Coprocessor.autogenerated";
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);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

However the generated code has error in SumResquest and SumResponse generated class :
The auto generated method : HashCode( ) use an int "memoizedHashCode" as if it was an instance attribute but it is never declared.
In SumRequest :

    @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;
    }

//////////////////////////////////////////////////////////////////////////////////////////////////////
In SumResponse I have an extra error : The method hashLong(long) is undefined for the type Internal

    @java.lang.Override
    public int hashCode() {
      if (memoizedHashCode != 0) {
        return memoizedHashCode;
      }
      int hash = 41;
      hash = (19 * hash) + getDescriptorForType().hashCode();
      if (hasSum()) {
        hash = (37 * hash) + SUM_FIELD_NUMBER;
        hash = (53 * hash) + com.google.protobuf.Internal.hashLong(
            getSum());
      }
      hash = (29 * hash) + getUnknownFields().hashCode();
      memoizedHashCode = hash;
      return hash;
    }


As we are not supposed to edit the code. Do you have any idea what the solution could be ?
Should I add the missing attribute on my own, at what level and visibility ? How to solve my second issue ?
Is there any risk to add directly "getSum( ) ?

Thx

Feng Xiao

unread,
Apr 16, 2015, 4:49:41 PM4/16/15
to Tristan Soriano, Protocol Buffers
The error seems to suggest you are using a different version for protoc and protobuf Java runtime. Could you check whether they are of the same version?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Tristan Soriano

unread,
Apr 17, 2015, 4:40:47 AM4/17/15
to prot...@googlegroups.com, mtr.s...@gmail.com
I am using protobuf 2.6.1 with java 8 (Oracle downloaded jdk 1.8.0) on linux ubuntu. I cannot locate any JAVA_HOME call in protobuf folder using 'grep'. How does protoc locate JAVA ? How can I check which version is used by who ?

Feng Xiao

unread,
Apr 17, 2015, 1:49:18 PM4/17/15
to Tristan Soriano, Protocol Buffers
On Fri, Apr 17, 2015 at 1:40 AM, Tristan Soriano <mtr.s...@gmail.com> wrote:
I am using protobuf 2.6.1 with java 8 (Oracle downloaded jdk 1.8.0) on linux ubuntu. I cannot locate any JAVA_HOME call in protobuf folder using 'grep'. How does protoc locate JAVA ? How can I check which version is used by who ?
Can you check the protoc version with "protoc --version"? How did you compile your Java code? If you used Maven, what's protobuf version you specified in your pom.xml file? 

Tristan Soriano

unread,
Apr 20, 2015, 6:25:33 AM4/20/15
to prot...@googlegroups.com, mtr.s...@gmail.com
Hi,

protoc --version
libprotoc 2.6.1

I compile using :
protoc --java_out=./TestProto ./workspace/Coprocessor/src/sum.proto

Feng Xiao

unread,
Apr 20, 2015, 1:07:27 PM4/20/15
to Tristan Soriano, Protocol Buffers
So you are using the correct protoc version. Then the problem should be in the protobuf Java runtime. The missing memorizedHashCode is defined in AbstractMessageLite: https://github.com/google/protobuf/blob/v2.6.1/java/src/main/java/com/google/protobuf/AbstractMessageLite.java#L47 which is available from 2.6.1 protobuf runtime.
Reply all
Reply to author
Forward
0 new messages