Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,93 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.dependencies.ChangeDependency;
import org.openrewrite.maven.tree.MavenResolutionResult;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Value
@EqualsAndHashCode(callSuper = false)
public class ChangeJaxbApiDependency extends ScanningRecipe<ChangeJaxbApiDependency.Accumulator> {
private static final ChangeDependency CHANGE = new ChangeDependency(
"javax.xml.bind", "jaxb-api", "jakarta.xml.bind", "jakarta.xml.bind-api", "3.0.x", null, null, null);

String displayName = "Migrate the JAXB API dependency unless Recorder still needs it";
String description = "Migrate JAXB API coordinates while preserving the legacy API needed alongside Jakarta JAXB " +
"by external Arquillian Recorder 1.x binaries. Recorder modules migrated in the same reactor are excluded.";

static class Accumulator {
final Set<String> projects = new HashSet<>();
final ChangeDependency.Accumulator dependency;

Accumulator(ChangeDependency.Accumulator dependency) {
this.dependency = dependency;
}
}

@Override
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator(CHANGE.getInitialValue(ctx));
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
TreeVisitor<?, ExecutionContext> delegate = CHANGE.getScanner(acc.dependency);
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
tree.getMarkers().findFirst(MavenResolutionResult.class)
.ifPresent(model -> acc.projects.add(RetainJaxbApiForArquillianRecorder.projectKey(model)));
if (tree instanceof SourceFile && delegate.isAcceptable((SourceFile) tree, ctx)) {
delegate.visit(tree, ctx);
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
TreeVisitor<?, ExecutionContext> delegate = CHANGE.getVisitor(acc.dependency);
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
MavenResolutionResult model = tree.getMarkers().findFirst(MavenResolutionResult.class).orElse(null);
if (model != null && RetainJaxbApiForArquillianRecorder.hasJakartaApi(model) &&
model.getDependencies().values().stream().flatMap(Collection::stream)
.anyMatch(d -> RetainJaxbApiForArquillianRecorder.externalRecorder(d, acc.projects))) {
return tree;
}
if (tree instanceof SourceFile && delegate.isAcceptable((SourceFile) tree, ctx)) {
return delegate.visitNonNull(tree, ctx);
}
return tree;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Option;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.tree.MavenResolutionResult;

import java.util.Collection;
import java.util.List;

@Value
@EqualsAndHashCode(callSuper = false)
public class DoesNotHaveProvidedApiDependency extends Recipe {
String displayName = "Build does not declare a provided API dependency";

String description = "Find build files without an explicitly provided or compile-only API, " +
"so adding a compile dependency does not promote a container-provided API to runtime scope.";

@Option(displayName = "API coordinates",
description = "Legacy and Jakarta groupId:artifactId coordinates whose provided scope must be preserved.",
example = "javax.enterprise:cdi-api, jakarta.enterprise:jakarta.enterprise.cdi-api")
List<String> coordinates;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
MavenResolutionResult maven = tree.getMarkers().findFirst(MavenResolutionResult.class).orElse(null);
if (maven != null && maven.getDependencies().values().stream().flatMap(Collection::stream)
.anyMatch(d -> d.getDepth() == 0 &&
"provided".equals(maven.getPom().getValue(d.getRequested().getScope())) &&
coordinates.contains(d.getGroupId() + ":" + d.getArtifactId()))) {
return tree;
}
GradleProject gradle = tree.getMarkers().findFirst(GradleProject.class).orElse(null);
if (gradle != null) {
GradleDependencyConfiguration compileOnly = gradle.getConfiguration("compileOnly");
if (compileOnly != null) {
for (String coordinate : coordinates) {
String[] ga = coordinate.split(":", 2);
if (ga.length == 2 && compileOnly.findRequestedDependency(ga[0], ga[1]) != null) {
return tree;
}
}
}
}
return SearchResult.found(tree);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.maven.tree.MavenResolutionResult;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;

@Value
@EqualsAndHashCode(callSuper = false)
public class HasJettyDependency extends ScanningRecipe<AtomicBoolean> {
String displayName = "Build uses Jetty before version 12";

String description = "Mark the source set when a Maven or Gradle module depends on servlet or WebSocket artifacts relocated by the Jetty 12 migration. " +
"This permits updating the Java baseline in parent build files as well as the module using Jetty.";

@Override
public AtomicBoolean getInitialValue(ExecutionContext ctx) {
return new AtomicBoolean();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean usesJetty) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
tree.getMarkers().findFirst(MavenResolutionResult.class).ifPresent(model -> {
if (model.getDependencies().values().stream().flatMap(Collection::stream)
.filter(d -> d.getDepth() == 0)
.anyMatch(d -> isLegacyJetty(d.getGroupId(), d.getArtifactId(), d.getVersion()))) {
usesJetty.set(true);
}
});
tree.getMarkers().findFirst(GradleProject.class).ifPresent(model -> {
if (model.getConfigurations().stream().flatMap(c -> c.getDirectResolved().stream())
.anyMatch(d -> isLegacyJetty(d.getGroupId(), d.getArtifactId(), d.getVersion()))) {
usesJetty.set(true);
}
});
return tree;
}
};
}

private static boolean isLegacyJetty(String groupId, String artifactId, String version) {
if (version == null || !version.matches("(?:8|9|10|11)(?:\\..*)?")) {
return false;
}
return "org.eclipse.jetty".equals(groupId) && Arrays.asList(
"apache-jsp", "jetty-servlets", "jetty-servlet", "jetty-webapp", "jetty-security",
"jetty-proxy", "jetty-annotations").contains(artifactId) ||
"org.eclipse.jetty.websocket".equals(groupId) && Arrays.asList(
"websocket-api", "websocket-server", "websocket-client",
"javax-websocket-server-impl", "javax-websocket-client-impl").contains(artifactId);
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean usesJetty) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
return usesJetty.get() ? SearchResult.found(tree) : tree;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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 lombok.Getter;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.*;

public class InvocationContextGetConstructor extends Recipe {
@Getter
final String displayName = "Complete InvocationContext decorators";
@Getter
final String description = "Delegate getConstructor() when a legacy InvocationContext wrapper delegates getMethod().";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
J.NewClass n = super.visitNewClass(newClass, ctx);
if (n.getBody() != null && n.getClazz() != null &&
TypeUtils.isOfClassType(n.getClazz().getType(), "jakarta.interceptor.InvocationContext")) {
n = n.withBody(complete(n.getBody(), updateCursor(n)));
}
return n;
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);
if (c.getKind() == J.ClassDeclaration.Kind.Type.Class &&
TypeUtils.isAssignableTo("jakarta.interceptor.InvocationContext", c.getType()) &&
c.getType() != null && (c.getType().getSupertype() == null ||
"java.lang.Object".equals(c.getType().getSupertype().getFullyQualifiedName()))) {
c = c.withBody(complete(c.getBody(), updateCursor(c)));
}
return c;
}

