Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemovalsServletJakarta10 Recipe #338

Merged
merged 27 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cbc7c6d
RemovalsServletJakarta10 Recipe
AnuRam123 Nov 3, 2023
77a905e
push post DeleteMethodArgument fix
AnuRam123 Nov 3, 2023
225a329
cleanup RemovalsServletJakarta10Test.java
AnuRam123 Nov 4, 2023
320c28c
Polish & enable exception test
timtebeek Nov 6, 2023
27a09e8
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Nov 6, 2023
5a0c335
Better expectations
timtebeek Nov 6, 2023
3d6ea26
Remove unused imports
timtebeek Nov 6, 2023
8e39c10
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Nov 7, 2023
dfd8c5e
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Nov 15, 2023
8b86e13
Merge branch 'main' into jakarta_ee_recipe_migration
ranuradh Nov 16, 2023
b0f060d
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Nov 20, 2023
f86caa0
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Nov 20, 2023
d3d7cb1
Restore jakarta-ee-10.yml
timtebeek Nov 20, 2023
6b29a95
remove annotations and rename class file
AnuRam123 Nov 27, 2023
2701895
Update src/main/java/org/openrewrite/java/migrate/jakarta/UpdateGetRe…
timtebeek Nov 28, 2023
7913b64
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Dec 11, 2023
c609d5f
Merge branch 'main' into jakarta_ee_recipe_migration
ranuradh Jan 9, 2024
a8909db
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Jan 17, 2024
4ed019d
Merge branch 'main' into jakarta_ee_recipe_migration
cjobinabo Jan 19, 2024
91774c2
should resolve tests
cjobinabo Jan 19, 2024
43ea9a2
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Jan 22, 2024
56d4d9d
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Jan 28, 2024
20a49b2
Merge branch 'main' into jakarta_ee_recipe_migration
timtebeek Jan 30, 2024
6739ed8
Remove Faces recipes that were split off
timtebeek Jan 30, 2024
8d733ff
Remove parameters to UpdateGetRealPath & add test
timtebeek Jan 30, 2024
10d1540
Remove optional `ignoreDefinition: true`
timtebeek Jan 30, 2024
9770410
Add missing yaml recipe type
timtebeek Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.migrate.jakarta;

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

public class UpdateGetRealPath extends Recipe {
@Override
public String getDisplayName() {
return "Updates `getRealPath()` to call `getContext()` followed by `getRealPath()`";
}

@Override
public String getDescription() {
return "Updates `getRealPath()` for `jakarta.servlet.ServletRequest` and `jakarta.servlet.ServletRequestWrapper` to use `ServletContext.getRealPath(String)`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
private final MethodMatcher METHOD_PATTERN = new MethodMatcher("jakarta.servlet.ServletRequest* getRealPath(String)", false);

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ec) {
if (METHOD_PATTERN.matches(method)) {
return JavaTemplate.builder("#{any()}.getServletContext().getRealPath(#{any(String)})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ec, "jakarta.servlet-api-6.0.0"))
.build()
.apply(updateCursor(method),
method.getCoordinates().replace(),
method.getSelect(),
method.getArguments().get(0));
}
return method;
}
};
}
}
88 changes: 81 additions & 7 deletions src/main/resources/META-INF/rewrite/jakarta-ee-10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ recipeList:
- org.openrewrite.java.migrate.jakarta.RemovedSOAPElementFactory
- org.openrewrite.java.migrate.jakarta.WsWsocServerContainerDeprecation
- org.openrewrite.java.migrate.jakarta.ServletCookieBehaviorChangeRFC6265
- org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10
- org.openrewrite.java.migrate.jakarta.DeprecatedCDIAPIsRemoved40
- org.openrewrite.java.migrate.BeanDiscovery
- org.openrewrite.java.migrate.jakarta.BeanValidationMessages
Expand All @@ -53,7 +54,6 @@ recipeList:
methodPattern: jakarta.servlet.SessionCookieConfig getComment()
- org.openrewrite.java.migrate.RemoveMethodInvocation:
methodPattern: jakarta.servlet.SessionCookieConfig setComment(String)

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.WsWsocServerContainerDeprecation
Expand All @@ -63,11 +63,9 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.ibm.websphere.wsoc.WsWsocServerContainer doUpgrade(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.ibm.websphere.wsoc.ServerEndpointConfig, java.util.Map)
newMethodName: upgradeHttpToWebSocket
ignoreDefinition: true
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: com.ibm.websphere.wsoc.WsWsocServerContainer
newFullyQualifiedTypeName: jakarta.websocket.server.ServerContainer
ignoreDefinition: true
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.RemovedIsParmetersProvidedMethod
Expand All @@ -79,7 +77,6 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.el.MethodExpression isParmetersProvided()
newMethodName: isParametersProvided
ignoreDefinition: true
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.RemovedSOAPElementFactory
Expand All @@ -91,15 +88,12 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.xml.soap.SOAPElementFactory create(String,..)
newMethodName: createElement
ignoreDefinition: true
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.xml.soap.SOAPElementFactory create(jakarta.xml.soap.Name)
newMethodName: createElement
ignoreDefinition: true
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: jakarta.xml.soap.SOAPElementFactory
newFullyQualifiedTypeName: jakarta.xml.soap.SOAPFactory
ignoreDefinition: true
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.JavaxBeansXmlToJakartaBeansXml
Expand Down Expand Up @@ -127,6 +121,86 @@ recipeList:
newValue: https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10
displayName: Replace deprecated Jakarta Servlet methods and classes
description: >
This recipe replaces the classes and methods deprecated in Jakarta Servlet 6.0
recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.http.HttpServletRequest
newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletRequest
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.http.HttpServletRequestWrapper
newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletRequestWrapper
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.http.HttpServletResponse
newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletResponse
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.http.HttpServletResponseWrapper
newFullyQualifiedTypeName: jakarta.servlet.http.HttpServletResponseWrapper
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.http.HttpSession
newFullyQualifiedTypeName: jakarta.servlet.http.HttpSession
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.ServletContext
newFullyQualifiedTypeName: jakarta.servlet.ServletContext
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.servlet.UnavailableException
newFullyQualifiedTypeName: jakarta.servlet.UnavailableException
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletRequest isRequestedSessionIdFromUrl()
newMethodName: isRequestedSessionIdFromURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletRequestWrapper isRequestedSessionIdFromUrl()
newMethodName: isRequestedSessionIdFromURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletResponse encodeUrl(String)
newMethodName: encodeURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletResponseWrapper encodeUrl(String)
newMethodName: encodeURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletResponse encodeRedirectUrl(String)
newMethodName: encodeRedirectURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpServletResponseWrapper encodeRedirectUrl(String)"
newMethodName: encodeRedirectURL
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpSession getValue(String)
newMethodName: getAttribute
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpSession getValueNames()
newMethodName: getAttributeNames
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpSession putValue(String, Object)
newMethodName: setAttribute
- org.openrewrite.java.ChangeMethodName:
methodPattern: jakarta.servlet.http.HttpSession removeValue(String)
newMethodName: removeAttribute
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: jakarta.servlet.http.HttpServletResponse setStatus(int, String)
argumentIndex: 1
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: jakarta.servlet.http.HttpServletResponseWrapper setStatus(int, String)
argumentIndex: 1
- org.openrewrite.java.ReorderMethodArguments:
methodPattern: jakarta.servlet.ServletContext log(Exception, String)
newParameterNames: [ ex, str ]
oldParameterNames: [ str, ex ]
matchOverrides: true
- org.openrewrite.java.migrate.jakarta.UpdateGetRealPath
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: jakarta.servlet.UnavailableException <constructor>(jakarta.servlet.Servlet, String)
argumentIndex: 0
- org.openrewrite.java.DeleteMethodArgument:
methodPattern: jakarta.servlet.UnavailableException <constructor>(int, jakarta.servlet.Servlet, String)
argumentIndex: 1
- org.openrewrite.java.ReorderMethodArguments:
methodPattern: jakarta.servlet.UnavailableException <constructor>(int, String)
oldParameterNames: [ in, str ]
newParameterNames: [ str, in ]
matchOverrides: true
---
name: org.openrewrite.java.migrate.jakarta.DeprecatedCDIAPIsRemoved40
displayName: Remove deprecated API's not supported in CDI4.0
description: >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* 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.migrate.jakarta;

