Skip to content

Commit 9e445df

Browse files
authored
Merge pull request #24 from Azure/no-comment-repetition
fix issue of adding repetitive comment when run recipe multiple times
2 parents 10bfef9 + 9ee4be4 commit 9e445df

File tree

7 files changed

+63
-16
lines changed

7 files changed

+63
-16
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.azure.spring.migration.openrewrite.java.search;
18+
19+
import java.util.List;
20+
import java.util.Objects;
21+
import org.openrewrite.internal.lang.Nullable;
22+
23+
import org.openrewrite.java.tree.J;
24+
import org.openrewrite.java.tree.TextComment;
25+
import org.openrewrite.marker.SearchResult;
26+
27+
import static org.openrewrite.Tree.randomId;
28+
29+
public class AddComment {
30+
public static <T extends J> T addIfAbsent(@Nullable T t, String description) {
31+
if (t == null) {
32+
//noinspection ConstantConditions
33+
return null;
34+
}
35+
List<SearchResult> existingSearchResult = t.getMarkers().findAll(SearchResult.class);
36+
if (existingSearchResult.stream().anyMatch(sr -> Objects.equals(sr.getDescription(), description))) {
37+
return t;
38+
}
39+
if (t.getPrefix().getComments().stream().anyMatch(comment -> comment instanceof TextComment && ((TextComment) comment).getText().contains(description))) {
40+
return t;
41+
}
42+
return t.withMarkers(t.getMarkers().computeByType(new SearchResult(randomId(), description), (s1, s2) -> s1 == null ? s2 : s1));
43+
}
44+
45+
}

src/main/java/com/azure/spring/migration/openrewrite/java/search/FindLiterals.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.openrewrite.java.JavaVisitor;
3030
import org.openrewrite.java.tree.J;
3131
import org.openrewrite.java.tree.JavaType.Primitive;
32-
import org.openrewrite.marker.SearchResult;
3332

