Skip to content

Commit 6a9c390

Browse files
committed
Fix bean injection when bean name is different from type simple name
1 parent 6caac67 commit 6a9c390

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/java/AddFieldRecipe.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2024 Broadcom, Inc.
2+
* Copyright (c) 2017, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616

1717
import org.jspecify.annotations.NonNull;
18+
import org.jspecify.annotations.Nullable;
1819
import org.openrewrite.ExecutionContext;
1920
import org.openrewrite.Recipe;
2021
import org.openrewrite.Tree;
@@ -51,21 +52,28 @@ public String getDescription() {
5152

5253
@NonNull
5354
String classFqName;
55+
56+
String fieldName;
57+
58+
transient JavaType.FullyQualified fullyQualifiedType;
5459

5560
@JsonCreator
56-
public AddFieldRecipe(@NonNull @JsonProperty("fullyQualifiedClassName") String fullyQualifiedName, @NonNull @JsonProperty("classFqName") String classFqName) {
61+
public AddFieldRecipe(
62+
@NonNull @JsonProperty("fullyQualifiedClassName") String fullyQualifiedName,
63+
@NonNull @JsonProperty("classFqName") String classFqName,
64+
@Nullable @JsonProperty("fieldName") String fieldName) {
5765
this.fullyQualifiedName = fullyQualifiedName;
66+
fullyQualifiedType = JavaType.ShallowClass.build(fullyQualifiedName);
5867
this.classFqName = classFqName;
68+
this.fieldName = fieldName == null ? getFieldName(fullyQualifiedType) : fieldName;
5969
}
6070

6171
@Override
6272
public TreeVisitor<?, ExecutionContext> getVisitor() {
6373

6474
return new JavaIsoVisitor<ExecutionContext>() {
6575

66-
JavaType.FullyQualified fullyQualifiedType = JavaType.ShallowClass.build(fullyQualifiedName);
6776
String fieldType = getFieldType(fullyQualifiedType);
68-
String fieldName = getFieldName(fullyQualifiedType);
6977

7078
@Override
7179
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {

headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/java/ConstructorInjectionRecipe.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2024 Broadcom, Inc.
2+
* Copyright (c) 2017, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -289,9 +289,17 @@ public MethodDeclaration visitMethodDeclaration(MethodDeclaration method, Execut
289289
ShallowClass type = JavaType.ShallowClass.build(methodType);
290290
J.FieldAccess fa = new J.FieldAccess(Tree.randomId(), Space.EMPTY, Markers.EMPTY, new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, Collections.emptyList(), "this", md.getMethodType().getDeclaringType(), null), JLeftPadded.build(createFieldNameIdentifier()), type);
291291
Assignment assign = new J.Assignment(Tree.randomId(), Space.build("\n", Collections.emptyList()), Markers.EMPTY, fa, JLeftPadded.build(createFieldNameIdentifier()), type);
292+
assign = autoFormat(assign, p, getCursor());
292293
List<Statement> newStatements = new ArrayList<>(md.getBody().getStatements());
293-
newStatements.add(assign);
294-
md = md.withBody(autoFormat(md.getBody().withStatements(newStatements), p, getCursor()));
294+
boolean empty = newStatements.isEmpty();
295+
if (empty) {
296+
newStatements.add(assign);
297+
md = md.withBody(autoFormat(md.getBody().withStatements(newStatements), p, getCursor()));
298+
} else {
299+
// Prefix is off otherwise even after autoFormat
300+
newStatements.add(assign.withPrefix(newStatements.get(newStatements.size() - 1).getPrefix()));
301+
md = md.withBody(md.getBody().withStatements(newStatements));
302+
}
295303
}
296304
return md;
297305
}

headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/java/InjectBeanCompletionRecipe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2024 Broadcom, Inc.
2+
* Copyright (c) 2017, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -47,7 +47,7 @@ public InjectBeanCompletionRecipe(String fullyQualifiedName, String fieldName, S
4747
@Override
4848
public List<Recipe> getRecipeList() {
4949
List<Recipe> list = new ArrayList<>();
50-
list.add(new AddFieldRecipe(fullyQualifiedName, classFqName));
50+
list.add(new AddFieldRecipe(fullyQualifiedName, classFqName, fieldName));
5151
list.add(new ConstructorInjectionRecipe(fullyQualifiedName, fieldName, classFqName));
5252
return list;
5353
}

headless-services/commons/commons-rewrite/src/test/java/org/springframework/ide/vscode/commons/rewrite/java/AddFieldRecipeTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2024 Broadcom, Inc.
2+
* Copyright (c) 2017, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -28,7 +28,7 @@ public class AddFieldRecipeTest implements RewriteTest {
2828

2929
@Override
3030
public void defaults(RecipeSpec spec) {
31-
spec.recipe(new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar"))
31+
spec.recipe(new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar", null))
3232
.parser(JavaParser.fromJavaVersion()
3333
.logCompilationWarningsAndErrors(true));
3434
}
@@ -86,7 +86,7 @@ public void test() {
8686
public interface OwnerRepository{}
8787
""";
8888

89-
Recipe recipe = new AddFieldRecipe("com.example.demo.OwnerRepository", "com.example.demo.FooBar");
89+
Recipe recipe = new AddFieldRecipe("com.example.demo.OwnerRepository", "com.example.demo.FooBar", null);
9090
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
9191
}
9292

@@ -128,7 +128,7 @@ public void test() {
128128
public interface OwnerRepository{}
129129
""";
130130

131-
Recipe recipe = new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar");
131+
Recipe recipe = new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar", null);
132132
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
133133
}
134134

@@ -170,7 +170,7 @@ public void test() {
170170
public interface OwnerRepository{}
171171
""";
172172

173-
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBar");
173+
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBar", null);
174174
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
175175
}
176176

@@ -214,7 +214,7 @@ public void test() {
214214
public interface OwnerRepository{}
215215
""";
216216

217-
Recipe recipe = new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar$Inner");
217+
Recipe recipe = new AddFieldRecipe("com.example.test.OwnerRepository", "com.example.demo.FooBar$Inner", null);
218218
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
219219
}
220220

@@ -266,7 +266,7 @@ public void test1() {}
266266
public interface OwnerRepository{}
267267
""";
268268

269-
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBar");
269+
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBar", null);
270270
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
271271
}
272272

@@ -313,7 +313,7 @@ public void test() {
313313
@Component
314314
class FooBarNew {
315315
316-
private final Inner.OwnerRepository ownerRepository;
316+
private final Inner.OwnerRepository ownerRepo;
317317
318318
public void test1() {}
319319
@@ -325,7 +325,7 @@ public void test1() {}
325325
public interface OwnerRepository{}
326326
""";
327327

328-
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBarNew");
328+
Recipe recipe = new AddFieldRecipe("com.example.test.Inner.OwnerRepository", "com.example.demo.FooBarNew", "ownerRepo");
329329
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
330330
}
331331

headless-services/commons/commons-rewrite/src/test/java/org/springframework/ide/vscode/commons/rewrite/java/ConstructorInjectionRecipeTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017, 2024 Broadcom, Inc.
2+
* Copyright (c) 2017, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -21,6 +21,7 @@
2121
import org.openrewrite.SourceFile;
2222
import org.openrewrite.internal.InMemoryLargeSourceSet;
2323
import org.openrewrite.java.JavaParser;
24+
import org.openrewrite.java.JavaParser.Builder;
2425
import org.openrewrite.test.RecipeSpec;
2526
import org.openrewrite.test.RewriteTest;
2627

@@ -32,8 +33,12 @@ public void defaults(RecipeSpec spec) {
3233
.parser(JavaParser.fromJavaVersion().classpath("spring-beans"));
3334
}
3435

35-
public static void runRecipeAndAssert(Recipe recipe, String beforeSourceStr, String expectedSourceStr, String dependsOn) {
36-
JavaParser javaParser = JavaParser.fromJavaVersion().dependsOn(dependsOn).build();
36+
public static void runRecipeAndAssert(Recipe recipe, String beforeSourceStr, String expectedSourceStr, String... dependsOn) {
37+
Builder<? extends JavaParser, ?> builder = JavaParser.fromJavaVersion();
38+
if (dependsOn.length > 0) {
39+
builder.dependsOn(dependsOn);
40+
}
41+
JavaParser javaParser = builder.build();
3742

3843
List<SourceFile> list = javaParser.parse(beforeSourceStr).toList();
3944
SourceFile beforeSource = list.get(0);
@@ -486,4 +491,42 @@ public interface OwnerRepository{}
486491
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr, dependsOn);
487492
}
488493

494+
@Test
495+
void injectObjectFieldIntoNewConstructor() {
496+
497+
String beforeSourceStr = """
498+
package com.example.demo;
499+
500+
public class A {
501+
502+
private final String s;
503+
private final Object obj;
504+
505+
A(String s) {
506+
this.s = s;
507+
}
508+
509+
}
510+
""";
511+
512+
String expectedSourceStr = """
513+
package com.example.demo;
514+
515+
public class A {
516+
517+
private final String s;
518+
private final Object obj;
519+
520+
A(String s, Object obj) {
521+
this.s = s;
522+
this.obj = obj;
523+
}
524+
525+
}
526+
""";
527+
528+
Recipe recipe = new ConstructorInjectionRecipe("java.lang.Object", "obj", "com.example.demo.A");
529+
runRecipeAndAssert(recipe, beforeSourceStr, expectedSourceStr);
530+
}
531+
489532
}

0 commit comments

Comments
 (0)