当前位置: 首页 > 知识库问答 >
问题:

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

董宜然
2023-03-14

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

// 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中编译代码时,它没有编译器错误。

此外,当从命令行使用protobuf-java-3.0.0.jar调用javac时,生成的源代码编译时不会出现任何错误。

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

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

共有1个答案

柳高卓
2023-03-14

我对日食霓虹也有同样的问题。我将eclipse更新到4.6.1版本,这个问题就解决了。

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

 类似资料:
  • 我试图从protobuf生成java类。 下面是我的协议- 我运行命令main/exec/protoc--java\u out=main/java main/proto/datamodel。协议 该协议是从链接https://github.com/protocolbuffers/protobuf/releases/tag/v3.7.1下载的,osx-x86_64.zip.我也尝试了3.8.0和3.

  • 问题内容: 我正在学习Java。我试图运行代码,但出现此错误:。它向我显示错误的代码部分。 为什么会这样呢? 问题答案: 这是因为在类中不能有两个具有相同名称但返回类型不同的方法。 子类不能使用与返回类型不同的超类中已经存在的方法相同的名称声明方法。 但是,子类可以声明具有与超类相同的签名的方法。我们称此为“覆盖”。 你需要这个 要么 一个好的做法是通过注释标记覆盖的方法:

  • 我创建了一个简单的. proto文件并执行了编译器(protoc-2.5.0rc1-win32.zip)。为规定的包生成了一个java文件。但是,生成的文件不会编译。. proto文件很简单,只有一条消息,其中包含一堆简单类型的可选字段(正确编号)。. java文件不编译,例如: 构造函数生成了消息。FieldAccessorTable(Descriptors.Descriptor,字符串[])未

  • 我应该如何更改getPhoneNumber内部的代码才能正确工作,有什么想法吗?这是我最后应该得到的+38(050)123-45-67。38是国家代码,这是电话号码501234567,但你必须在数字开头放零才能得到10个类似于这里的数字(>0<50) 完整的代码。

  • 在我的laravel项目上运行composer install时,我得到错误: 脚本php artisan clear已编译,用于处理返回的安装后cmd事件,错误代码为255 对可能出现的问题有什么建议吗?注意:composer安装所有供应商软件包。完整输出如下: [user@some_path]$composer update 加载具有包信息的composer存储库 更新依赖项(包括需要开发)