@@ -275,11 +275,14 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
275275 if (initMethodDeclarationTemplate != null ) {
276276 cd = cd .withBody (cd .getBody ().withTemplate (initMethodDeclarationTemplate ,
277277 cd .getBody ().getCoordinates ().lastStatement ()));
278+ J .Block finalBody = cd .getBody ();
278279 cd = cd .withBody (cd .getBody ().withStatements (ListUtils .map (cd .getBody ().getStatements (), stmt -> {
279280 if (stmt instanceof J .MethodDeclaration ) {
280281 J .MethodDeclaration md = (J .MethodDeclaration ) stmt ;
281282 if (md .getName ().getSimpleName ().equals (initMethodName )) {
282- return md .withParameters (parameterizedTestMethodParameters );
283+ J .Block body = md .getBody (); // Preserve body formatting
284+ return autoFormat (md .withParameters (parameterizedTestMethodParameters ).withBody (null ),
285+ executionContext , new Cursor (getCursor (), finalBody )).withBody (body );
283286 }
284287 }
285288 return stmt ;
@@ -289,18 +292,19 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
289292 // if a constructor was converted to an init method then remove final modifiers from any associated field variables.
290293 final Set <String > fieldNames = getCursor ().pollMessage ("INIT_VARS" );
291294 if (fieldNames != null && !fieldNames .isEmpty ()) {
295+ J .Block finalBody = cd .getBody ();
292296 cd = cd .withBody (cd .getBody ().withStatements (ListUtils .map (cd .getBody ().getStatements (), statement -> {
293297 if (statement instanceof J .VariableDeclarations ) {
294298 J .VariableDeclarations varDecls = (J .VariableDeclarations ) statement ;
295299 if (varDecls .getVariables ().stream ().anyMatch (it -> fieldNames .contains (it .getSimpleName ()))
296300 && (varDecls .hasModifier (J .Modifier .Type .Final ))) {
297- statement = varDecls .withModifiers (ListUtils .map (varDecls .getModifiers (), mod -> mod .getType () == J .Modifier .Type .Final ? null : mod ));
301+ varDecls = varDecls .withModifiers (ListUtils .map (varDecls .getModifiers (), mod -> mod .getType () == J .Modifier .Type .Final ? null : mod ));
302+ statement = maybeAutoFormat (statement , varDecls , executionContext , new Cursor (getCursor (), finalBody ));
298303 }
299304 }
300305 return statement ;
301306 })));
302307 }
303- cd = maybeAutoFormat (classDecl , cd , executionContext );
304308 return cd ;
305309 }
306310
@@ -356,14 +360,14 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
356360 assert m .getBody () != null ;
357361 JavaCoordinates newStatementCoordinates = !m .getBody ().getStatements ().isEmpty () ? m .getBody ().getStatements ().get (0 ).getCoordinates ().before () : m .getBody ().getCoordinates ().lastStatement ();
358362 m = m .withTemplate (initMethodStatementTemplate , newStatementCoordinates , initStatementParamString );
359- m = maybeAutoFormat (m , m .withParameters (parameterizedTestMethodParameters ), executionContext , getCursor ().dropParentUntil ( J . class :: isInstance ));
363+ m = maybeAutoFormat (m , m .withParameters (parameterizedTestMethodParameters ), m . getName (), executionContext , getCursor ().getParentTreeCursor ( ));
360364 }
361365
362366 // Change constructor to test init method
363367 if (initMethodDeclarationTemplate == null && m .isConstructor ()) {
364368 m = m .withName (m .getName ().withSimpleName (initMethodName ));
365369 m = maybeAutoFormat (m , m .withReturnTypeExpression (new J .Primitive (randomId (), Space .EMPTY , Markers .EMPTY , JavaType .Primitive .Void )),
366- executionContext , getCursor ().dropParentUntil ( J . class :: isInstance ));
370+ m . getName (), executionContext , getCursor ().getParentTreeCursor ( ));
367371
368372 // converting a constructor to a void init method may require removing final modifiers from field vars.
369373 if (m .getBody () != null ) {
0 commit comments