20

I'm trying to serialize a structure with protobuf. after many hours trying to figure out what I'm doing wrong I decided to test the google's example and it didn't worked as well

I have the following protocol from google (https://developers.google.com/protocol-buffers/docs/javatutorial):

package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";

message Person {
    required string name = 1;
    required int32 id = 2;
    optional string email = 3;
    repeated PhoneNumber phone = 4;

    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }

    message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
    }
}

message AddressBook {
    repeated Person person = 1;
}

and I'm trying to serialize it with:

Person john = Person.newBuilder()   
    .setId(1234)
    .setName("John Doe")
    .setEmail("jdoe@example.com")
    .addPhone(
        Person.PhoneNumber.newBuilder()
            .setNumber("555-4321")
            .setType(Person.PhoneType.HOME))
    .build();

byte[] serialized = john.toByteArray();

and I get "java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses."

Thanks;

Rylander
  • 19,449
  • 25
  • 93
  • 144
Fynn
  • 811
  • 2
  • 10
  • 19

1 Answers1

29

As Marc said, A mismatch in Protocol Buffer versions will give you this exact message. In particular if

  • The .proto definition is converted to java using the 2.4.3 (or earlier) protoc.exe
  • You use the 2.5.0 protobuffers library

you will get this message in many methods (e.g. getParserForType, getUnknownFields) of class GeneratedMessage. There are no doubt other potential mismatch's that will cause this error


With protocol buffers 2.5.0 it is essential you regenerate all java classes with the 2.5.0 version of protoc (or on windows protoc.exe).


If you do the reverse - run code generated by protoc version 2.5 with the libraries for protocol buffers version 2.4. You will get the following message

java.lang.VerifyError: class xxx.xxx.xx.. 
overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet;
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
  • But I get this message even protobuf and java lib are 2.5.0 – Adelin Jan 23 '14 at 17:04
  • @Adio did you generate protobuf class with 2.5.0. This message occurs when the protobuf-java class is generated from the .proto definition with a prior version of java. I will update my answer to make this clearer – Bruce Martin Feb 25 '14 at 04:29
  • 8
    This is sort of amazing. I was planning to use protocol buffers to eliminate version dependencies and now I don't have anything working because protoc is in Ubuntu of version 2.4.1 and protobuf-java in the project has version 2.5.0. Plus message "This is supposed to be overridden by subclasses" is really lousy in describing real reason for a problem. – divanov Jul 04 '14 at 14:14
  • I'm running in same problem. Based on @BruceMartin mention I can confirm that my Ubuntu version running on proto V3.0.0 where In my source code I'm using lower [V2.6.1](https://github.com/julienr/protobuf-android). I'm really stuck here as I don't see any source code that build .so for V3.0.0. Any suggestion here, what can be done? – CoDe Sep 22 '15 at 06:08
  • I have not looked atV3.0.0, but do you have the Proto definition's (may have a .proto extension) ???. If you do, you should be able to run a protoc to regenerate interface files – Bruce Martin Sep 22 '15 at 08:05
  • @BruceMartin I got to build for **3.0.0-beta-1** and same I install on my system. So now generated lib and .h, .cc file refer same version "3.0.0-beta-1"..but still same facing issue. Any suggestion here! – CoDe Sep 22 '15 at 08:16
  • What Error messages are you getting and where are you getting them ???, you might be better off asking a question with all the details – Bruce Martin Sep 22 '15 at 08:37