Skip to content

Commit

Permalink
Merge pull request #24 from Azure/no-comment-repetition
Browse files Browse the repository at this point in the history
fix issue of adding repetitive comment when run recipe multiple times
  • Loading branch information
KaiqianYang authored Mar 28, 2023
2 parents 10bfef9 + 9ee4be4 commit 9e445df
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <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 com.azure.spring.migration.openrewrite.java.search;

import java.util.List;
import java.util.Objects;
import org.openrewrite.internal.lang.Nullable;

import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TextComment;
import org.openrewrite.marker.SearchResult;

import static org.openrewrite.Tree.randomId;

public class AddComment {
public static <T extends J> T addIfAbsent(@Nullable T t, String description) {
if (t == null) {
//noinspection ConstantConditions
return null;
}
List<SearchResult> existingSearchResult = t.getMarkers().findAll(SearchResult.class);
if (existingSearchResult.stream().anyMatch(sr -> Objects.equals(sr.getDescription(), description))) {
return t;
}
if (t.getPrefix().getComments().stream().anyMatch(comment -> comment instanceof TextComment && ((TextComment) comment).getText().contains(description))) {
return t;
}
return t.withMarkers(t.getMarkers().computeByType(new SearchResult(randomId(), description), (s1, s2) -> s1 == null ? s2 : s1));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType.Primitive;
import org.openrewrite.marker.SearchResult;

@EqualsAndHashCode(callSuper = true)
@Value
Expand Down Expand Up @@ -69,10 +68,10 @@ public FindLiterals(final String pattern, @Nullable final String mark) {
public J. @NonNull Literal visitLiteral(J. @NonNull Literal literal, @NonNull ExecutionContext ctx) {
if (literal.getValueSource() != null) {
if (literal.getType() == Primitive.String && compiledPattern.matcher(literal.getValueSource().substring(1, literal.getValueSource().length() - 1)).matches()) {
return SearchResult.found(literal,mark);
return AddComment.addIfAbsent(literal,mark);
}
if (compiledPattern.matcher(literal.getValueSource()).matches()) {
return SearchResult.found(literal,mark);
return AddComment.addIfAbsent(literal,mark);
}
}
return literal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.marker.SearchResult;

/**
* Finds matching method invocations.
Expand Down Expand Up @@ -107,7 +106,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
method.printTrimmed(getCursor())
));
}
m = SearchResult.found(m, mark);
m = AddComment.addIfAbsent(m, mark);
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(method)));
}
Expand All @@ -127,7 +126,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
memberRef.printTrimmed(getCursor())
));
}
m = m.withReference(SearchResult.found(m.getReference(), mark));
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(memberRef)));
}
Expand All @@ -147,7 +146,7 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
newClass.printTrimmed(getCursor())
));
}
n = SearchResult.found(n, mark);
n = AddComment.addIfAbsent(n, mark);
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(newClass)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.NameTree;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.SearchResult;

import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -85,7 +84,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
JavaType.FullyQualified type = TypeUtils.asFullyQualified(ident.getType());
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
ident.getSimpleName().equals(type.getClassName())) {
return SearchResult.found(ident, mark);
return AddComment.addIfAbsent(ident, mark);
}
}
return super.visitIdentifier(ident, executionContext);
Expand All @@ -97,7 +96,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
JavaType.FullyQualified type = TypeUtils.asFullyQualified(n.getType());
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
getCursor().firstEnclosing(J.Import.class) == null) {
return SearchResult.found(n, mark);
return AddComment.addIfAbsent(n, mark);
}
return n;
}
Expand All @@ -108,7 +107,7 @@ public FindTypes(final String fullyQualifiedTypeName, @Nullable final Boolean ch
JavaType.FullyQualified type = TypeUtils.asFullyQualified(fa.getTarget().getType());
if (typeMatches(Boolean.TRUE.equals(checkAssignability), fullyQualifiedType, type) &&
fa.getName().getSimpleName().equals("class")) {
return SearchResult.found(fa, mark);
return AddComment.addIfAbsent(fa, mark);
}
return fa;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public class FindProperty extends Recipe {
}

String HASH_TAG = "#";
String wrapComment(String commentText, String indent) {
return HASH_TAG + commentText + indent;
String LINE_BREAK = "\n";
String wrapComment(String commentText, String prefix) {
return prefix + HASH_TAG + commentText + (prefix.contains(LINE_BREAK) ? prefix : LINE_BREAK + prefix);
}

@Override
Expand All @@ -76,9 +77,9 @@ String wrapComment(String commentText, String indent) {
if (!Boolean.FALSE.equals(relaxedBinding) ?
NameCaseConvention.matchesGlobRelaxedBinding(prop, propertyKey) :
StringUtils.matchesGlob(prop, propertyKey)) {
String wrappedComment = wrapComment(commentText, e.getPrefix());
if (!e.getKey().getPrefix().startsWith(wrappedComment)) {
e = e.withKey(e.getKey().withPrefix(wrappedComment));
if (!e.getPrefix().contains(commentText) && !e.getKey().getPrefix().contains(commentText)) {
String wrappedComment = wrapComment(commentText, e.getPrefix());
e = e.withPrefix(wrappedComment);
}
}
return e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.test.RewriteTest;

public final class FindMethodsTest implements RewriteTest {

@Test
void testFindMethods() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ void testFindPassword() {
spec -> spec.recipe(new FindProperty("*password","TODO ASA-FindPassword: Don't save passwords or login information in files",true)),
yaml(
"""
password: test
application:
Password: 1111
application:
password:
test: 111
""",
"""
#TODO ASA-FindPassword: Don't save passwords or login information in files
password: test
application:
#TODO ASA-FindPassword: Don't save passwords or login information in files
Password: 1111
Expand Down

0 comments on commit 9e445df

Please sign in to comment.