protocolbuffers/protobuf

Java generated code fails to compile if 'descriptor' field name is used

Open

#14.392 geöffnet am 13. Okt. 2023

Auf GitHub ansehen
 (15 Kommentare) (2 Reaktionen) (2 zugewiesene Personen)C++ (16.128 Forks)batch import
help wantedjava

Repository-Metriken

Stars
 (71.223 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 2T 11h) (185 gemergte PRs in 30 T)

Beschreibung

What version of protobuf and what language are you using? Version: v24.4 Language: Java

What operating system (Linux, Windows, ...) and version? MacOS 14.0

What runtime / compiler are you using (e.g., python version or gcc version) n/a

What did you do? Steps to reproduce the behavior:

  1. Create a protobuf message with a field named 'descriptor' (see below).
  2. Generate code with protoc com/example/example.proto --java_out=src/main/java.
  3. Compile code (I used a simple Maven project).
syntax = "proto3";

package com.example;

option java_multiple_files = true;

message FailsToCompile {
  string descriptor = 1;
}

What did you expect to see Compilation succeeded.

What did you see instead?

[ERROR] COMPILATION ERROR : 
[ERROR] src/main/java/com/example/FailsToCompile.java:[50,27] method getDescriptor() is already defined in class com.example.FailsToCompile
[ERROR] src/main/java/com/example/FailsToCompile.java:[412,29] method getDescriptor() is already defined in class com.example.FailsToCompile.Builder
[ERROR] src/main/java/com/example/FailsToCompile.java:[9,14] com.example.FailsToCompile is not abstract and does not override abstract method getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR] src/main/java/com/example/FailsToCompile.java:[30,7] getDescriptor() in com.example.FailsToCompile cannot implement getDescriptor() in com.example.FailsToCompileOrBuilder
  overriding method is static
[ERROR] src/main/java/com/example/FailsToCompile.java:[240,23] com.example.FailsToCompile.Builder is not abstract and does not override abstract method getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR] src/main/java/com/example/FailsToCompile.java:[245,9] getDescriptor() in com.example.FailsToCompile.Builder cannot implement getDescriptor() in com.example.FailsToCompileOrBuilder
  overriding method is static
[ERROR] src/main/java/com/example/FailsToCompile.java:[354,33] cannot find symbol
  symbol:   method isEmpty()
  location: class com.google.protobuf.Descriptors.Descriptor
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (java-compile) on project example: Compilation failure: Compilation failure: 
[ERROR] src/main/java/com/example/FailsToCompile.java:[50,27] method getDescriptor() is already defined in class com.example.FailsToCompile
[ERROR] src/main/java/com/example/FailsToCompile.java:[412,29] method getDescriptor() is already defined in class com.example.FailsToCompile.Builder
[ERROR] src/main/java/com/example/FailsToCompile.java:[9,14] com.example.FailsToCompile is not abstract and does not override abstract method getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR] src/main/java/com/example/FailsToCompile.java:[30,7] getDescriptor() in com.example.FailsToCompile cannot implement getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR]   overriding method is static
[ERROR] src/main/java/com/example/FailsToCompile.java:[240,23] com.example.FailsToCompile.Builder is not abstract and does not override abstract method getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR] src/main/java/com/example/FailsToCompile.java:[245,9] getDescriptor() in com.example.FailsToCompile.Builder cannot implement getDescriptor() in com.example.FailsToCompileOrBuilder
[ERROR]   overriding method is static
[ERROR] src/main/java/com/example/FailsToCompile.java:[354,33] cannot find symbol
[ERROR]   symbol:   method isEmpty()
[ERROR]   location: class com.google.protobuf.Descriptors.Descriptor
[ERROR] -> [Help 1]

Perhaps https://github.com/protocolbuffers/protobuf/blob/7789b3ac85248ad75631a1919071fa268e466210/src/google/protobuf/compiler/java/names.cc#L83-L99 needs to be updated to add Descriptor to the list of forbidden names?

Contributor Guide