protobuf生成的Maven Java编译器错误代码:不兼容的返回类型

时间:2016-09-21 15:19:47

标签: java eclipse maven compilation

使用Google Protocol Buffers版本3.0.0(发行版)时,maven编译以下生成的java-class会导致编译错误:

Java Class(生成的一个),这是一个简单的消息" PBReset"没有任何领域。

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: PBMessages.proto

package at.fhhagenberg.nemo.traffsim.remote.messages;

/**
 * Protobuf type {@code messages.PBReset}
 */
public  final class PBReset extends
    com.google.protobuf.GeneratedMessageV3 implements
    // @@protoc_insertion_point(message_implements:messages.PBReset)
    PBResetOrBuilder {
  // Use PBReset.newBuilder() to construct.
  private PBReset(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
    super(builder);
  }
  private PBReset() {
  }

  @java.lang.Override
  public final com.google.protobuf.UnknownFieldSet
  getUnknownFields() {
    return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
  }
  private PBReset(
      com.google.protobuf.CodedInputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    this();
    try {
      boolean done = false;
      while (!done) {
        int tag = input.readTag();
        switch (tag) {
          case 0:
            done = true;
            break;
          default: {
            if (!input.skipField(tag)) {
              done = true;
            }
            break;
          }
        }
      }
    } catch (com.google.protobuf.InvalidProtocolBufferException e) {
      throw e.setUnfinishedMessage(this);
    } catch (java.io.IOException e) {
      throw new com.google.protobuf.InvalidProtocolBufferException(
          e).setUnfinishedMessage(this);
    } finally {
      makeExtensionsImmutable();
    }
  }
  public static final com.google.protobuf.Descriptors.Descriptor
      getDescriptor() {
    return at.fhhagenberg.nemo.traffsim.remote.messages.PBMessages.internal_static_messages_PBReset_descriptor;
  }

  protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
      internalGetFieldAccessorTable() {
    return at.fhhagenberg.nemo.traffsim.remote.messages.PBMessages.internal_static_messages_PBReset_fieldAccessorTable
        .ensureFieldAccessorsInitialized(
            at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.class, at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.Builder.class);
  }

  private byte memoizedIsInitialized = -1;
  public final boolean isInitialized() {
    byte isInitialized = memoizedIsInitialized;
    if (isInitialized == 1) return true;
    if (isInitialized == 0) return false;

    memoizedIsInitialized = 1;
    return true;
  }

  public void writeTo(com.google.protobuf.CodedOutputStream output)
                      throws java.io.IOException {
  }

  public int getSerializedSize() {
    int size = memoizedSize;
    if (size != -1) return size;

    size = 0;
    memoizedSize = size;
    return size;
  }

  private static final long serialVersionUID = 0L;
  @java.lang.Override
  public boolean equals(final java.lang.Object obj) {
    if (obj == this) {
     return true;
    }
    if (!(obj instanceof at.fhhagenberg.nemo.traffsim.remote.messages.PBReset)) {
      return super.equals(obj);
    }
    at.fhhagenberg.nemo.traffsim.remote.messages.PBReset other = (at.fhhagenberg.nemo.traffsim.remote.messages.PBReset) obj;

    boolean result = true;
    return result;
  }

  @java.lang.Override
  public int hashCode() {
    if (memoizedHashCode != 0) {
      return memoizedHashCode;
    }
    int hash = 41;
    hash = (19 * hash) + getDescriptorForType().hashCode();
    hash = (29 * hash) + unknownFields.hashCode();
    memoizedHashCode = hash;
    return hash;
  }

  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      com.google.protobuf.ByteString data)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      com.google.protobuf.ByteString data,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data, extensionRegistry);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(byte[] data)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      byte[] data,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data, extensionRegistry);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(java.io.InputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      java.io.InputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input, extensionRegistry);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseDelimitedFrom(java.io.InputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseDelimitedWithIOException(PARSER, input);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseDelimitedFrom(
      java.io.InputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      com.google.protobuf.CodedInputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input);
  }
  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parseFrom(
      com.google.protobuf.CodedInputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input, extensionRegistry);
  }

  public Builder newBuilderForType() { return newBuilder(); }
  public static Builder newBuilder() {
    return DEFAULT_INSTANCE.toBuilder();
  }
  public static Builder newBuilder(at.fhhagenberg.nemo.traffsim.remote.messages.PBReset prototype) {
    return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
  }
  public Builder toBuilder() {
    return this == DEFAULT_INSTANCE
        ? new Builder() : new Builder().mergeFrom(this);
  }

  @java.lang.Override
  protected Builder newBuilderForType(
      com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
    Builder builder = new Builder(parent);
    return builder;
  }
  /**
   * Protobuf type {@code messages.PBReset}
   */
  public static final class Builder extends
      com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
      // @@protoc_insertion_point(builder_implements:messages.PBReset)
      at.fhhagenberg.nemo.traffsim.remote.messages.PBResetOrBuilder {
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return at.fhhagenberg.nemo.traffsim.remote.messages.PBMessages.internal_static_messages_PBReset_descriptor;
    }

    protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return at.fhhagenberg.nemo.traffsim.remote.messages.PBMessages.internal_static_messages_PBReset_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.class, at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.Builder.class);
    }

    // Construct using at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.newBuilder()
    private Builder() {
      maybeForceBuilderInitialization();
    }

    private Builder(
        com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
      super(parent);
      maybeForceBuilderInitialization();
    }
    private void maybeForceBuilderInitialization() {
      if (com.google.protobuf.GeneratedMessageV3
              .alwaysUseFieldBuilders) {
      }
    }
    public Builder clear() {
      super.clear();
      return this;
    }

    public com.google.protobuf.Descriptors.Descriptor
        getDescriptorForType() {
      return at.fhhagenberg.nemo.traffsim.remote.messages.PBMessages.internal_static_messages_PBReset_descriptor;
    }

    public at.fhhagenberg.nemo.traffsim.remote.messages.PBReset getDefaultInstanceForType() {
      return at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.getDefaultInstance();
    }

    public at.fhhagenberg.nemo.traffsim.remote.messages.PBReset build() {
      at.fhhagenberg.nemo.traffsim.remote.messages.PBReset result = buildPartial();
      if (!result.isInitialized()) {
        throw newUninitializedMessageException(result);
      }
      return result;
    }

    public at.fhhagenberg.nemo.traffsim.remote.messages.PBReset buildPartial() {
      at.fhhagenberg.nemo.traffsim.remote.messages.PBReset result = new at.fhhagenberg.nemo.traffsim.remote.messages.PBReset(this);
      onBuilt();
      return result;
    }

    public Builder clone() {
      return (Builder) super.clone();
    }
    public Builder setField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        Object value) {
      return (Builder) super.setField(field, value);
    }
    public Builder clearField(
        com.google.protobuf.Descriptors.FieldDescriptor field) {
      return (Builder) super.clearField(field);
    }
    public Builder clearOneof(
        com.google.protobuf.Descriptors.OneofDescriptor oneof) {
      return (Builder) super.clearOneof(oneof);
    }
    public Builder setRepeatedField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        int index, Object value) {
      return (Builder) super.setRepeatedField(field, index, value);
    }
    public Builder addRepeatedField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        Object value) {
      return (Builder) super.addRepeatedField(field, value);
    }
    public Builder mergeFrom(com.google.protobuf.Message other) {
      if (other instanceof at.fhhagenberg.nemo.traffsim.remote.messages.PBReset) {
        return mergeFrom((at.fhhagenberg.nemo.traffsim.remote.messages.PBReset)other);
      } else {
        super.mergeFrom(other);
        return this;
      }
    }

    public Builder mergeFrom(at.fhhagenberg.nemo.traffsim.remote.messages.PBReset other) {
      if (other == at.fhhagenberg.nemo.traffsim.remote.messages.PBReset.getDefaultInstance()) return this;
      onChanged();
      return this;
    }

    public final boolean isInitialized() {
      return true;
    }

    public Builder mergeFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      at.fhhagenberg.nemo.traffsim.remote.messages.PBReset parsedMessage = null;
      try {
        parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        parsedMessage = (at.fhhagenberg.nemo.traffsim.remote.messages.PBReset) e.getUnfinishedMessage();
        throw e.unwrapIOException();
      } finally {
        if (parsedMessage != null) {
          mergeFrom(parsedMessage);
        }
      }
      return this;
    }
    public final Builder setUnknownFields(
        final com.google.protobuf.UnknownFieldSet unknownFields) {
      return this;
    }

    public final Builder mergeUnknownFields(
        final com.google.protobuf.UnknownFieldSet unknownFields) {
      return this;
    }


    // @@protoc_insertion_point(builder_scope:messages.PBReset)
  }

  // @@protoc_insertion_point(class_scope:messages.PBReset)
  private static final at.fhhagenberg.nemo.traffsim.remote.messages.PBReset DEFAULT_INSTANCE;
  static {
    DEFAULT_INSTANCE = new at.fhhagenberg.nemo.traffsim.remote.messages.PBReset();
  }

  public static at.fhhagenberg.nemo.traffsim.remote.messages.PBReset getDefaultInstance() {
    return DEFAULT_INSTANCE;
  }

  private static final com.google.protobuf.Parser<PBReset>
      PARSER = new com.google.protobuf.AbstractParser<PBReset>() {
    public PBReset parsePartialFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
        return new PBReset(input, extensionRegistry);
    }
  };

  public static com.google.protobuf.Parser<PBReset> parser() {
    return PARSER;
  }

  @java.lang.Override
  public com.google.protobuf.Parser<PBReset> getParserForType() {
    return PARSER;
  }

  public at.fhhagenberg.nemo.traffsim.remote.messages.PBReset getDefaultInstanceForType() {
    return DEFAULT_INSTANCE;
  }

}

