-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
borrowed some code from openrewrite and bent it to my will
- Loading branch information
1 parent
46cf59f
commit 27a8b40
Showing
9 changed files
with
524 additions
and
18 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
rewrite/src/main/java/dev/morphia/rewrite/recipes/MorphiaConfigMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
rewrite/src/main/java/dev/morphia/rewrite/recipes/openrewrite/RemoveMethodInvocations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.