protocolbuffers / protocolbuffers/protobuf

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

Open
#14,392 15 comments 2 reactions 2 assignees View on GitHub

@mkruskal-google is already working on this.

Since May 22, 2024.

help wanted java
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.