Skip to content

Commit

Permalink
remove method declarations
Browse files Browse the repository at this point in the history
more internal test clean ups
remove Datastore.ensure*() calls
  • Loading branch information
evanchooly committed Jan 9, 2025
1 parent 35374b4 commit 0041760
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.morphia.rewrite.recipes.internal;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.MethodDeclaration;

public class RemoveMethodDeclaration extends Recipe {
@Option(displayName = "Method pattern", description = "A pattern to match method declarations for removal.", example = "java.lang.StringBuilder append(java.lang.String)")
private String methodPattern;

@Override
public String getDisplayName() {
return "Remove method declarations";
}

@Override
public String getDescription() {
return "Remove method declarations.";
}

public String getMethodPattern() {
return methodPattern;
}

public void setMethodPattern(String methodPattern) {
this.methodPattern = methodPattern;
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new RemoveMethodDeclarationVisitor(methodPattern);
}

private class RemoveMethodDeclarationVisitor extends JavaVisitor<ExecutionContext> {
private final MethodMatcher matcher;

public RemoveMethodDeclarationVisitor(MethodMatcher matcher) {
this.matcher = matcher;
}

public RemoveMethodDeclarationVisitor(String pattern) {
this(new MethodMatcher(pattern));
}

@Override
public J visitMethodDeclaration(MethodDeclaration method, ExecutionContext executionContext) {
return matcher.matches(method.getMethodType()) ? null : super.visitMethodDeclaration(method, executionContext);
}
}
}
19 changes: 19 additions & 0 deletions rewrite/src/main/resources/META-INF/rewrite/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.mapping.MapperOptions.PropertyDiscovery
newFullyQualifiedTypeName: dev.morphia.mapping.PropertyDiscovery
- org.openrewrite.java.RemoveMethodInvocations:
methodPattern: dev.morphia.MorphiaDatastore enableDocumentValidation(..)
- org.openrewrite.java.RemoveMethodInvocations:
methodPattern: dev.morphia.MorphiaDatastore enableCaps(..)
- org.openrewrite.java.RemoveMethodInvocations:
methodPattern: dev.morphia.MorphiaDatastore ensureIndexes(..)
- org.openrewrite.maven.UpgradeDependencyVersion:
groupId: dev.morphia.morphia
artifactId: morphia-core
Expand All @@ -44,3 +50,16 @@ recipeList:
filePattern: src/test/java/dev/morphia/test/models/Keys.java
- org.openrewrite.DeleteSourceFiles:
filePattern: src/test/java/dev/morphia/test/MorphiaVersionTest.java
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery testCriteriaContainers()
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery check()
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery testKeys()
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery testKeyList()
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery testFetchKeys()
- dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration:
methodPattern: dev.morphia.test.query.TestQuery check(dev.morphia.query.Query)
- org.openrewrite.java.RemoveUnusedImports
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public abstract class MorphiaRewriteTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
// spec.recipe(getRecipe())
// .parser(JavaParser.fromJavaVersion()
// .classpath(classpath()));

Builder<? extends JavaParser, ?> builder = JavaParser.fromJavaVersion()
.addClasspathEntry(Path.of(findMorphiaCore()));
findMongoDependencies().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.morphia.rewrite.recipes.test.internal;

import dev.morphia.rewrite.recipes.internal.RemoveMethodDeclaration;
import dev.morphia.rewrite.recipes.test.MorphiaRewriteTest;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.openrewrite.Recipe;

import static org.openrewrite.java.Assertions.java;

class RemoveMethodDeclarationTest extends MorphiaRewriteTest {

@Override
protected @NotNull Recipe getRecipe() {
RemoveMethodDeclaration declaration = new RemoveMethodDeclaration();
declaration.setMethodPattern("Test deleteMe(String)");
return declaration;
}

@Test
public void testRemoveMethod() {
rewriteRun(java("""
public class Test {
public String deleteMe() {
return "Not today!";
}
public void deleteMe(String param) {
}
}
""",
"""
public class Test {
public String deleteMe() {
return "Not today!";
}
}
"""));
}
}

0 comments on commit 0041760

Please sign in to comment.