Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Annotation location when mixed with modifiers

Abierto
#1,152 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
java
Área
tooling

Línea de trabajo

Comienza comparando los ejemplos proporcionados con anotaciones mixtas con las secciones 4.8.5 y 4.8.7 de la Google Java Style Guide enlazada. No se indica ningún archivo fuente ni ninguna prueba, así que primero localiza la lógica del formateador y la cobertura existente para modificadores y anotaciones; se considerará terminado cuando se haya decidido la colocación prevista y los ejemplos se formateen de forma coherente.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

non-whitespace

When annotations are mixed with modifiers, formatter formats the code in following way:

$ cat TestingModifiers.java
/** some javadoc. */
public abstract class TestingModifiers {
  abstract @MyAnnotation2 public void fooMet1();

  final strictfp synchronized protected @MyAnnotation2 void fooMethod7() {}

  synchronized final strictfp @MyAnnotation2 public void fooMethod5() {}

  @MyAnnotation2 public static @MyAnnotation4 strictfp void someMethod5() {}

  strictfp protected final @MyAnnotation2 static synchronized void fooMethod1() {}

  native synchronized protected static @MyAnnotation2 void fooMethod3();
}

$ java -jar google-java-format-1.23.0-all-deps.jar TestingModifiers.java > TestingModifiersUpdated.java 

$ cat TestingModifiersUpdated.java 
/** some javadoc. */
public abstract class TestingModifiers {
  abstract @MyAnnotation2 public void fooMet1();

  protected final synchronized strictfp @MyAnnotation2 void fooMethod7() {}

  final synchronized strictfp @MyAnnotation2 public void fooMethod5() {}

  @MyAnnotation2
  public static @MyAnnotation4 strictfp void someMethod5() {}

  protected final strictfp @MyAnnotation2 static synchronized void fooMethod1() {}

  protected static synchronized native @MyAnnotation2 void fooMethod3();
}

$ diff -u TestingModifiers.java TestingModifiersUpdated.java 
--- TestingModifiers.java       2024-08-28 11:00:02.129632600 +0530
+++ TestingModifiersUpdated.java        2024-08-28 11:01:40.748988100 +0530
@@ -2,13 +2,14 @@
 public abstract class TestingModifiers {
   abstract @MyAnnotation2 public void fooMet1();

-  final strictfp synchronized protected @MyAnnotation2 void fooMethod7() {}
+  protected final synchronized strictfp @MyAnnotation2 void fooMethod7() {}

-  synchronized final strictfp @MyAnnotation2 public void fooMethod5() {}
+  final synchronized strictfp @MyAnnotation2 public void fooMethod5() {}

-  @MyAnnotation2 public static @MyAnnotation4 strictfp void someMethod5() {}
+  @MyAnnotation2
+  public static @MyAnnotation4 strictfp void someMethod5() {}

-  strictfp protected final @MyAnnotation2 static synchronized void fooMethod1() {}
+  protected final strictfp @MyAnnotation2 static synchronized void fooMethod1() {}

-  native synchronized protected static @MyAnnotation2 void fooMethod3();
+  protected static synchronized native @MyAnnotation2 void fooMethod3();
}

From 4.8.7 Modifiers

Class and member modifiers, when present, appear in the order recommended by the Java Language Specification:

public protected private abstract default static final transient volatile synchronized native strictfp

There's not explicitly mentioned anything about position of annotations when mixed with modifiers or annotations should not be mixed with modifiers or something similar to that in above rule.

Also, in 4.8.5 Annotations, there's not explicitly mentioned that how annotations should be positioned when used with modifiers. But all sub-sections mostly refers to:

Annotations applying to a class appear immediately after the documentation block, and each annotation is listed on a line of its own (that is, one annotation per line).

Attention to:

immediately after the documentation block

This kind of implies that annotations should be placed before any modifiers. But formatter does not places annotations before modifiers as shown in above example. So if annotations should not be mixed with modifiers and placed at the beginning or in a separate line then formatter should be updated and format the code according to this.

Lenguaje dominante
Java
Estrellas
6.2k
Forks
936
Merge medio
6 min
PR fusionados (30 d)
3

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de google/google-java-format

Todos los issues de google/google-java-format

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.