3433
@EqualsAndHashCode(callSuper = true)
3534
@Value
@@ -69,10 +68,10 @@ public FindLiterals(final String pattern, @Nullable final String mark) {
6968
public J. @NonNull Literal visitLiteral(J. @NonNull Literal literal, @NonNull ExecutionContext ctx) {
7069
if (literal.getValueSource() != null) {
7170
if (literal.getType() == Primitive.String && compiledPattern.matcher(literal.getValueSource().substring(1, literal.getValueSource().length() - 1)).matches()) {
72-
return SearchResult.found(literal,mark);
71+
return AddComment.addIfAbsent(literal,mark);
7372
}
7473
if (compiledPattern.matcher(literal.getValueSource()).matches()) {
75-
return SearchResult.found(literal,mark);
74+
return AddComment.addIfAbsent(literal,mark);
7675
}
7776
}
7877
return literal;

src/main/java/com/azure/spring/migration/openrewrite/java/search/FindMethods.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.openrewrite.java.tree.Expression;
3232
import org.openrewrite.java.tree.J;
3333
import org.openrewrite.java.tree.JavaSourceFile;
34-
import org.openrewrite.marker.SearchResult;
3534

3635
/**
3736
* Finds matching method invocations.
@@ -107,7 +106,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
107106
method.printTrimmed(getCursor())
108107
));
109108
}
110-
m = SearchResult.found(m, mark);
109+
m = AddComment.addIfAbsent(m, mark);
111110
} else {
112111
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(method)));
113112
}
@@ -127,7 +126,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
127126
memberRef.printTrimmed(getCursor())
128127
));
129128
}
130-
m = m.withReference(SearchResult.found(m.getReference(), mark));
129+
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
131130
} else {
132131
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(memberRef)));
133132
}
@@ -147,7 +146,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
147146
newClass.printTrimmed(getCursor())
148147
));
149148
}
150-
n = SearchResult.found(n, mark);
149+
n = AddComment.addIfAbsent(n, mark);
151150
} else {
152151
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(newClass)));
153152
}

src/main/java/com/azure/spring/migration/openrewrite/java/search/FindTypes.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.openrewrite.java.tree.JavaType;
3131
import org.openrewrite.java.tree.NameTree;
3232
import org.openrewrite.java.tree.TypeUtils;
33-
import org.openrewrite.marker.SearchResult;
3433

3534
import lombok.EqualsAndHashCode;
3635

@@ -85,7 +84,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
8584
JavaType.FullyQualified type = TypeUtils.asFullyQualified(ident.getType());
8685
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
8786
ident.getSimpleName().equals(type.getClassName())) {
88-
return SearchResult.found(ident, mark);
87+
return AddComment.addIfAbsent(ident, mark);
8988
}
9089
}
9190
return super.visitIdentifier(ident, executionContext);
@@ -97,7 +96,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
9796
JavaType.FullyQualified type = TypeUtils.asFullyQualified(n.getType());
9897
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
9998
getCursor().firstEnclosing(J.Import.class) == null) {
100-
return SearchResult.found(n, mark);
99+
return AddComment.addIfAbsent(n, mark);
101100
}
102101
return n;
103102
}
@@ -108,7 +107,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
108107
JavaType.FullyQualified type = TypeUtils.asFullyQualified(fa.getTarget().getType());
109108
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
110109
fa.getName().getSimpleName().equals("class")) {
111-
return SearchResult.found(fa, mark);
110+
return AddComment.addIfAbsent(fa, mark);
112111
}
113112
return fa;
114113
}

src/main/java/com/azure/spring/migration/openrewrite/yaml/FindProperty.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public class FindProperty extends Recipe {
6161
}
6262

6363
String HASH_TAG = "#";
64-
String wrapComment(String commentText, String indent) {
65-
return HASH_TAG + commentText + indent;
64+
String LINE_BREAK = "\n";
65+
String wrapComment(String commentText, String prefix) {
66+
return prefix + HASH_TAG + commentText + (prefix.contains(LINE_BREAK) ? prefix : LINE_BREAK + prefix);
6667
}
6768

6869
@Override
@@ -76,9 +77,9 @@ String wrapComment(String commentText, String indent) {
7677
if (!Boolean.FALSE.equals(relaxedBinding) ?
7778
NameCaseConvention.matchesGlobRelaxedBinding(prop, propertyKey) :
7879
StringUtils.matchesGlob(prop, propertyKey)) {
79-
String wrappedComment = wrapComment(commentText, e.getPrefix());
80-
if (!e.getKey().getPrefix().startsWith(wrappedComment)) {
81-
e = e.withKey(e.getKey().withPrefix(wrappedComment));
80+
if (!e.getPrefix().contains(commentText) && !e.getKey().getPrefix().contains(commentText)) {
81+
String wrappedComment = wrapComment(commentText, e.getPrefix());
82+
e = e.withPrefix(wrappedComment);
8283
}
8384
}
8485
return e;

src/test/java/com/azure/spring/migration/openrewrite/java/search/FindMethodsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.test.RewriteTest;
2323

2424
public final class FindMethodsTest implements RewriteTest {
25+
2526
@Test
2627
void testFindMethods() {
2728
rewriteRun(

src/test/java/com/azure/spring/migration/openrewrite/yaml/FindPropertyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ void testFindPassword() {
2828
spec -> spec.recipe(new FindProperty("*password","TODO ASA-FindPassword: Don't save passwords or login information in files",true)),
2929
yaml(
3030
"""
31+
password: test
3132
application:
3233
Password: 1111
3334
application:
3435
password:
3536
test: 111
3637
""",
3738
"""
39+
#TODO ASA-FindPassword: Don't save passwords or login information in files
40+
password: test
3841
application:
3942
#TODO ASA-FindPassword: Don't save passwords or login information in files
4043
Password: 1111

0 commit comments

Comments
 (0)