Skip to content

Commit

Permalink
Merge pull request #27 from Azure/update-versions
Browse files Browse the repository at this point in the history
Update to be compatible with latest rewrite
  • Loading branch information
KaiqianYang committed Jun 9, 2023
2 parents 2be7ec4 + a90a9c2 commit 440e6be
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("org.openrewrite.build.recipe-library") version "1.8.1"
id("org.openrewrite.build.recipe-library") version "1.12.0"
}

group = "com.azure.spring.migration"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.dataflow.FindLocalFlowPaths;
import org.openrewrite.java.dataflow.LocalFlowSpec;
import org.openrewrite.java.dataflow.LocalTaintFlowSpec;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.table.MethodCalls;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;

Expand All @@ -55,27 +51,18 @@ public class FindMethods extends Recipe {
@Nullable
Boolean matchOverrides;

@Option(displayName = "Show flow",
description = "When enabled, show the data or taint flow of the method invocation.",
valid = {"none", "data", "taint"},
required = false
)
@Nullable
String flow;

@Option(displayName = "mark",
description = "Mark in matched types",
required = false)
String mark;

org.openrewrite.java.search.FindMethods findMethods;

public FindMethods(final String methodPattern, @Nullable final Boolean matchOverrides, @Nullable final String flow, @Nullable final String mark) {
public FindMethods(final String methodPattern, @Nullable final Boolean matchOverrides, @Nullable final String mark) {
this.methodPattern = methodPattern;
this.matchOverrides = matchOverrides;
this.flow = flow;
this.mark = mark;
this.findMethods = new org.openrewrite.java.search.FindMethods(methodPattern, matchOverrides, flow);
this.findMethods = new org.openrewrite.java.search.FindMethods(methodPattern, matchOverrides);
}

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

@Override
@SuppressWarnings("ConstantConditions")
public @NonNull TreeVisitor<?, ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher methodMatcher = new MethodMatcher(methodPattern, matchOverrides);
boolean flowEnabled = !StringUtils.isBlank(flow) && !"none".equals(flow);
return new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesMethod<>(methodPattern, matchOverrides), new JavaIsoVisitor<ExecutionContext>() {
@Override
public @NonNull J.MethodInvocation visitMethodInvocation(@NonNull J.MethodInvocation method, @NonNull ExecutionContext ctx) {
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (methodMatcher.matches(method)) {
if (!flowEnabled) {
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if(javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
method.printTrimmed(getCursor())
));
}
m = AddComment.addIfAbsent(m, mark);
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(method)));
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if (javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
method.printTrimmed(getCursor())
));
}
m = AddComment.addIfAbsent(m, mark);
}
return m;
}

@Override
public @NonNull J.MemberReference visitMemberReference(@NonNull J.MemberReference memberRef, @NonNull ExecutionContext ctx) {
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
J.MemberReference m = super.visitMemberReference(memberRef, ctx);
if (methodMatcher.matches(m.getMethodType())) {
if (!flowEnabled) {
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if(javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
memberRef.printTrimmed(getCursor())
));
}
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(memberRef)));
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if (javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
memberRef.printTrimmed(getCursor())
));
}
m = m.withReference(AddComment.addIfAbsent(m.getReference(), mark));
}
return m;
}

@Override
public @NonNull J.NewClass visitNewClass(@NonNull J.NewClass newClass, @NonNull ExecutionContext ctx) {
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass n = super.visitNewClass(newClass, ctx);
if (methodMatcher.matches(newClass)) {
if (!flowEnabled) {
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if(javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
newClass.printTrimmed(getCursor())
));
}
n = AddComment.addIfAbsent(n, mark);
} else {
doAfterVisit(new FindLocalFlowPaths<>(getFlowSpec(newClass)));
JavaSourceFile javaSourceFile = getCursor().firstEnclosing(JavaSourceFile.class);
if (javaSourceFile != null) {
methodCalls.insertRow(ctx, new MethodCalls.Row(
javaSourceFile.getSourcePath().toString(),
newClass.printTrimmed(getCursor())
));
}
n = AddComment.addIfAbsent(n, mark);
}
return n;
}

