-
Notifications
You must be signed in to change notification settings - Fork 90
Constructor binding report generation for spring 2.7 to 3.0 project #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabapp2
merged 3 commits into
spring-projects-experimental:main
from
sanagaraj-pivotal:166-constructor-binding
Nov 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...va/org/springframework/sbm/boot/upgrade_27_30/report/helper/ConstructorBindingHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2021 - 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.sbm.boot.upgrade_27_30.report.helper; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.spring.boot3.RemoveConstructorBindingAnnotation; | ||
import org.openrewrite.java.tree.J; | ||
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection; | ||
import org.springframework.sbm.engine.context.ProjectContext; | ||
import org.springframework.sbm.engine.recipe.OpenRewriteSourceFilesFinder; | ||
import org.springframework.sbm.project.resource.RewriteSourceFileHolder; | ||
import org.springframework.sbm.support.openrewrite.GenericOpenRewriteRecipe; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
public class ConstructorBindingHelper implements SpringBootUpgradeReportSection.Helper<List<String>> { | ||
|
||
private List<String> constructorBindingFiles; | ||
|
||
@Override | ||
public String getDescription() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(ProjectContext context) { | ||
|
||
GenericOpenRewriteRecipe<TreeVisitor<?, ExecutionContext>> recipe = | ||
new GenericOpenRewriteRecipe<>(() -> new UsesType("org.springframework.boot.context.properties.ConstructorBinding")); | ||
|
||
List<RewriteSourceFileHolder<J.CompilationUnit>> rewriteSourceFileHolders = | ||
context.getProjectJavaSources().find(recipe); | ||
|
||
constructorBindingFiles = rewriteSourceFileHolders | ||
.stream() | ||
.map(k -> k.getAbsolutePath().toString()) | ||
.collect(Collectors.toList()); | ||
|
||
return !rewriteSourceFileHolders.isEmpty(); | ||
} | ||
|
||
@Override | ||
public Map<String, List<String>> getData(ProjectContext context) { | ||
|
||
return Map.of("files", constructorBindingFiles); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...rg/springframework/sbm/boot/upgrade_27_30/report/helper/ConstructorBindingHelperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2021 - 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.sbm.boot.upgrade_27_30.report.helper; | ||
|
||
import org.intellij.lang.annotations.Language; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportTestSupport; | ||
import org.springframework.sbm.engine.context.ProjectContext; | ||
import org.springframework.sbm.project.resource.TestProjectContext; | ||
|
||
public class ConstructorBindingHelperTest { | ||
|
||
@Test | ||
public void reportMigrationSuggestionsWhenConstructorBindingUsageIsFound() { | ||
@Language("java") | ||
String javaClassWithConstructorBinding = """ | ||
package com.example; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.ConstructorBinding; | ||
|
||
@ConfigurationProperties(prefix = "mail") | ||
@ConstructorBinding | ||
public class ConfigProperties { | ||
private String hostName; | ||
private int port; | ||
private String from; | ||
|
||
public ConfigProperties(String hostName, int port, String from) { | ||
this.hostName = hostName; | ||
this.port = port; | ||
this.from = from; | ||
} | ||
|
||
public String getHostName() { | ||
return hostName; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public String getFrom() { | ||
return from; | ||
} | ||
} | ||
"""; | ||
|
||
ProjectContext context = TestProjectContext.buildProjectContext() | ||
.addJavaSource("src/main/java", javaClassWithConstructorBinding) | ||
.withBuildFileHavingDependencies("org.springframework.boot:spring-boot:2.7.1") | ||
.build(); | ||
|
||
SpringBootUpgradeReportTestSupport | ||
.generatedSection("Constructor Binding") | ||
.fromProjectContext(context) | ||
.shouldRenderAs( | ||
""" | ||
fabapp2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=== Constructor Binding | ||
Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/166[#166], Contributors: https://github.com/sanagaraj-pivotal[@sanagaraj-pivotal^, role="ext-link"] | ||
|
||
==== What Changed | ||
When using constructor bound @ConfigurationProperties the @ConstructorBinding annotation | ||
is no longer required if the class has a single parameterized constructor. | ||
If you have more than one constructor, you’ll still need to use `@ConstructorBinding` | ||
to tell Spring Boot which one to use. | ||
|
||
For most users, this updated logic will allow for simpler `@ConfigurationProperties` | ||
classes. If, however, you have a `@ConfigurationProperties` and you want to inject | ||
beans into the constructor rather than binding it, you’ll now need to add an | ||
`@Autowired` annotation. | ||
|
||
==== Why is the application affected | ||
We found usage of `@ConstructorBinding` in following files: | ||
|
||
* <PATH>/src/main/java/com/example/ConfigProperties.java | ||
|
||
==== Remediation | ||
Remove `@ConstructorBinding` if it matches the criteria, please refer issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/166[#166] | ||
for more information | ||
|
||
"""); | ||
} | ||
|
||
@Test | ||
public void shouldNotReportConstructorBindingSuggestionWhenNothingIsFound() { | ||
@Language("java") | ||
String javaClassWithConstructorBinding = """ | ||
package com.example; | ||
|
||
public class A { } | ||
"""; | ||
|
||
ProjectContext context = TestProjectContext.buildProjectContext() | ||
.addJavaSource("src/main/java/com/example/A.java", javaClassWithConstructorBinding) | ||
.withBuildFileHavingDependencies("org.springframework.boot:spring-boot:2.7.1") | ||
.build(); | ||
|
||
SpringBootUpgradeReportTestSupport | ||
.generatedSection("Constructor Binding") | ||
.fromProjectContext(context) | ||
.shouldNotRender(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.