Skip to content

Commit

Permalink
properly removing methods
Browse files Browse the repository at this point in the history
borrowed some code from openrewrite and bent it to my will
  • Loading branch information
evanchooly committed Dec 3, 2024
1 parent 46cf59f commit 27a8b40
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.morphia.rewrite.recipes;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J.Identifier;
import org.openrewrite.java.tree.J.MethodInvocation;
import org.openrewrite.java.tree.JavaType;

public class MorphiaConfigMigration extends Recipe {
private static final String OLD_TYPE = "dev.morphia.mapping.MapperOptions";

private static final String NEW_TYPE = "dev.morphia.config.MorphiaConfig";

@Override
public String getDisplayName() {
return "Migrate Morphia MapperOptions to MorphiaConfig";
}

@Override
public String getDescription() {
return "Converts uses of dev.morphia.mapping.MapperOptions to dev.morphia.config.MorphiaConfig.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(OLD_TYPE, true),
new MorphiaConfigMigrationVisitor());
}

private static class MorphiaConfigMigrationVisitor extends JavaIsoVisitor<ExecutionContext> {
private static final MethodMatcher BUILDER_MATCHER = new MethodMatcher("dev.morphia.mapping.MapperOptions builder()");

@Override
public MethodInvocation visitMethodInvocation(@NotNull MethodInvocation methodInvocation, @NotNull ExecutionContext context) {
if (BUILDER_MATCHER.matches(methodInvocation)) {
return methodInvocation.withName(methodInvocation.getName().withSimpleName("load"))
.withSelect(((Identifier) methodInvocation.getSelect())
.withSimpleName("MorphiaConfig")
.withType(JavaType.buildType(NEW_TYPE)));
} else {
return super.visitMethodInvocation(methodInvocation, context);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 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 dev.morphia.rewrite.recipes.openrewrite;

import java.util.Objects;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.search.UsesMethod;

import static java.util.Collections.singletonList;

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

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

@Override
public String getDescription() {
return "Remove method invocations if syntactically safe.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(methodPattern),
new RemoveMethodInvocationsVisitor(singletonList(methodPattern)));
}

public String getMethodPattern() {
return methodPattern;
}

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

@Override
public boolean equals(Object o) {
if (!(o instanceof RemoveMethodInvocations that)) {
return false;
}
if (!super.equals(o)) {
return false;
}
return Objects.equals(methodPattern, that.methodPattern);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), methodPattern);
}
}
Loading

0 comments on commit 27a8b40

Please sign in to comment.