Skip to content

Commit

Permalink
jakarta-api-to-quarkus groovy rules
Browse files Browse the repository at this point in the history
  • Loading branch information
m-brophy committed Jan 12, 2024
1 parent 8705fa8 commit 69e5606
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package quarkus.javaee

import org.jboss.windup.ast.java.data.TypeReferenceLocation
import org.jboss.windup.config.GraphRewrite
import org.jboss.windup.config.Variables
import org.jboss.windup.config.metadata.TechnologyReference
import org.jboss.windup.config.operation.Iteration
import org.jboss.windup.config.operation.iteration.AbstractIterationOperation
import org.jboss.windup.graph.model.FileLocationModel
import org.jboss.windup.graph.model.FileReferenceModel
import org.jboss.windup.graph.model.ProjectModel
import org.jboss.windup.graph.model.WindupVertexFrame
import org.jboss.windup.graph.model.resource.FileModel
import org.jboss.windup.project.condition.Artifact
import org.jboss.windup.project.condition.Project
import org.jboss.windup.reporting.category.IssueCategory
import org.jboss.windup.reporting.category.IssueCategoryRegistry
import org.jboss.windup.reporting.config.Hint
import org.jboss.windup.reporting.config.Link
import org.jboss.windup.rules.apps.java.condition.JavaClass
import org.ocpsoft.rewrite.config.And
import org.ocpsoft.rewrite.context.EvaluationContext

import java.util.stream.Collectors
import java.util.stream.StreamSupport

final IssueCategory mandatoryIssueCategory = new IssueCategoryRegistry().getByID(IssueCategoryRegistry.MANDATORY)
final Link guideLink = Link.to("Quarkus - Guides", "https://quarkus.io/guides/resteasy-reactive")

static boolean matchesProject(GraphRewrite event, FileLocationModel payload) {
final Iterable<? extends WindupVertexFrame> previouslyFound = Optional.ofNullable(Variables.instance(event).findVariable("discard")).orElse(Collections.emptySet())
final Set<ProjectModel> projectModels = StreamSupport.stream(previouslyFound.spliterator(), false)
.map {
if (it instanceof FileReferenceModel) return ((FileReferenceModel) it).getFile().getProjectModel()
else if (it instanceof FileModel) return ((FileModel) it).getProjectModel()
else return null
}
.collect (Collectors.toSet())
final boolean matchesProject = projectModels.isEmpty() || projectModels.stream().anyMatch{payload.getFile().belongsToProject(it)}
return matchesProject
}

ruleSet("javaee-api-to-quarkus-groovy")
.addSourceTechnology(new TechnologyReference("jakarta", null))
.addTargetTechnology(new TechnologyReference("quarkus", null))
.addRule()
.when(
And.all(
JavaClass.references("jakarta.ws.rs.{*}").at(TypeReferenceLocation.IMPORT).as("discard"),
Project.dependsOnArtifact(Artifact.withGroupId("jakarta.platform").andArtifactId("jakarta.jakartaee-api")).as("dependency")
)
)
.perform(
Iteration.over("dependency").perform(
new AbstractIterationOperation<FileLocationModel>() {
void perform(GraphRewrite event, EvaluationContext context, FileLocationModel payload) {
if (matchesProject(event, payload)) {
((Hint) Hint.titled("Replace jakarta JAX-RS dependency")
.withText("""
At least one Java class importing from the `jakarta.ws.rs` package has been found so the dependency `jakarta.platform:jakarta.jakartaee-api` has to be replaced with `io.quarkus:quarkus-resteasy-reactive` artifact.
""")
.withIssueCategory(mandatoryIssueCategory)
.with(guideLink)
.withEffort(1)
).performParameterized(event, context, payload)
}
}
}
)
.endIteration()
)
.withId("jakarta-api-to-quarkus-groovy-00000")
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static boolean matchesProject(GraphRewrite event, FileLocationModel payload) {
return matchesProject
}

ruleSet("javaee-api-to-quarkus-groovy")
ruleSet("jakarta-api-to-quarkus-groovy")
.addSourceTechnology(new TechnologyReference("java-ee", null))
.addTargetTechnology(new TechnologyReference("quarkus", null))
.addRule()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
JBoss, Home of Professional Open Source
Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @authors tag. See the copyright.txt in the
distribution for a full listing of individual contributors.
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
http://www.apache.org/licenses/LICENSE-2.0
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jboss.eap.quickstarts</groupId>
<artifactId>quickstart-parent</artifactId>
<!--
Maintain separation between the artifact id and the version to help prevent
merge conflicts between commits changing the GA and those changing the V.
-->
<version>7.2.0.GA</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>helloworld-rs</artifactId>
<packaging>war</packaging>
<name>Quickstart: helloworld-rs</name>
<description>A simple Hello World project that uses JAX-RS</description>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>


<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>9.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* 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
* http://www.apache.org/licenses/LICENSE-2.0
* 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.jboss.as.quickstarts.rshelloworld;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

/**
* A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS
* is enabled
*
* @author [email protected]
*
*/

@Path("/")
public class HelloWorld {
@GET
@Path("/json")
@Produces({ "application/json" })
public String getHelloWorldJSON() {
return "{\"result\":\"" + helloService.createHelloMessage("World") + "\"}";
}

@GET
@Path("/xml")
@Produces({ "application/xml" })
public String getHelloWorldXML() {
return "<xml><result>" + helloService.createHelloMessage("World") + "</result></xml>";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<ruletest id="jakarta-api-to-quarkus-tests"
xmlns="http://windup.jboss.org/schema/jboss-ruleset"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">
<testDataPath>data/*</testDataPath>
<rulePath>../jakarta-api-to-quarkus.windup.groovy</rulePath>
<ruleset>
<rules>
<rule id="jakarta-api-to-quarkus-groovy-00000-test">
<when>
<not>
<iterable-filter size="1">
<hint-exists message="At least one Java class importing from the `jakarta.ws.rs` package has been found"/>
</iterable-filter>
</not>
</when>
<perform>
<fail message="[jakarta-api-to-quarkus-groovy-00000] javax.ws.rs transitive dependency hint was not found!" />
</perform>
</rule>
</rules>
</ruleset>
</ruletest>

0 comments on commit 69e5606

Please sign in to comment.