private J.Block complete(J.Block body, Cursor parent) {
Expression delegate = null;
for (Statement statement : body.getStatements()) {
if (!(statement instanceof J.MethodDeclaration)) {
continue;
}
J.MethodDeclaration method = (J.MethodDeclaration) statement;
if ("getConstructor".equals(method.getSimpleName()) && method.getMethodType() != null &&
method.getMethodType().getParameterTypes().isEmpty()) {
return body;
}
if ("getMethod".equals(method.getSimpleName()) && method.getMethodType() != null &&
method.getMethodType().getParameterTypes().isEmpty() && method.getBody() != null &&
method.getBody().getStatements().size() == 1 &&
method.getBody().getStatements().get(0) instanceof J.Return) {
Expression returned = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
if (returned instanceof J.MethodInvocation) {
J.MethodInvocation call = (J.MethodInvocation) returned;
Expression select = call.getSelect();
if ("getMethod".equals(call.getSimpleName()) &&
(select instanceof J.Identifier || select instanceof J.FieldAccess) &&
TypeUtils.isAssignableTo("jakarta.interceptor.InvocationContext", select.getType())) {
delegate = select;
}
}
}
}
if (delegate == null) {
return body;
}
maybeAddImport("java.lang.reflect.Constructor");
return JavaTemplate.builder("@Override public Constructor<?> getConstructor() { return #{any(jakarta.interceptor.InvocationContext)}.getConstructor(); }")
.contextSensitive().imports("java.lang.reflect.Constructor")
.javaParser(JavaParser.fromJavaVersion().dependsOn(
"package jakarta.interceptor; public interface InvocationContext { java.lang.reflect.Method getMethod(); java.lang.reflect.Constructor<?> getConstructor(); }"))
.build().apply(new Cursor(parent, body), body.getCoordinates().lastStatement(), delegate);
}
};
}
}
Loading
Loading