-
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.
LaunchDarkly Java SDK 6.x & 7.x Migration (#12)
* Migrate `LDUser` to `LDContext` * Flatten private attributes using varargs * Cleanup based on PR review * Add 7.x migration * Inline isApplicableMethod, as the matcher checks null & instanceof * Collapse dependency recipes to have less at top level Unlikely folks would run these separately, as it's only one dependency; if they would want to they can run `UpgradeDependencyVersion` separately. * Add language hints for code highlighting in text blocks * Drop unused launchdarkly-java-server-sdk-7.1.1.jar for now --------- Co-authored-by: Tim te Beek <[email protected]>
- Loading branch information
1 parent
d9d9a06
commit 7ec7748
Showing
11 changed files
with
649 additions
and
110 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
238 changes: 238 additions & 0 deletions
238
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,238 @@ | ||
/* | ||
* Copyright 2023 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 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.Expression; | ||
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.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
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 from `LDUser` and `LDUser.Builder` to `LDContext` and `ContextBuilder`."; | ||
} | ||
|
||
@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())) { | ||
doAfterVisit(new UseVarargsForPrivateAttributes()); | ||
|
||
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)) { | ||
doAfterVisit(new UseVarargsForPrivateAttributes()); | ||
|
||
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; | ||
} | ||
} | ||
); | ||
} | ||
|
||
private static class UseVarargsForPrivateAttributes extends JavaIsoVisitor<ExecutionContext> { | ||
private static final MethodMatcher CONTEXT_BUILDER_MATCHER = new MethodMatcher("com.launchdarkly.sdk.ContextBuilder *(..)"); | ||
private static final MethodMatcher PRIVATE_ATTRIBUTES_STRING_VARARGS_MATCHER = new MethodMatcher("com.launchdarkly.sdk.ContextBuilder privateAttributes(java.lang.String...)"); | ||
private static final MethodMatcher USER_BUILDER_BUILD_MATCHER = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder build()"); | ||
private static final MethodMatcher CONTEXT_BUILDER_BUILD_MATCHER = new MethodMatcher("com.launchdarkly.sdk.ContextBuilder build()"); | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation m = super.visitMethodInvocation(method, ctx); | ||
if (!(USER_BUILDER_BUILD_MATCHER.matches(m) || CONTEXT_BUILDER_BUILD_MATCHER.matches(m))) { | ||
return m; | ||
} | ||
|
||
List<J.MethodInvocation> chain = computeChain(m); | ||
return unfold(m, chain); | ||
} | ||
|
||
private List<J.MethodInvocation> computeChain(J.MethodInvocation build) { | ||
List<J.MethodInvocation> chain = new ArrayList<>(); | ||
if (!(build.getSelect() instanceof J.MethodInvocation)) { | ||
return chain; | ||
} | ||
|
||
List<Expression> attributes = new ArrayList<>(); | ||
Expression select = build.getSelect(); | ||
int privateAttributesInvocations = 0; | ||
int lastPrivateAttributesIdx = -1; | ||
while (CONTEXT_BUILDER_MATCHER.matches(select)) { | ||
J.MethodInvocation m = (J.MethodInvocation) select; | ||
if (PRIVATE_ATTRIBUTES_STRING_VARARGS_MATCHER.matches(m)) { | ||
if (lastPrivateAttributesIdx == -1 && CONTEXT_BUILDER_MATCHER.matches(m.getSelect())) { | ||
lastPrivateAttributesIdx = chain.size(); | ||
chain.add(m); | ||
} | ||
attributes.addAll(0, m.getArguments()); | ||
privateAttributesInvocations++; | ||
} else { | ||
chain.add(m); | ||
} | ||
select = m.getSelect(); | ||
} | ||
if (privateAttributesInvocations <= 1) { | ||
return Collections.emptyList(); | ||
} | ||
for (int i = 1; i < attributes.size(); i++) { | ||
attributes.set(i, attributes.get(i).withPrefix(Space.SINGLE_SPACE)); | ||
} | ||
chain.set(lastPrivateAttributesIdx, chain.get(lastPrivateAttributesIdx).withArguments(attributes)); | ||
return chain; | ||
} | ||
|
||
private J.MethodInvocation unfold(J.MethodInvocation build, List<J.MethodInvocation> chain) { | ||
if (chain.isEmpty()) { | ||
return build; | ||
} | ||
Collections.reverse(chain); | ||
|
||
J.MethodInvocation select = chain.get(0); | ||
for (int i = 1; i < chain.size(); i++) { | ||
select = chain.get(i) | ||
.withId(Tree.randomId()) | ||
.withSelect(select); | ||
} | ||
return build.withSelect(select); | ||
} | ||
} | ||
} |
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.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# | ||
# Copyright 2023 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. | ||
# | ||
|
||
--- | ||
type: specs.openrewrite.org/v1beta/recipe | ||
name: org.openrewrite.launchdarkly.UpgradeLaunchDarkly7 | ||
displayName: Migrate to LaunchDarkly 7.x | ||
description: This recipe will apply changes commonly needed when migrating to LaunchDarkly 7.x. | ||
recipeList: | ||
- org.openrewrite.launchdarkly.UpgradeLaunchDarkly6 | ||
# https://docs.launchdarkly.com/sdk/server-side/java/migration-6-to-7 | ||
- org.openrewrite.java.dependencies.UpgradeDependencyVersion: | ||
groupId: com.launchdarkly | ||
artifactId: launchdarkly-java-server-sdk | ||
newVersion: 7.x |
Oops, something went wrong.