原始文件来源(摘录):

message PBReset {

}

这只是生成的类之一,所有类的maven错误都是相同的,如下所示:

[ERROR] C:\Users\Christian\Documents\FuE\Dissertation\Java\workspace\at.fhhagenberg.nemo.traffsim.remote\src\at\fhhagenberg\nemo\traffsim\remote\messages\PBReset.java:[9]
[ERROR] public  final class PBReset extends 
[ERROR] ^^^^^^^ 
[ERROR] The return types are incompatible for the inherited methods MessageOrBuilder.getAllFields(), GeneratedMessageV3.getAllFields()

这些类包含在正在运行的Maven-Tycho-Build配置中,在其类路径上有 protobuf-java-3.0.0.jar 。在Eclipse中编译代码时,它没有编译器错误。

此外,在类路径上从here使用 protobuf-java-3.0.0.jar 从命令行调用 javac 时,生成的源代码无需编译任何错误。

当然,Java源代码也是使用protoc 3.0.0版生成的

我还查看了MessageOrBuilder类和GeneratedMessageV3类以及声明的getAllFields()方法,它们都具有相同的返回类型,即public Map&lt; FieldDescriptor,Object&gt;。但是,唯一的区别是不同的表示法,因为在MessageOrBuilder接口中它被声明为Map&lt; Descriptors.FieldDescriptor,Object&gt;。

有人能告诉我为什么maven java编译器不能将这些返回类型识别为相同类型并显示错误吗?

有趣的细节:Eclipse Java Compiler中的这个(已修复的)错误似乎导致了同样的问题:https://bugs.eclipse.org/bugs/show_bug.cgi?id=496591

1 个答案:

答案 0 :(得分:0)

Eclipse Neon遇到了同样的问题。我将我的eclipse更新到版本4.6.1,问题解决了。

@Christian:你有趣的细节帮助了我。谢谢你的提示。