-
Notifications
You must be signed in to change notification settings - Fork 461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
formatAnnotations()
contradicts Google Java style
#1366
Comments
This step was added in the PR below, might be some answers there. In particular, from the Google Java Style Guide
The current behavior of @mernst I'm happy to give you ownership of the |
The text of the Google Java Style Guide is a bit unclear about whether its type-use annotations rule (4.8.5.1) also applies to classes and methods (4.8.5.2 and 4.8.5.3). There is an example though As far as I can see, PR #1275 did not introduce any test case with type-use annotations on a class, so it's hard to guess @mernst's intent here. 😉 |
Thanks for bringing up this issue!
There is no such thing as a type annotation on a method declaration. In your example, With regard to class annotations, you are right that style guidelines are not explicit. The preferred style is: @DeclarationAnnotation1
@DeclarationAnnotation2
public @TypeAnnotation1 @TypeAnnotation2 class MyClass {
...
} Note that this is similar to the case for methods. The preferred style for methods is: @DeclarationAnnotation1
@DeclarationAnnotation2
public @TypeAnnotation1 @TypeAnnotation2 ReturnType myMethod {
...
} If you write the annotations in the correct places (before/after modifiers such as As noted at https://github.com/diffplug/spotless/blob/main/plugin-gradle/README.md#formatAnnotations, Spotless does not currently reorder annotations. It does not move annotations around modifiers. Spotless also does not reorder annotations of a particular variety (say, putting all declaration annotations in a canonical order, such as alphabetical order. As you point out, the Spotless test suite lacks test cases for the dispreferred style, and it entirely lacks test cases for annotations on class declarations. I'll add some at #1368.
The reason is that the qualified type is, in your example, Here are some code examples from Oracle showing the preferred style: https://docs.oracle.com/javase/tutorial/java/annotations/basics.html Here are citations from Google: https://google.github.io/styleguide/javaguide.html#s4.8.5-annotations Note that the GJF manual incorrectly uses the name "type-use annotation" when it should use the standard term "type annotation". Here is one from the Checker Framework: https://checkerframework.org/manual/#declaration-annotations-moved I can also be considered an authority. I designed Java's type annotations feature, and the javac implementation is by a group of people including me. |
Thank you very much for the explanation @mernst ! 😄 I was indeed thrown off by this sentence from the Google Java Style Guide:
Now, I get it.
Oops sorry! 🙇♂️ I don't know you. |
@edysli No problem! Thanks again for bringing up the issue. |
I'm using
formatAnnotations()
together withgoogleJavaFormat()
and introducing the former also brought surprising changes. Type annotations on class and method declarations are affected:However, Google's Java Style Guide, section "4.8.5 Annotations" says that
and the same applies to method and constructor annotations, without mentioning any exception for type annotations.
Moreover, the claim that "Type annotations should be on the same line as the type that they qualify." in Spotless Gradle plugin's README doesn't cite any source.
So which of these formatters is right?
My Spotless configuration for Java:
Spotless 6.11.0 with Gradle 7.5.1
The text was updated successfully, but these errors were encountered: