Skip to content

Commit 440e6be

Browse files
authored
Merge pull request #27 from Azure/update-versions
Update to be compatible with latest rewrite
2 parents 2be7ec4 + a90a9c2 commit 440e6be

File tree

5 files changed

+44
-109
lines changed

5 files changed

+44
-109
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id("org.openrewrite.build.recipe-library") version "1.8.1"
2+
id("org.openrewrite.build.recipe-library") version "1.12.0"
33
}
44

55
group = "com.azure.spring.migration"

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

Lines changed: 30 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
import lombok.EqualsAndHashCode;
2020
import lombok.Value;
2121
import org.openrewrite.*;
22-
import org.openrewrite.internal.StringUtils;
2322
import org.openrewrite.internal.lang.NonNull;
2423
import org.openrewrite.internal.lang.Nullable;
2524
import org.openrewrite.java.JavaIsoVisitor;
2625
import org.openrewrite.java.MethodMatcher;
27-
import org.openrewrite.java.dataflow.FindLocalFlowPaths;
28-
import org.openrewrite.java.dataflow.LocalFlowSpec;
29-
import org.openrewrite.java.dataflow.LocalTaintFlowSpec;
26+
import org.openrewrite.java.search.UsesMethod;
3027
import org.openrewrite.java.table.MethodCalls;
31-
import org.openrewrite.java.tree.Expression;
3228
import org.openrewrite.java.tree.J;
3329
import org.openrewrite.java.tree.JavaSourceFile;
3430

@@ -55,27 +51,18 @@ public class FindMethods extends Recipe {
5551
@Nullable
5652
Boolean matchOverrides;
5753

58-
@Option(displayName = "Show flow",
59-
description = "When enabled, show the data or taint flow of the method invocation.",
60-
valid = {"none", "data", "taint"},
61-
required = false
62-
)
63-
@Nullable
64-
String flow;
65-
6654
@Option(displayName = "mark",
6755
description = "Mark in matched types",
6856
required = false)
6957
String mark;
7058

7159
org.openrewrite.java.search.FindMethods findMethods;
7260

73-
public FindMethods(final String methodPattern, @Nullable final Boolean matchOverrides, @Nullable final String flow, @Nullable final String mark) {
61+
public FindMethods(final String methodPattern, @Nullable final Boolean matchOverrides, @Nullable final String mark) {
7462
this.methodPattern = methodPattern;
7563
this.matchOverrides = matchOverrides;
76-
this.flow = flow;
7764
this.mark = mark;
78-
this.findMethods = new org.openrewrite.java.search.FindMethods(methodPattern, matchOverrides, flow);
65+
this.findMethods = new org.openrewrite.java.search.FindMethods(methodPattern, matchOverrides);
7966
}
8067

8168
@Override
@@ -90,101 +77,57 @@ public FindMethods(final String methodPattern, @Nullable final Boolean matchOver
9077

9178
@Override
9279
@SuppressWarnings("ConstantConditions")
93-
public @NonNull TreeVisitor<?, ExecutionContext> getVisitor() {
80+
public TreeVisitor<?, ExecutionContext> getVisitor() {
9481
MethodMatcher methodMatcher = new MethodMatcher(methodPattern, matchOverrides);
95-
boolean flowEnabled = !StringUtils.isBlank(flow) && !"none".equals(flow);
96-
return new JavaIsoVisitor<ExecutionContext>() {
82+
return Preconditions.check(new UsesMethod<>(methodPattern, matchOverrides), new JavaIsoVisitor<ExecutionContext>() {
9783
@Override
98-
public @NonNull J.MethodInvocation visitMethodInvocation(@NonNull J.MethodInvocation method, @NonNull ExecutionContext ctx) {
84+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
9985
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
10086
if (methodMatcher.matches(method)) {
101-
if (!flowEnabled) {
102-
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
103-
if(javaSourceFile != null) {
104-
methodCalls.insertRow(ctx, new MethodCalls.Row(
105-
javaSourceFile.getSourcePath().toString(),
106-
method.printTrimmed(getCursor())
107-
));
108-
}
109-
m = AddComment.addIfAbsent(m, mark);
110-
} else {
111-
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(method)));
87+
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
88+
if (javaSourceFile != null) {
89+
methodCalls.insertRow(ctx, new MethodCalls.Row(
90+
javaSourceFile.getSourcePath().toString(),
91+
method.printTrimmed(getCursor())
92+
));
11293
}
94+
m = AddComment.addIfAbsent(m, mark);
11395
}
11496
return m;
11597
}
11698

11799
@Override
118-
public @NonNull J.MemberReference visitMemberReference(@NonNull J.MemberReference memberRef, @NonNull ExecutionContext ctx) {
100+
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
119101
J.MemberReference m = super.visitMemberReference(memberRef, ctx);
120102
if (methodMatcher.matches(m.getMethodType())) {
121-
if (!flowEnabled) {
122-
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
123-
if(javaSourceFile != null) {
124-
methodCalls.insertRow(ctx, new MethodCalls.Row(
125-
javaSourceFile.getSourcePath().toString(),
126-
memberRef.printTrimmed(getCursor())
127-
));
128-
}
129-
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
130-
} else {
131-
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(memberRef)));
103+
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
104+
if (javaSourceFile != null) {
105+
methodCalls.insertRow(ctx, new MethodCalls.Row(
106+
javaSourceFile.getSourcePath().toString(),
107+
memberRef.printTrimmed(getCursor())
108+
));
132109
}
110+
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
133111
}
134112
return m;
135113
}
136114

137115
@Override
138-
public @NonNull J.NewClass visitNewClass(@NonNull J.NewClass newClass, @NonNull ExecutionContext ctx) {
116+
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
139117
J.NewClass n = super.visitNewClass(newClass, ctx);
140118
if (methodMatcher.matches(newClass)) {
141-
if (!flowEnabled) {
142-
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
143-
if(javaSourceFile != null) {
144-
methodCalls.insertRow(ctx, new MethodCalls.Row(
145-
javaSourceFile.getSourcePath().toString(),
146-
newClass.printTrimmed(getCursor())
147-
));
148-
}
149-
n = AddComment.addIfAbsent(n, mark);
150-
} else {
151-
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(newClass)));
119+
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
120+
if (javaSourceFile != null) {
121+
methodCalls.insertRow(ctx, new MethodCalls.Row(
122+
javaSourceFile.getSourcePath().toString(),
123+
newClass.printTrimmed(getCursor())
124+
));
152125
}
126+
n = AddComment.addIfAbsent(n, mark);
153127
}
154128
return n;
155129
}
156-
157-
private LocalFlowSpec<Expression, Expression> getFlowSpec(Expression source) {
158-
switch (flow) {
159-
case "data":
160-
return new LocalFlowSpec<Expression, Expression>() {
161-
@Override
162-
public boolean isSource(@NonNull Expression expression, @NonNull Cursor cursor) {
163-
return expression == source;
164-
}
165-
166-
@Override
167-
public boolean isSink(@NonNull Expression expression, @NonNull Cursor cursor) {
168-
return true;
169-
}
170-
};
171-
case "taint":
172-
return new LocalTaintFlowSpec<Expression, Expression>() {
173-
@Override
174-
public boolean isSource(@NonNull Expression expression, @NonNull Cursor cursor) {
175-
return expression == source;
176-
}
177-
178-
@Override
179-
public boolean isSink(@NonNull Expression expression, @NonNull Cursor cursor) {
180-
return true;
181-
}
182-
};
183-
default:
184-
throw new IllegalStateException("Unknown flow: " + flow);
185-
}
186-
}
187-
};
130+
});
188131
}
189132

190133
}

src/main/java/com/azure/spring/migration/openrewrite/xml/AddConsoleCommentInLog4j.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openrewrite.ExecutionContext;
2525
import org.openrewrite.HasSourcePath;
2626
import org.openrewrite.Option;
27+
import org.openrewrite.Preconditions;
2728
import org.openrewrite.Recipe;
2829
import org.openrewrite.TreeVisitor;
2930
import org.openrewrite.internal.lang.NonNull;
@@ -63,11 +64,6 @@ public class AddConsoleCommentInLog4j extends Recipe {
6364
return "Adds a comment in a `XML` tag of matched attribute.";
6465
}
6566

66-
@Override
67-
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
68-
return new HasSourcePath<>(fileMatcher);
69-
}
70-
7167
public boolean checkLog4j1HasConsole(Xml.Tag tag) {
7268
AttributeToFind log4j1KeyAttribute = ATTRIBUTE_MAP.get("log4j1");
7369
return !XmlUtil.searchChildTag(tag, APPENDER_TAG_NAME) || XmlUtil.searchChildAttribute(tag, APPENDER_TAG_NAME, log4j1KeyAttribute.attributeName, log4j1KeyAttribute.attributeValueKeyword);
@@ -81,7 +77,7 @@ public boolean checkLog4j2HasConsole(Xml.Tag tag) {
8177

8278
@Override
8379
public @NonNull TreeVisitor<?, ExecutionContext> getVisitor() {
84-
return new XmlVisitor<ExecutionContext>() {
80+
return Preconditions.check(fileMatcher != null ? new HasSourcePath<>(fileMatcher) : TreeVisitor.noop(), new XmlVisitor<ExecutionContext>() {
8581
final CaseInsensitiveXPathMatcher log4j2AppendersTagMatcher = new CaseInsensitiveXPathMatcher(LOG4J2_APPENDERS_XPATH);
8682
final CaseInsensitiveXPathMatcher log4jConfigurationTagMatcher = new CaseInsensitiveXPathMatcher(LOG4J_CONFIGURATION_XPATH);
8783

@@ -99,6 +95,6 @@ public boolean checkLog4j2HasConsole(Xml.Tag tag) {
9995
}
10096
return t;
10197
}
102-
};
98+
});
10399
}
104100
}

src/main/java/com/azure/spring/migration/openrewrite/xml/AddConsoleCommentInLogback.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.ExecutionContext;
2222
import org.openrewrite.HasSourcePath;
2323
import org.openrewrite.Option;
24+
import org.openrewrite.Preconditions;
2425
import org.openrewrite.Recipe;
2526
import org.openrewrite.TreeVisitor;
2627
import org.openrewrite.internal.lang.NonNull;
@@ -55,19 +56,14 @@ public class AddConsoleCommentInLogback extends Recipe {
5556
return "Adds a comment in a `XML` tag of matched attribute.";
5657
}
5758

58-
@Override
59-
protected @NonNull TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
60-
return new HasSourcePath<>(fileMatcher);
61-
}
62-
6359
private boolean checkLogbackHasConsole(Xml.Tag tag) {
6460
return !XmlUtil.searchChildTag(tag, APPENDER_TAG_NAME) || XmlUtil.searchChildAttribute(tag, APPENDER_TAG_NAME, KEY_ATTRIBUTE.attributeName,
6561
KEY_ATTRIBUTE.attributeValueKeyword);
6662
}
6763

6864
@Override
6965
public @NonNull TreeVisitor<?, ExecutionContext> getVisitor() {
70-
return new XmlVisitor<ExecutionContext>() {
66+
return Preconditions.check(fileMatcher != null ? new HasSourcePath<>(fileMatcher) : TreeVisitor.noop(), new XmlVisitor<ExecutionContext>() {
7167
final CaseInsensitiveXPathMatcher configurationTagMatcher = new CaseInsensitiveXPathMatcher(CONFIGURATION_XPATH);
7268

7369
@Override
@@ -80,6 +76,6 @@ private boolean checkLogbackHasConsole(Xml.Tag tag) {
8076
}
8177
return t;
8278
}
83-
};
79+
});
8480
}
8581
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class FindMethodsTest implements RewriteTest {
2626
@Test
2727
void testFindMethods() {
2828
rewriteRun(
29-
spec -> spec.recipe(new FindMethods("java.io.File *(..)",true,null, "TODO ASA-FileStorageApi: need configuration to use storage")),
29+
spec -> spec.recipe(new FindMethods("java.io.File *(..)",true, "TODO ASA-FileStorageApi: need configuration to use storage")),
3030
java(
3131
"""
3232
import java.io.File;
@@ -50,7 +50,7 @@ public void test(){
5050
);
5151

5252
rewriteRun(
53-
spec -> spec.recipe(new FindMethods("java.lang.System getenv(..)",true,null, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
53+
spec -> spec.recipe(new FindMethods("java.lang.System getenv(..)",true, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
5454
java(
5555
"""
5656
public class LocalEnv {
@@ -70,7 +70,7 @@ public void test(){
7070
);
7171

7272
rewriteRun(
73-
spec -> spec.recipe(new FindMethods("java.lang.System getProperty(..)",true,null, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
73+
spec -> spec.recipe(new FindMethods("java.lang.System getProperty(..)",true, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
7474
java(
7575
"""
7676
public class LocalProperty {
@@ -90,7 +90,7 @@ public void test(){
9090
);
9191

9292
rewriteRun(
93-
spec -> spec.recipe(new FindMethods("java.lang.System setProperties(..)",true,null, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
93+
spec -> spec.recipe(new FindMethods("java.lang.System setProperties(..)",true, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
9494
java(
9595
"""
9696
public class LocalProperty {
@@ -110,7 +110,7 @@ public void test(java.util.Properties p){
110110
);
111111

112112
rewriteRun(
113-
spec -> spec.recipe(new FindMethods("java.lang.System setProperty(..)",true,null, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
113+
spec -> spec.recipe(new FindMethods("java.lang.System setProperty(..)",true, "TODO ASA-JavaSystemConfig: need environment configuration in azure spring apps")),
114114
java(
115115
"""
116116
public class LocalProperty {
@@ -130,7 +130,7 @@ public void test(String key, String value){
130130
);
131131

132132
rewriteRun(
133-
spec -> spec.recipe(new FindMethods("java.lang.System loadLibrary(..)",true,null, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
133+
spec -> spec.recipe(new FindMethods("java.lang.System loadLibrary(..)",true, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
134134
java(
135135
"""
136136
public class LocalNative {
@@ -152,7 +152,7 @@ public void test(){
152152
);
153153

154154
rewriteRun(
155-
spec -> spec.recipe(new FindMethods("java.lang.System load(..)",true,null, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
155+
spec -> spec.recipe(new FindMethods("java.lang.System load(..)",true, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
156156
java(
157157
"""
158158
import java.io.File;

0 commit comments

Comments
 (0)