From dfdb3eb3035a0d5d4454fa4fba87d7f6d7fa4798 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 7 Jan 2025 23:05:51 +0100 Subject: [PATCH] Retain whitespace before annotations in NoFinalizedLocalVariables Fixes #431 --- .../NoFinalizedLocalVariables.java | 2 +- .../NoFinalizedLocalVariablesTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariables.java b/src/main/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariables.java index c1d07b307..b19163e6f 100644 --- a/src/main/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariables.java +++ b/src/main/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariables.java @@ -78,7 +78,7 @@ private J.VariableDeclarations removeFinal(J.VariableDeclarations mv) { m -> m.getType() == J.Modifier.Type.Final ? null : m)); if (v.getModifiers().isEmpty() && v.getTypeExpression() != null) { v = v.withTypeExpression(v.getTypeExpression().withPrefix(v.getTypeExpression().getPrefix() - .withWhitespace(""))); + .withWhitespace(v.getLeadingAnnotations().isEmpty() ? "" : " "))); } return v; } diff --git a/src/test/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariablesTest.java b/src/test/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariablesTest.java index d1300c1ea..dfbe90109 100644 --- a/src/test/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariablesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/NoFinalizedLocalVariablesTest.java @@ -69,4 +69,25 @@ public String get() { ) ); } + + @Test + void retainPrefix() { + rewriteRun( + //language=java + java( + """ + class T { + public void test(@SuppressWarnings final String s) { + } + } + """, + """ + class T { + public void test(@SuppressWarnings String s) { + } + } + """ + ) + ); + } }