-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d9a06
commit 3ffbe14
Showing
8 changed files
with
288 additions
and
21 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
|
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
149 changes: 149 additions & 0 deletions
149
src/main/java/org/openrewrite/launchdarkly/MigrateUserToContext.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,149 @@ | ||
package org.openrewrite.launchdarkly; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.*; | ||
import org.openrewrite.internal.StringUtils; | ||
import org.openrewrite.java.*; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.marker.Markers; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class MigrateUserToContext extends Recipe { | ||
private static final MethodMatcher NEW_USER = new MethodMatcher("com.launchdarkly.sdk.LDUser <constructor>(java.lang.String)"); | ||
private static final MethodMatcher NEW_USER_BUILDER = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder <constructor>(java.lang.String)"); | ||
|
||
private static final List<String> BASIC_ATTRIBUTES = Arrays.asList("avatar", "country", "email", "firstName", "ip", "lastName"); | ||
private static final List<String> PRIVATE_ATTRIBUTES = Arrays.asList("privateAvatar", "privateCountry", "privateEmail", "privateFirstName", "privateIp", "privateLastName", "privateName"); | ||
private static final MethodMatcher BUILTIN_ATTRIBUTE = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder *(java.lang.String)"); | ||
private static final MethodMatcher BUILTIN_PRIVATE_ATTRIBUTE = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder private*(java.lang.String)"); | ||
private static final MethodMatcher CUSTOM_ATTRIBUTES = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder custom(java.lang.String, ..)"); // FIXME: This really should be `*` | ||
private static final MethodMatcher PRIVATE_CUSTOM_ATTRIBUTES = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder privateCustom(java.lang.String, ..)"); // FIXME: This really should be `*` | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Migrate `LDUser` to `LDContext`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Migrate `LDUser` to `LDContext`."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new UsesType<>("com.launchdarkly.sdk.LDUser", null), | ||
new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
if (NEW_USER.matches(newClass)) { | ||
maybeRemoveImport("com.launchdarkly.sdk.LDUser"); | ||
maybeAddImport("com.launchdarkly.sdk.LDContext"); | ||
doAfterVisit(new ChangeType("com.launchdarkly.sdk.LDUser", "com.launchdarkly.sdk.LDContext", null).getVisitor()); | ||
return JavaTemplate.builder("LDContext.create(#{any(java.lang.String)})") | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.LDContext") | ||
.build() | ||
.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0)); | ||
} else if (NEW_USER_BUILDER.matches(newClass)) { | ||
maybeRemoveImport("com.launchdarkly.sdk.LDUser"); | ||
maybeAddImport("com.launchdarkly.sdk.LDContext"); | ||
doAfterVisit(new ChangeType("com.launchdarkly.sdk.LDUser", "com.launchdarkly.sdk.LDContext", null).getVisitor()); | ||
return JavaTemplate.builder("LDContext.builder(#{any(java.lang.String)})") | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.LDContext") | ||
.build() | ||
.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0)); | ||
} | ||
|
||
return super.visitNewClass(newClass, ctx); | ||
} | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
if (BUILTIN_ATTRIBUTE.matches(m) && BASIC_ATTRIBUTES.contains(m.getSimpleName())) { | ||
String code; | ||
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})"; | ||
} else { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()})"; | ||
} | ||
return JavaTemplate.builder(code) | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.ContextBuilder") | ||
.build() | ||
.apply( | ||
getCursor(), | ||
m.getCoordinates().replace(), | ||
m.getSelect(), | ||
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, m.getSimpleName(), "\"" + m.getSimpleName() + "\"", null, JavaType.Primitive.String), | ||
m.getArguments().get(0) | ||
); | ||
} else if (BUILTIN_PRIVATE_ATTRIBUTE.matches(m) && PRIVATE_ATTRIBUTES.contains(m.getSimpleName())) { | ||
String code; | ||
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})\n.privateAttributes(#{any(java.lang.String)})"; | ||
} else { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()}).privateAttributes(#{any(java.lang.String)})"; | ||
} | ||
String attributeName = StringUtils.uncapitalize(m.getSimpleName().replace("private", "")); | ||
return JavaTemplate.builder(code) | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.ContextBuilder") | ||
.build() | ||
.apply( | ||
getCursor(), | ||
m.getCoordinates().replace(), | ||
m.getSelect(), | ||
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, attributeName, "\"" + attributeName + "\"", null, JavaType.Primitive.String), | ||
m.getArguments().get(0), | ||
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, attributeName, "\"" + attributeName + "\"", null, JavaType.Primitive.String) | ||
); | ||
} else if (CUSTOM_ATTRIBUTES.matches(m)) { | ||
String code; | ||
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})"; | ||
} else { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()})"; | ||
} | ||
return JavaTemplate.builder(code) | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.ContextBuilder") | ||
.build() | ||
.apply(getCursor(), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0), m.getArguments().get(1)); | ||
} else if (PRIVATE_CUSTOM_ATTRIBUTES.matches(m)) { | ||
String code; | ||
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})\n.privateAttributes(#{any(java.lang.String)})"; | ||
} else { | ||
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()}).privateAttributes(#{any(java.lang.String)})"; | ||
} | ||
return JavaTemplate.builder(code) | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6")) | ||
.imports("com.launchdarkly.sdk.ContextBuilder") | ||
.build() | ||
.apply(getCursor(), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0), m.getArguments().get(1), m.getArguments().get(0)); | ||
} | ||
return m; | ||
} | ||
} | ||
); | ||
} | ||
} |
Binary file added
BIN
+7.29 MB
src/main/resources/META-INF/rewrite/classpath/launchdarkly-java-server-sdk-5.10.9.jar
Binary file not shown.
Binary file added
BIN
+7.34 MB
src/main/resources/META-INF/rewrite/classpath/launchdarkly-java-server-sdk-6.3.0.jar
Binary file not shown.
Binary file added
BIN
+7.37 MB
src/main/resources/META-INF/rewrite/classpath/launchdarkly-java-server-sdk-7.1.1.jar
Binary file not shown.
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
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 |
---|---|---|
|
@@ -15,10 +15,10 @@ | |
*/ | ||
package org.openrewrite.launchdarkly; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.config.Environment; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
|
@@ -28,6 +28,8 @@ | |
import java.util.regex.Pattern; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.openrewrite.gradle.Assertions.buildGradle; | ||
import static org.openrewrite.gradle.Assertions.withToolingApi; | ||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.maven.Assertions.pomXml; | ||
|
||
|
@@ -39,13 +41,13 @@ public void defaults(RecipeSpec spec) { | |
.scanRuntimeClasspath("org.openrewrite.launchdarkly") | ||
.build() | ||
.activateRecipes("org.openrewrite.launchdarkly.UpgradeLaunchDarkly6")) | ||
.parser(JavaParser.fromJavaVersion().classpath("launchdarkly-java-server-sdk")); | ||
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "launchdarkly-java-server-sdk-5")); | ||
} | ||
|
||
@Nested | ||
class Dependencies { | ||
@Test | ||
@DocumentExample | ||
@DocumentExample("Maven") | ||
void mavenDependency() { | ||
rewriteRun( | ||
//language=xml | ||
|
@@ -92,44 +94,154 @@ void mavenDependency() { | |
) | ||
); | ||
} | ||
|
||
@Test | ||
@DocumentExample("Gradle") | ||
void gradleDependency() { | ||
rewriteRun( | ||
spec -> spec.beforeRecipe(withToolingApi()), | ||
buildGradle( | ||
""" | ||
plugins { | ||
id "java" | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
implementation "com.launchdarkly:launchdarkly-java-server-sdk:5.10.9" | ||
} | ||
""", | ||
spec -> spec.after(actual -> { | ||
Matcher matcher = Pattern.compile("com\\.launchdarkly:launchdarkly-java-server-sdk:(6\\.\\d+\\.\\d+)").matcher(actual); | ||
assertTrue(matcher.find(), actual); | ||
return """ | ||
plugins { | ||
id "java" | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
implementation "com.launchdarkly:launchdarkly-java-server-sdk:%s" | ||
} | ||
""".formatted(matcher.group(1)); | ||
} | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
|
||
@Nested | ||
class CodeChanges { | ||
@Test | ||
@DocumentExample | ||
@Disabled("Not yet implemented") | ||
void builderUserToContext() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import com.launchdarkly.sdk.LDUser; | ||
import com.launchdarkly.sdk.LDValue; | ||
class A { | ||
void foo() { | ||
LDUser user = new LDUser.Builder("user-key-123abc") | ||
.name("Sandy") | ||
.email("[email protected]") | ||
.custom("groups", LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.build(); | ||
} | ||
} | ||
""", | ||
""" | ||
import com.launchdarkly.sdk.LDContext; | ||
import com.launchdarkly.sdk.LDValue; | ||
class A { | ||
void foo() { | ||
LDContext user = LDContext.builder("user-key-123abc") | ||
.name("Sandy") | ||
.set("email", "[email protected]") | ||
.set("groups", LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.build(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void userToContext() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import com.launchdarkly.sdk.LDUser; | ||
class A { | ||
void foo() { | ||
LDUser user = new LDUser.Builder("user-key-123abc") | ||
.name("Sandy") | ||
.email("[email protected]") | ||
.custom("groups", | ||
LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.build(); | ||
LDUser user = new LDUser("user-key-123abc"); | ||
} | ||
} | ||
""", | ||
""" | ||
import com.launchdarkly.sdk.LDContext; | ||
class A { | ||
void foo() { | ||
LDContext context = LDContext.builder("user-key-123abc") | ||
.name("Sandy") | ||
.set("email", "[email protected]") | ||
.set("groups", | ||
LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.build(); | ||
LDContext user = LDContext.create("user-key-123abc"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void privateAttribute() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import com.launchdarkly.sdk.LDUser; | ||
import com.launchdarkly.sdk.LDValue; | ||
class A { | ||
void foo() { | ||
LDUser user = new LDUser.Builder("user-key-123abc") | ||
.name("Sandy") | ||
.privateEmail("[email protected]") | ||
.privateCustom("groups", LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.build(); | ||
} | ||
} | ||
""", | ||
""" | ||
import com.launchdarkly.sdk.LDContext; | ||
import com.launchdarkly.sdk.LDValue; | ||
class A { | ||
void foo() { | ||
LDContext user = LDContext.builder("user-key-123abc") | ||
.name("Sandy") | ||
.set("email", "[email protected]") | ||
.privateAttributes("email") | ||
.set("groups", LDValue.buildArray().add("Google").add("Microsoft").build()) | ||
.privateAttributes("groups") | ||
.build(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} | ||
} |