private LocalFlowSpec<Expression, Expression> getFlowSpec(Expression source) {
switch (flow) {
case "data":
return new LocalFlowSpec<Expression, Expression>() {
@Override
public boolean isSource(@NonNull Expression expression, @NonNull Cursor cursor) {
return expression == source;
}

@Override
public boolean isSink(@NonNull Expression expression, @NonNull Cursor cursor) {
return true;
}
};
case "taint":
return new LocalTaintFlowSpec<Expression, Expression>() {
@Override
public boolean isSource(@NonNull Expression expression, @NonNull Cursor cursor) {
return expression == source;
}

@Override
public boolean isSink(@NonNull Expression expression, @NonNull Cursor cursor) {
return true;
}
};
default:
throw new IllegalStateException("Unknown flow: " + flow);
}
}
};
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.HasSourcePath;
import org.openrewrite.Option;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.NonNull;
Expand Down Expand Up @@ -63,11 +64,6 @@ public class AddConsoleCommentInLog4j extends Recipe {
return "Adds a comment in a `XML` tag of matched attribute.";
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new HasSourcePath<>(fileMatcher);
}

public boolean checkLog4j1HasConsole(Xml.Tag tag) {
AttributeToFind log4j1KeyAttribute = ATTRIBUTE_MAP.get("log4j1");
return !XmlUtil.searchChildTag(tag, APPENDER_TAG_NAME) || XmlUtil.searchChildAttribute(tag, APPENDER_TAG_NAME, log4j1KeyAttribute.attributeName, log4j1KeyAttribute.attributeValueKeyword);
Expand All @@ -81,7 +77,7 @@ public boolean checkLog4j2HasConsole(Xml.Tag tag) {

@Override
public @NonNull TreeVisitor<?, ExecutionContext> getVisitor() {
return new XmlVisitor<ExecutionContext>() {
return Preconditions.check(fileMatcher != null ? new HasSourcePath<>(fileMatcher) : TreeVisitor.noop(), new XmlVisitor<ExecutionContext>() {
final CaseInsensitiveXPathMatcher log4j2AppendersTagMatcher = new CaseInsensitiveXPathMatcher(LOG4J2_APPENDERS_XPATH);
final CaseInsensitiveXPathMatcher log4jConfigurationTagMatcher = new CaseInsensitiveXPathMatcher(LOG4J_CONFIGURATION_XPATH);

Expand All @@ -99,6 +95,6 @@ public boolean checkLog4j2HasConsole(Xml.Tag tag) {
}
return t;
}
};
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.HasSourcePath;
import org.openrewrite.Option;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.NonNull;
Expand Down Expand Up @@ -55,19 +56,14 @@ public class AddConsoleCommentInLogback extends Recipe {
return "Adds a comment in a `XML` tag of matched attribute.";
}

@Override
protected @NonNull TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new HasSourcePath<>(fileMatcher);
}

private boolean checkLogbackHasConsole(Xml.Tag tag) {
return !XmlUtil.searchChildTag(tag, APPENDER_TAG_NAME) || XmlUtil.searchChildAttribute(tag, APPENDER_TAG_NAME, KEY_ATTRIBUTE.attributeName,
KEY_ATTRIBUTE.attributeValueKeyword);
}

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

@Override
Expand All @@ -80,6 +76,6 @@ private boolean checkLogbackHasConsole(Xml.Tag tag) {
}
return t;
}
};
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class FindMethodsTest implements RewriteTest {
@Test
void testFindMethods() {
rewriteRun(
spec -> spec.recipe(new FindMethods("java.io.File *(..)",true,null, "TODO ASA-FileStorageApi: need configuration to use storage")),
spec -> spec.recipe(new FindMethods("java.io.File *(..)",true, "TODO ASA-FileStorageApi: need configuration to use storage")),
java(
"""
import java.io.File;
Expand All @@ -50,7 +50,7 @@ public void test(){
);

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

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

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

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

rewriteRun(
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")),
spec -> spec.recipe(new FindMethods("java.lang.System loadLibrary(..)",true, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
java(
"""
public class LocalNative {
Expand All @@ -152,7 +152,7 @@ public void test(){
);

rewriteRun(
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")),
spec -> spec.recipe(new FindMethods("java.lang.System load(..)",true, "TODO ASA-JavaSystemLoad: need to mount your own storage and upload your binary code")),
java(
"""
import java.io.File;
Expand Down

0 comments on commit 440e6be

Please sign in to comment.