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

Revamp/integrate rewrite commons extract jax rs #1048

Open
wants to merge 19 commits into
base: revamp/integrate-rewrite-commons
Choose a base branch
from
Open
240 changes: 240 additions & 0 deletions components/jaxrs-recipes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2021 - 2022 the original author or authors.
~
~ 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
~
~ https://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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.sbm</groupId>
<artifactId>jaxrs-recipes</artifactId>
<version>0.15.2-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jaxb-maven-plugin.version>2.5.0</jaxb-maven-plugin.version>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<java.version>17</java.version>
<spring-shell.version>2.1.14</spring-shell.version>
<openrewrite.version>8.29.0</openrewrite.version>
<openrewrite.spring.version>4.32.0</openrewrite.spring.version>
<rewrite-migrate-java.version>1.17.0</rewrite-migrate-java.version>
<spring-boot.version>3.3.1</spring-boot.version>
<progressbar.version>0.10.0</progressbar.version>
<testcontainers.version>1.19.1</testcontainers.version>
<maven-invoker.version>3.2.0</maven-invoker.version>
<shrinkwrap.resolvers.version>3.1.4</shrinkwrap.resolvers.version>
<lombok.version>1.18.30</lombok.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.csvParser.outputEncoding>UTF-8</project.csvParser.outputEncoding>
<generated-sources.dir>src/generated/java</generated-sources.dir>
<snakeyaml.version>1.33</snakeyaml.version>
<spring-asciidoctor-backends.version>0.0.7</spring-asciidoctor-backends.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recipe-bom</artifactId>
<version>2.14.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.20.0</version>
</dependency>

<!-- Required or dependencies, previously transitively -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.rewrite</groupId>
<artifactId>spring-rewrite-commons-plugin-invoker-polyglot</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.sbm</groupId>
<artifactId>sbm-core</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
</exclusion>
<exclusion>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.sbm</groupId>
<artifactId>sbm-support-jee</artifactId>
<version>0.15.2-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.sbm</groupId>
<artifactId>sbm-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- TODO: why? remove ? -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>

<!-- TODO: remove as these are (probably) only required in sbm-spport-jee -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>

<dependency>
<groupId>org.springframework.sbm</groupId>
<artifactId>test-helper</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.sbm</groupId>-->
<!-- <artifactId>sbm-openrewrite</artifactId>-->
<!-- <type>test-jar</type>-->
<!-- <version>${project.version}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.sbm</groupId>
<artifactId>recipe-test-support</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.sbm</groupId>
<artifactId>sbm-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.sbm</groupId>
<artifactId>sbm-core</artifactId>
<version>0.15.2-SNAPSHOT</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
<version>8.29.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-static-analysis</artifactId>
<version>1.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-test</artifactId>
<version>8.29.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<excludedGroups>integration</excludedGroups>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<groups>integration</groups>
<includes>
<include>**/*Test.java</include>
</includes>
<trimStackTrace>false</trimStackTrace>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* 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
*
* https://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 example.recipe;


import freemarker.template.Configuration;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.maven.MavenSettings;
import org.openrewrite.maven.cache.LocalMavenArtifactCache;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.rewrite.parser.JavaParserBuilder;
import org.springframework.rewrite.parser.maven.MavenSettingsInitializer;
import org.springframework.rewrite.parser.maven.RewriteMavenArtifactDownloader;
import org.springframework.rewrite.resource.*;
import org.springframework.rewrite.scopes.ProjectMetadata;
import org.springframework.sbm.build.impl.MavenBuildFileRefactoringFactory;
import org.springframework.sbm.build.impl.RewriteMavenParser;
import org.springframework.sbm.build.resource.BuildFileResourceWrapper;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextFactory;
import org.springframework.sbm.engine.recipe.RewriteRecipeLoader;
import org.springframework.sbm.java.JavaSourceProjectResourceWrapper;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactory;
import org.springframework.sbm.java.refactoring.JavaRefactoringFactoryImpl;
import org.springframework.sbm.java.util.BasePackageCalculator;
import org.springframework.sbm.jee.jaxrs.MigrateJaxRsRecipe;
import org.springframework.sbm.jee.jaxrs.actions.ConvertJaxRsAnnotations;
import org.springframework.sbm.project.resource.ProjectResourceSetHolder;
import org.springframework.sbm.project.resource.ProjectResourceWrapper;
import org.springframework.sbm.project.resource.ProjectResourceWrapperRegistry;
import org.springframework.sbm.project.resource.SbmApplicationProperties;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* @author Fabian Krüger
*/
public class SbmAdapterRecipe extends ScanningRecipe<List<SourceFile>> {

private ProjectResourceSetFactory projectResourceSetFactory;
private ProjectContextFactory projectContextFactory;

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "";
}

public SbmAdapterRecipe() {

}

@Override
public List<SourceFile> getInitialValue(ExecutionContext ctx) {
return new ArrayList<>();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(List<SourceFile> acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
if(tree instanceof SourceFile) {
acc.add((SourceFile) tree);
}
return super.visit(tree, executionContext);
}
};
}

@Override
public Collection<? extends SourceFile> generate(List<SourceFile> generate, ExecutionContext executionContext) {
// create the required classes
initBeans();

// transform nodes to SourceFiles
List<SourceFile> sourceFiles = generate;

// FIXME: base dir calculation is fake
Path baseDir = Path.of(".").resolve("testcode/jee/jaxrs/bootify-jaxrs/given").toAbsolutePath().normalize(); //executionContext.getMessage("base.dir");

// Create the SBM resource set abstraction
ProjectResourceSet projectResourceSet = projectResourceSetFactory.create(baseDir, sourceFiles);
// Create the SBM ProjectContext
ProjectContext pc = projectContextFactory.createProjectContext(baseDir, projectResourceSet);

// Execute the SBM Action = the JAXRS Recipe
// new ConvertJaxRsAnnotations().apply(pc);

RewriteRecipeLoader recipeLoader = new RewriteRecipeLoader();
new MigrateJaxRsRecipe().jaxRs(recipeLoader).apply(pc);

List<? extends SourceFile> list = pc.getProjectResources().stream()
.map(pr -> pr.getSourceFile())
.toList();

return list;
}

private void initBeans() {
// ExecutionContext is not retrieved from OpenRewrite here
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("org.springframework.freemarker", "org.springframework.sbm", "org.springframework.rewrite");
ctx.register(Configuration.class);
this.projectContextFactory = ctx.getBean(ProjectContextFactory.class);
this.projectResourceSetFactory = ctx.getBean(ProjectResourceSetFactory.class);
}

}
Loading
Loading