From 925356d659aed4b8550ce526f1772a706661c246 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Mon, 17 May 2021 10:17:44 -0700 Subject: [PATCH] fix(types): Reorder nested types for legacy protos w/ java_outer_class in one file [ggj] (#724) * fix(types): Use fully-qualified message type names * fix: cleanup * fix(types): Reorder nested types for legacy protos w/o java_multiple_files set --- .../api/generator/engine/ast/Reference.java | 3 +++ .../generator/gapic/protoparser/TypeParser.java | 17 +++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/Reference.java b/src/main/java/com/google/api/generator/engine/ast/Reference.java index e735f5be1c..fdc2c5c5ee 100644 --- a/src/main/java/com/google/api/generator/engine/ast/Reference.java +++ b/src/main/java/com/google/api/generator/engine/ast/Reference.java @@ -34,6 +34,9 @@ public interface Reference extends AstNode { boolean useFullName(); + // The nested types in left-to-right order, if any. + // Example: com.google.Foo.Bar.Car.ThisType will have the outer types listed in the order + // [Foo, Bar, Car]. @Nullable ImmutableList enclosingClassNames(); diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/TypeParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/TypeParser.java index aef1c1dff7..7a4542ee25 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/TypeParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/TypeParser.java @@ -147,7 +147,8 @@ static Reference parseMessageReference(@Nonnull Descriptor messageDescriptor) { // Handles nesting. while (containingType != null) { // Outermost type in the nested type hierarchy lies at index 0. - outerNestedTypeNames.add(0, containingType.getName()); + // If the Java outer proto class has already been set, apapend after it at index 1. + outerNestedTypeNames.add(hasJavaOuterClass ? 1 : 0, containingType.getName()); containingType = containingType.getContainingType(); } @@ -158,15 +159,17 @@ static Reference parseMessageReference(@Nonnull Descriptor messageDescriptor) { .setPakkage(pakkage) .setEnclosingClassNames(outerNestedTypeNames) .build(); + String protoPackage = messageDescriptor.getFile().getPackage(); String messageFullName = messageDescriptor.getFullName(); if (hasJavaOuterClass) { + int packageIndex = protoPackage.length(); messageFullName = String.format( "%s.%s.%s", - messageFullName.substring(0, messageFullName.lastIndexOf(DOT)), + messageFullName.substring(0, packageIndex), javaOuterClassname, - messageFullName.substring(messageFullName.lastIndexOf(DOT) + 1)); + messageFullName.substring(packageIndex + 1)); } Preconditions.checkState( messageReference.fullName().replace(pakkage, protoPackage).equals(messageFullName), @@ -207,7 +210,8 @@ static Reference parseEnumReference(@Nonnull EnumDescriptor enumDescriptor) { // Handles nesting. while (containingType != null) { // Outermost type in the nested type hierarchy lies at index 0. - outerNestedTypeNames.add(0, containingType.getName()); + // If the Java outer proto class has already been set, apapend after it at index 1. + outerNestedTypeNames.add(hasJavaOuterClass ? 1 : 0, containingType.getName()); containingType = containingType.getContainingType(); } @@ -221,12 +225,13 @@ static Reference parseEnumReference(@Nonnull EnumDescriptor enumDescriptor) { String protoPackage = enumDescriptor.getFile().getPackage(); String enumFullName = enumDescriptor.getFullName(); if (hasJavaOuterClass) { + int packageIndex = protoPackage.length(); enumFullName = String.format( "%s.%s.%s", - enumFullName.substring(0, enumFullName.lastIndexOf(DOT)), + enumFullName.substring(0, packageIndex), javaOuterClassname, - enumFullName.substring(enumFullName.lastIndexOf(DOT) + 1)); + enumFullName.substring(packageIndex + 1)); } Preconditions.checkState(