Skip to content

Option to keep a single marker annotation on the field's line #159

Open

Description

@abashev

Upstream: palantir#1782
Kind: idea | Priority: low

palantir-java-format#1782 (closed unmerged on 2026-10-17) lets a field with exactly one argument-less annotation keep it on the same line (@Mock private Foo foo;), reusing the predicate that locals and parameters already use. Fields with annotation arguments, several annotations, or lines that would overflow stay one-per-line. The author themselves flags it as a maintainer taste call, because the existing rule ("field annotations are always vertical") was a deliberate choice.

Why it matters to us: our JavaInputAstVisitor still hard-codes vertical annotations for fields (near line 3807), so this would be a deliberate style divergence from palantir, only worth it if our users ask for it (Mockito and injection annotations are the typical case). Upstream gave no reason beyond taste, and our recent lambda-parameter change (#103) shows we are open to inline annotations where they are clearly short.

Affected files: open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java, field-annotation test data.

Before (how it is formatted today; the field rule is hard-coded vertical in our code):

class Test {
    `@Mock`
    private Foo foo;

    `@Mock`
    private Bar bar;
}

After (with the change):

class Test {
    `@Mock` private Foo foo;
    `@Mock` private Bar bar;
}

Suggested action: watch; ignore unless users request it, and then consider it as an opt-in setting rather than a default change.


Filed from an upstream-watch ticket after manual review.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions