google / google/google-java-format

Handle type annotations

Aberta
#5 18 comentários 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
Java
Estrelas
6.2k
Forks
937
Merge médio
6min
PRs com merge (30d)
3

Descrição

We always want to format type annotations in-line:

```
@Nullable Object foo() {}
```

This is tricky in contexts where type annotations could be mixed together with declaration annotations, such as field, method, and variable declarations. There's no syntactic information to we can use to distinguish between `@NullableType Object foo() {}` and `@Deprecated Object foo() {}`, but we want to output the first annotation in-line, and the second vertically.

Currently, we output ambiguous annotations as declaration annotations. This is fine for Java 7, and has the advantage of never incorrectly formatting a declaration annotation as a type annotation.

Here are some possible heuristics we could use to support this case for Java 8:

1) If the annotation occurs before a regular modifier, it's probably a declaration annotation:

```
@Deprecated
static Object foo() {}
```

2) If the annotation occurs after regular modifiers, and it isn't on a void-returning method, it's probably a type annotation:

```
static @Nullable Object foo() {}
```

3) If the declaration isn't a void-returning method, and it has no regular modifiers, consider the existing formatting: type annotations are probably on the same line as the type, declaration annotations are probably vertical. (The risk here is that we'd preserve things like `@Override Object foo() {}`.)

4) If the declaration is named `@Override`, it's probably a declaration annotation. This would offset the disadvantage of (3), but it could be confusing if `@Override` behaves differently than other annotations.

5) An annotation with parameters is unlikely to be a type annotation, and should probably be formatted vertically.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.