Skip to content

166 constructor binding report #513

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ public Builder addJavaSource(Path sourcePathDir, String sourceCode) {
return this;
}

/**
* Adds a Java source file to the {@code ProjectContext}.
*
* @param sourcePath e.g. {@code src/main/java} or {@code src/test/java} the package and classname are extracted from the given source code
* @param sourceCode the source code of the java file. It will be used to calculate package and classname.
*/
public Builder addJavaSource(String sourcePath, String sourceCode) {
return addJavaSource(Path.of(sourcePath), sourceCode);
}
Expand Down
10 changes: 9 additions & 1 deletion components/sbm-recipes-boot-upgrade/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
</dependency>
<dependency>
<groupId>io.spring.asciidoctor.backends</groupId>
<artifactId>spring-asciidoctor-backends</artifactId>
<version>${spring-asciidoctor-backends.version}</version>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -102,4 +110,4 @@
</snapshots>
</repository>
</repositories>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@
recipe: boot-2.7-3.0-upgrade-report
contributors:
- "Sandeep Nagaraj[@sanagaraj-pivotal]"

- title: Constructor Binding
helper: org.springframework.sbm.boot.upgrade_27_30.report.helper.ConstructorBindingHelper
change: |-
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.
affected: |-
We found usage of `@ConstructorBinding` in following files:

<#list files as file>
* ${file}
</#list>
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
gitHubIssue: 166
recipe: boot-2.7-3.0-upgrade-report
contributors:
- "Sandeep Nagaraj[@sanagaraj-pivotal]"

footer: |-
We want to say thank you to all Contributors:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.tree.J;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.maven.UpgradeDependencyVersion;
import org.openrewrite.xml.tree.Xml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
*/
package org.springframework.sbm.boot.upgrade_27_30.report;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.nio.file.Path;
import java.util.Map;

/**
* @author Fabian Krüger
Expand All @@ -44,26 +41,25 @@ void changesToDataPropertiesSection_renders() {
.fromProjectContext(context)
.shouldRenderAs(
"""
=== Changes to Data Properties
Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/441[#441], Contributors: https://github.com/fabapp2[@fabapp2^, role="ext-link"]

==== What Changed
The data prefix has been reserved for Spring Data and any properties under the `data` prefix imply that Spring
Data is required on the classpath.

==== Why is the application affected
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.

* file://<PATH>/src/main/resources/application.properties[`src/main/resources/application.properties`]
** `spring.data.foo`
* file://<PATH>/src/main/resources/application-another.properties[`src/main/resources/application-another.properties`]
** `spring.data.here`

==== Remediation
Either add `spring-data` dependency, rename the property or remove it in case it's not required anymore.

""",
Map.of("PATH", Path.of(".").toAbsolutePath().resolve(TestProjectContext.getDefaultProjectRoot()).toString()));
=== Changes to Data Properties
Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/441[#441], Contributors: https://github.com/fabapp2[@fabapp2^, role="ext-link"]

==== What Changed
The data prefix has been reserved for Spring Data and any properties under the `data` prefix imply that Spring
Data is required on the classpath.

==== Why is the application affected
The scan found properties with `spring.data` prefix but no dependency matching `org.springframework.data:.*`.

* file://<PATH>/src/main/resources/application.properties[`src/main/resources/application.properties`]
** `spring.data.foo`
* file://<PATH>/src/main/resources/application-another.properties[`src/main/resources/application-another.properties`]
** `spring.data.here`

==== Remediation
Either add `spring-data` dependency, rename the property or remove it in case it's not required anymore.

""");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.sbm.engine.recipe.Action;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.engine.recipe.Recipes;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.sbm.test.RecipeTestSupport;
import org.springframework.sbm.testhelper.common.utils.TestDiff;
import org.springframework.test.util.ReflectionTestUtils;
Expand Down Expand Up @@ -76,7 +77,7 @@ public Assertion(BuilderData builderData) {


public void shouldRenderAs(String expectedOutput) {
shouldRenderAs(expectedOutput, Map.of());
shouldRenderAs(expectedOutput, defaultMap());
}

public void shouldRenderAs(String expectedOutput, Map<String, String> templateVariables) {
Expand All @@ -90,7 +91,7 @@ public void shouldNotRender() {
}

public void shouldStartWith(String expectedOutput) {
shouldStartWith(expectedOutput, Map.of());
shouldStartWith(expectedOutput, defaultMap());
}

public void shouldStartWith(String expectedOutput, Map<String, String> templateVariables) {
Expand All @@ -99,6 +100,16 @@ public void shouldStartWith(String expectedOutput, Map<String, String> templateV
verify(assertion);
}


private Map<String, String> defaultMap() {
String path = Path
.of(".")
.toAbsolutePath()
.resolve(TestProjectContext.getDefaultProjectRoot()).toString();

return Map.of("PATH", path);
}

private void verifyDoesNotRender() {
if(SectionBuilderData.class.isInstance(builderData)) {
SectionBuilderData sectionBuilderData = SectionBuilderData.class.cast(builderData);
Expand Down Expand Up @@ -148,7 +159,6 @@ public void writeReport(String s, Path outputDir, String filename) {
action.apply(reportBuilderData.getContext());
});
} else if(SectionBuilderData.class.isInstance(builderData)) {
SectionBuilderData sectionBuilderData = SectionBuilderData.class.cast(builderData);
withRecipes(recipes -> {
Recipe recipe = recipes.getRecipeByName("boot-2.7-3.0-upgrade-report2").get();
SpringBootUpgradeReportAction action = (SpringBootUpgradeReportAction) recipe.getActions().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.nio.file.Path;
import java.util.Map;

public class BannerSupportHelperTest {

Expand Down Expand Up @@ -55,7 +53,6 @@ public void rendersBannerSupportInformation() {
==== Remediation
remove image banners and replace it with text-banner with banner.txt file

""",
Map.of("PATH", Path.of(".").toAbsolutePath().resolve(TestProjectContext.getDefaultProjectRoot()).toString()));
""");
}
}
Loading