Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Annotation location when mixed with modifiers

未关闭
#1,152 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
java
领域
tooling

调研方向

首先,将提供的混合注解示例与所链接的 Google Java Style Guide 的第 4.8.5 节和第 4.8.7 节进行比较。没有指定源文件或测试,因此首先定位 formatter 逻辑以及针对修饰符和注解的现有覆盖;当预期的放置位置确定,并且示例得到一致格式化时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Java
星标
6.2k
派生
936
平均合并
6 分钟
30 天内合并 PR
3

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/google-java-format 的其他 Issue

查看 google/google-java-format 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。