Skip to content

Commit

Permalink
Keep type-use annotation with type when wrapping
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 601800310
  • Loading branch information
cushon authored and google-java-format Team committed Jan 26, 2024
1 parent 412d761 commit 03efe44
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,14 @@ public Void visitMethod(MethodTree node, Void unused) {
annotations,
Direction.VERTICAL,
/* declarationAnnotationBreak= */ Optional.empty());
if (node.getTypeParameters().isEmpty() && node.getReturnType() != null) {
// If there are type parameters, we use a heuristic above to format annotations after the
// type parameter declarations as type-use annotations. If there are no type parameters,
// use the heuristics in visitModifiers for recognizing well known type-use annotations and
// formatting them as annotations on the return type.
returnTypeAnnotations = typeAnnotations;
typeAnnotations = ImmutableList.of();
}

Tree baseReturnType = null;
Deque<List<? extends AnnotationTree>> dims = null;
Expand Down Expand Up @@ -1436,10 +1444,6 @@ public Void visitMethod(MethodTree node, Void unused) {
}
token("<");
typeParametersRest(node.getTypeParameters(), plusFour);
if (!returnTypeAnnotations.isEmpty()) {
builder.breakToFill(" ");
visitAnnotations(returnTypeAnnotations, BreakOrNot.NO, BreakOrNot.NO);
}
first = false;
}

Expand All @@ -1456,8 +1460,14 @@ public Void visitMethod(MethodTree node, Void unused) {
builder.open(make(breakBeforeType, plusFour, ZERO));
openedNameAndTypeScope = true;
}
builder.open(ZERO);
if (!returnTypeAnnotations.isEmpty()) {
visitAnnotations(returnTypeAnnotations, BreakOrNot.NO, BreakOrNot.NO);
builder.breakOp(" ");
}
scan(baseReturnType, null);
maybeAddDims(dims);
builder.close();
}
if (!first) {
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", ZERO, Optional.of(breakBeforeName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class WrapTypeUse {
public static final <A extends @Nullable Object, B extends @Nullable Object> @Nullable
WrapTypeUse get() {
return null;
}

public @Nullable
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx getXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class WrapTypeUse {
public static final <A extends @Nullable Object, B extends @Nullable Object>
@Nullable WrapTypeUse get() {
return null;
}

public @Nullable Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
getXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx() {
return null;
}
}

0 comments on commit 03efe44

Please sign in to comment.