import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class RemovalsServletJakarta10Test implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(),
"javax.servlet-api-4.0.0",
"jakarta.servlet-api-5.0.0",
"jakarta.servlet-api-6.0.0"))
.recipeFromResource("/META-INF/rewrite/jakarta-ee-10.yml", "org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10");
}

@Test
void servletReplacements() {
rewriteRun(
//language=java
java(
"""
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromUrl();

res.encodeUrl("");
res.encodeRedirectUrl("");

res.setStatus(0, "");

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromUrl();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeUrl("");
resWrapper.encodeRedirectUrl("");

resWrapper.setStatus(0, "");

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getValue("");
httpSession.getValueNames();
httpSession.putValue("", null);
httpSession.removeValue("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log(null, "");
}
}
""",
"""
import java.io.IOException;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SingleThreadModel;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionContext;
import jakarta.servlet.http.HttpUtils;

class TestJakarta extends HttpServlet implements SingleThreadModel {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.isRequestedSessionIdFromURL();

res.encodeURL("");
res.encodeRedirectURL("");

res.setStatus(0);

res.setStatus(0);

HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
reqWrapper.isRequestedSessionIdFromURL();

HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);

resWrapper.encodeURL("");
resWrapper.encodeRedirectURL("");

resWrapper.setStatus(0);

HttpSession httpSession = req.getSession();
httpSession.getSessionContext();
httpSession.getAttribute("");
httpSession.getAttributeNames();
httpSession.setAttribute("", null);
httpSession.removeAttribute("");

ServletContext servletContext = getServletContext();

servletContext.getServlet("");
servletContext.getServlets();
servletContext.getServletNames();

servletContext.log("", null);
}
}
"""
)
);
}

@Test
void unavailableException() {
rewriteRun(
//language=java
java(
"""
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Test {
void doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
jakarta.servlet.Servlet servlet ;
UnavailableException unavailableEx1 = new UnavailableException(0, null, "x");
UnavailableException unavailableEx2 = new UnavailableException(0, servlet, "x");
UnavailableException unavailableEx3 = new UnavailableException(servlet, "x");
}
}
""",
"""
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Test {
void doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
jakarta.servlet.Servlet servlet ;
UnavailableException unavailableEx1 = new UnavailableException("x", 0);
UnavailableException unavailableEx2 = new UnavailableException("x", 0);
UnavailableException unavailableEx3 = new UnavailableException( "x");
}
}
"""
)
);
}
}
Loading
Loading