Skip to content

Commit

Permalink
Limit searches for spring config files to resources folders
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Feb 4, 2025
1 parent da6cdd9 commit 8d36d88
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
org.openrewrite.properties.ChangePropertyKey subpropertiesChangePropertyKey =
new org.openrewrite.properties.ChangePropertyKey(Pattern.quote(oldPropertyKey + ".") + exceptRegex() + "(.+)", newPropertyKey + ".$1", true, true);

return new TreeVisitor<Tree, ExecutionContext>() {
return Preconditions.check(new IsPossibleSpringConfigFile(), new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof Yaml.Documents) {
Expand All @@ -88,7 +88,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}
return tree;
}
};
});
}

private String exceptRegex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
Recipe changeProperties = new org.openrewrite.properties.AddPropertyComment(propertyKey, comment, true);
Recipe changeYaml = new org.openrewrite.yaml.CommentOutProperty(propertyKey, comment, true);
return new TreeVisitor<Tree, ExecutionContext>() {
return Preconditions.check(new IsPossibleSpringConfigFile(), new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree preVisit(@NonNull Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
Expand All @@ -62,6 +62,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}
return tree;
}
};
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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.openrewrite.java.spring;

import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.marker.SourceSet;
import org.openrewrite.properties.tree.Properties;
import org.openrewrite.yaml.tree.Yaml;

/**
* A precondition to determine if a file might be a
* <a href="https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files">Spring configuration file</a>.
* This does not make positive identification of files which are spring configuration files, as there are few hard limits.
* Instead, this tries to rule out files which, due to their type or location, cannot be spring properties.
*/
public class IsPossibleSpringConfigFile extends TreeVisitor<Tree, ExecutionContext> {

@Override
@Nullable
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (((tree instanceof Yaml.Documents) || (tree instanceof Properties.File)) && tree.getMarkers().findFirst(SourceSet.class).isPresent()) {
return SearchResult.found(tree);
}
return tree;
}
}
Loading

0 comments on commit 8d36d88

Please sign in to comment.