Skip to content

Commit

Permalink
fix(types): Reorder nested types for legacy protos w/ java_outer_clas…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
miraleung committed May 17, 2021
1 parent cc5475f commit 925356d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Expand Up @@ -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<String> enclosingClassNames();

Expand Down
Expand Up @@ -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();
}

Expand All @@ -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),
Expand Down Expand Up @@ -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();
}

Expand All @@ -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(
Expand Down

0 comments on commit 925356d

Please sign in to comment.