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,101 @@
/*
* 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.TreeVisitor;
import org.openrewrite.maven.AddDependencyVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.tree.Xml;

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

@Value
@EqualsAndHashCode(callSuper = false)
public class RetainJaxbApiForArquillianRecorder extends ScanningRecipe<Set<String>> {
private static final VersionComparator JAKARTA_API = Objects.requireNonNull(Semver.validate("[3,)", null).getValue());
String displayName = "Retain the JAXB 2 API for Arquillian Recorder 1.x";
String description = "Retain the legacy JAXB API in Maven modules using Arquillian Recorder 1.x binaries, " +
"which still reference javax.xml.bind classes after the application's Jakarta migration.";

@Override
public Set<String> getInitialValue(ExecutionContext ctx) {
return new HashSet<>();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Set<String> projects) {
return new MavenIsoVisitor<ExecutionContext>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
projects.add(projectKey(getResolutionResult()));
return document;
}
};
}

static String projectKey(MavenResolutionResult model) {
return model.getPom().getGroupId() + ":" + model.getPom().getArtifactId() + ":" + model.getPom().getVersion();
}

static boolean hasJakartaApi(MavenResolutionResult model) {
return model.getDependencies().values().stream().flatMap(Collection::stream)
.anyMatch(d -> "jakarta.xml.bind".equals(d.getGroupId()) &&
"jakarta.xml.bind-api".equals(d.getArtifactId()) && JAKARTA_API.isValid(null, d.getVersion()));
}

static boolean externalRecorder(ResolvedDependency dependency, Set<String> projects) {
return "org.arquillian.extension".equals(dependency.getGroupId()) &&
dependency.getArtifactId().startsWith("arquillian-recorder-") &&
dependency.getVersion().startsWith("1.") &&
!projects.contains(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion());
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Set<String> projects) {
return new MavenIsoVisitor<ExecutionContext>() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
if (!hasJakartaApi(getResolutionResult())) {
return document;
}
for (Scope scope : new Scope[]{Scope.Compile, Scope.Runtime, Scope.Provided, Scope.Test}) {
for (ResolvedDependency dependency : getResolutionResult().getDependencies()
.getOrDefault(scope, Collections.emptyList())) {
if (externalRecorder(dependency, projects)) {
return (Xml.Document) new AddDependencyVisitor("javax.xml.bind", "jaxb-api", "2.3.x", null,
scope == Scope.Test ? "test" : "runtime", null, null, null, null, null)
.visitNonNull(document, ctx);
}
}
}
return document;
}
};
}
}
8 changes: 2 additions & 6 deletions src/main/resources/META-INF/rewrite/jakarta-ee-9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1008,17 +1008,13 @@ preconditions:
- org.openrewrite.Singleton
recipeList:
- org.openrewrite.java.migrate.javax.MigrateOneGfwJaxbDependencies
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: javax.xml.bind
oldArtifactId: jaxb-api
newGroupId: jakarta.xml.bind
newArtifactId: jakarta.xml.bind-api
newVersion: 3.0.x
- org.openrewrite.java.migrate.jakarta.ChangeJaxbApiDependency
- org.openrewrite.java.migrate.jakarta.RetainJaxbApiForJackson
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: jakarta.xml.bind
artifactId: jakarta.xml.bind-api
newVersion: 3.0.x
- org.openrewrite.java.migrate.jakarta.RetainJaxbApiForArquillianRecorder
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.sun.xml.bind
oldArtifactId: jaxb-impl
Expand Down
Loading