Skip to content

Commit

Permalink
Create a dedicated recipe to replace ServerName calls (openrewrite#5)
Browse files Browse the repository at this point in the history
* Create a dedicated recipe to replace ServerName calls

* Remove call to ShortenFullyQualifiedTypeReferences
  • Loading branch information
timtebeek authored Aug 16, 2023
1 parent b850d13 commit 36b881d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 219 deletions.
202 changes: 0 additions & 202 deletions src/main/java/org/openrewrite/java/liberty/ChangeMethodInvocation.java

This file was deleted.

57 changes: 57 additions & 0 deletions src/main/java/org/openrewrite/java/liberty/ServerName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.java.liberty;

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

public class ServerName extends Recipe {

@Override
public String getDisplayName() {
return "Use `getProperty(\"wlp.server.name\")`";
}

@Override
public String getDescription() {
return "`ServerName.getDisplayName()` is not available in Liberty.";
}

private static final String SERVER_NAME = "com.ibm.websphere.runtime.ServerName";
private static final MethodMatcher getDisplayName = new MethodMatcher(SERVER_NAME + " getDisplayName()");
private static final MethodMatcher getFullName = new MethodMatcher(SERVER_NAME + " getFullName()");

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
if (getDisplayName.matches(elem) || getFullName.matches(elem)) {
maybeRemoveImport(SERVER_NAME);
return JavaTemplate.builder("System.getProperty(\"wlp.server.name\")").build()
.apply(getCursor(), elem.getCoordinates().replace());
}
return super.visitMethodInvocation(elem, ctx);
}
};
}
}

14 changes: 0 additions & 14 deletions src/main/resources/META-INF/rewrite/was-to-liberty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ recipeList:
- org.openrewrite.xml.liberty.WebDDNamespaceRule
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.liberty.ServerName
displayName: Use `getProperty("wlp.server.name")`
description: The `getDisplayName()` is not available in Liberty.
recipeList:
- org.openrewrite.java.liberty.ChangeMethodInvocation:
methodPattern: com.ibm.websphere.runtime.ServerName getDisplayName()
newMethodPattern: java.lang.System getProperty("wlp.server.name")
performStaticCall: true
- org.openrewrite.java.liberty.ChangeMethodInvocation:
methodPattern: com.ibm.websphere.runtime.ServerName getFullName()
newMethodPattern: java.lang.System getProperty("wlp.server.name")
performStaticCall: true
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.liberty.WebSphereUnavailableSSOTokenMethod
displayName: Use `getSSOCookieFromSSOToken`
description: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
package org.openrewrite.java.liberty;

import org.junit.jupiter.api.Test;
import org.openrewrite.config.Environment;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class ServerNameTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ServerName());
}

//language=java
String serverNameClass = """
package com.ibm.websphere.runtime;
Expand All @@ -44,7 +49,6 @@ public static String getFullName() {
@Test
void replaceGetFullNameTest() {
rewriteRun(
spec -> spec.recipe(Environment.builder().scanRuntimeClasspath("org.openrewrite.java.liberty").build().activateRecipes("org.openrewrite.java.liberty.ServerName")),
java(serverNameClass),
//language=java
java(
Expand Down Expand Up @@ -79,7 +83,6 @@ public void doX() {
@Test
void replaceGetDisplayNameTest() {
rewriteRun(
spec -> spec.recipe(Environment.builder().scanRuntimeClasspath("org.openrewrite.java.liberty").build().activateRecipes("org.openrewrite.java.liberty.ServerName")),
java(serverNameClass),
//language=java
java(
Expand Down

0 comments on commit 36b881d

Please sign in to comment.