Skip to content

Commit 03efe44

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Keep type-use annotation with type when wrapping
PiperOrigin-RevId: 601800310
1 parent 412d761 commit 03efe44

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,14 @@ public Void visitMethod(MethodTree node, Void unused) {
14071407
annotations,
14081408
Direction.VERTICAL,
14091409
/* declarationAnnotationBreak= */ Optional.empty());
1410+
if (node.getTypeParameters().isEmpty() && node.getReturnType() != null) {
1411+
// If there are type parameters, we use a heuristic above to format annotations after the
1412+
// type parameter declarations as type-use annotations. If there are no type parameters,
1413+
// use the heuristics in visitModifiers for recognizing well known type-use annotations and
1414+
// formatting them as annotations on the return type.
1415+
returnTypeAnnotations = typeAnnotations;
1416+
typeAnnotations = ImmutableList.of();
1417+
}
14101418

14111419
Tree baseReturnType = null;
14121420
Deque<List<? extends AnnotationTree>> dims = null;
@@ -1436,10 +1444,6 @@ public Void visitMethod(MethodTree node, Void unused) {
14361444
}
14371445
token("<");
14381446
typeParametersRest(node.getTypeParameters(), plusFour);
1439-
if (!returnTypeAnnotations.isEmpty()) {
1440-
builder.breakToFill(" ");
1441-
visitAnnotations(returnTypeAnnotations, BreakOrNot.NO, BreakOrNot.NO);
1442-
}
14431447
first = false;
14441448
}
14451449

@@ -1456,8 +1460,14 @@ public Void visitMethod(MethodTree node, Void unused) {
14561460
builder.open(make(breakBeforeType, plusFour, ZERO));
14571461
openedNameAndTypeScope = true;
14581462
}
1463+
builder.open(ZERO);
1464+
if (!returnTypeAnnotations.isEmpty()) {
1465+
visitAnnotations(returnTypeAnnotations, BreakOrNot.NO, BreakOrNot.NO);
1466+
builder.breakOp(" ");
1467+
}
14591468
scan(baseReturnType, null);
14601469
maybeAddDims(dims);
1470+
builder.close();
14611471
}
14621472
if (!first) {
14631473
builder.breakOp(Doc.FillMode.INDEPENDENT, " ", ZERO, Optional.of(breakBeforeName));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class WrapTypeUse {
2+
public static final <A extends @Nullable Object, B extends @Nullable Object> @Nullable
3+
WrapTypeUse get() {
4+
return null;
5+
}
6+
7+
public @Nullable
8+
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx getXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx() {
9+
return null;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class WrapTypeUse {
2+
public static final <A extends @Nullable Object, B extends @Nullable Object>
3+
@Nullable WrapTypeUse get() {
4+
return null;
5+
}
6+
7+
public @Nullable Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8+
getXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx() {
9+
return null;
10+
}
11+
}

0 commit comments

Comments
 (0)