Skip to content

Commit

Permalink
Recipes for camel-quarkus
Browse files Browse the repository at this point in the history
Co-authored-by: souvik ghosh <svkcemk@users.noreply.github.com>
  • Loading branch information
JiriOndrusek and svkcemk committed Aug 8, 2023
1 parent 61d346e commit 823f0a9
Show file tree
Hide file tree
Showing 18 changed files with 2,056 additions and 1 deletion.
70 changes: 69 additions & 1 deletion recipes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
<rewrite-maven-plugin.version>4.46.0</rewrite-maven-plugin.version>
<!-- for now, we don't know where to find compatibility information for the Gradle plugin -->
<rewrite-gradle-plugin.version>5.40.6</rewrite-gradle-plugin.version>
<!-- If version from rewrite-recipe-bom is used, error occures during testing ->
NoSuchMethod 'com.fasterxml.jackson.core.util.TextBuffer com.fasterxml.jackson.core.io.IOContext.constructReadConstrainedTextBuffer()'
-->
<rewrite-migrate-java.version>1.16.0</rewrite-migrate-java.version>
<!-- tests-->
<junit.version>5.4.0</junit.version>
<surefire.plugin.version>3.1.0</surefire.plugin.version>
<!-- Http version used by the tests -->
<http.version>4.5.14</http.version>

<lombok.version>1.18.28</lombok.version>
</properties>
Expand Down Expand Up @@ -74,6 +83,12 @@
<artifactId>rewrite-java-17</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
<version>${rewrite-migrate-java.version}</version>
<scope>provided</scope>
</dependency>

<!-- rewrite-maven dependency only necessary for Maven Recipe development -->
<dependency>
Expand Down Expand Up @@ -103,6 +118,13 @@
<scope>provided</scope>
</dependency>

<!-- rewrite-maven dependency is required for pom.xml recipe testing -->
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<scope>provided</scope>
</dependency>

<!-- lombok is optional, but recommended for authoring recipes -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand All @@ -117,6 +139,18 @@
<artifactId>rewrite-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -187,6 +221,40 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
<version>${surefire.plugin.version}</version>
</dependency>
</dependencies>
<configuration>
<systemPropertyVariables>
<maven.repo.local>${settings.localRepository}</maven.repo.local>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
</systemPropertyVariables>
</configuration>
</plugin>

</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.apache.camel.quarkus.update;

import org.openrewrite.ExecutionContext;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* Parent of Camel visitors, skips visit methods in case that there is no camel package imported.
* <p>
* Every method <i>visit*</i> is marked as final and methods <i>doVisit*</i> are used instead.
* </p>
* <p>
* Simple cache for methodMatchers is implemented here. Usage: call <i>MethodMatcher getMethodMatcher(String signature)</i>.
* </p>
*/
public abstract class AbstractCamelQuarkusJavaVisitor extends JavaIsoVisitor<ExecutionContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCamelQuarkusJavaVisitor.class);
//flag that camel package is imported to the file
private boolean camel = false;

private LinkedList<JavaType> implementsList = new LinkedList<>();

//There is no need to initialize all patterns at the class start.
//Map is a cache for created patterns
private static Map<String, MethodMatcher> methodMatchers = new HashMap();

@Override
public final J.Import visitImport(J.Import _import, ExecutionContext context) {
//if there is at least one import of camel class, the camel recipe should be executed
if(_import.getTypeName().contains("org.apache.camel")) {
camel = true;
}

if(!camel) {
//skip recipe if file does not contain camel
return _import;
}

return executeVisitWithCatch(() -> doVisitImport(_import, context), _import, context);
}

@Override
public final J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext context) {
if (classDecl.getImplements() != null && !classDecl.getImplements().isEmpty()) {
implementsList.addAll(classDecl.getImplements().stream().map(i -> i.getType()).collect(Collectors.toList()));
}
return executeVisitWithCatch(() -> doVisitClassDeclaration(classDecl, context), classDecl, context);
}

@Override
public final J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext context) {
if(!camel) {
//skip recipe if file does not contain camel
return fieldAccess;
}

return executeVisitWithCatch(() -> doVisitFieldAccess(fieldAccess, context), fieldAccess, context);
}

@Override
public final J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext context) {
if(!camel) {
//skip recipe if file does not contain camel
return method;
}

return executeVisitWithCatch(() -> doVisitMethodDeclaration(method, context), method, context);
}

@Override
public final J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
if(!camel) {
//skip recipe if file does not contain camel
return method;
}

return executeVisitWithCatch(() -> doVisitMethodInvocation(method, context), method, context);
}

@Override
public final J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext context) {
if(!camel) {
//skip recipe if file does not contain camel
return annotation;
}

return executeVisitWithCatch(() -> doVisitAnnotation(annotation, context), annotation, context);
}


//-------------------------------- internal methods used by children---------------------------------

protected J.Import doVisitImport(J.Import _import, ExecutionContext context) {
return super.visitImport(_import, context);
}

protected J.ClassDeclaration doVisitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext context) {
return super.visitClassDeclaration(classDecl, context);
}

protected J.FieldAccess doVisitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext context) {
return super.visitFieldAccess(fieldAccess, context);
}

protected J.MethodDeclaration doVisitMethodDeclaration(J.MethodDeclaration method, ExecutionContext context) {
return super.visitMethodDeclaration(method, context);
}

protected J.MethodInvocation doVisitMethodInvocation(J.MethodInvocation method, ExecutionContext context) {
return super.visitMethodInvocation(method, context);
}

protected J.Annotation doVisitAnnotation(J.Annotation annotation, ExecutionContext context) {
return super.visitAnnotation(annotation, context);
}

// ------------------------------------------ helper methods -------------------------------------------

protected LinkedList<JavaType> getImplementsList() {
return implementsList;
}

// If the migration fails - do not fail whole migration process, only this one recipe
protected <T extends J> T executeVisitWithCatch(Supplier<T> visitMethod, T origValue, ExecutionContext context) {
try {
return visitMethod.get();
} catch (Exception e) {
LOGGER.warn(String.format("Internal error detected in %s, recipe is skipped.",context.getMessage("org.openrewrite.currentRecipe").getClass().getSimpleName()), e);
return origValue;
}
}

protected MethodMatcher getMethodMatcher(String signature) {
synchronized (methodMatchers) {
MethodMatcher matcher = methodMatchers.get(signature);

if (matcher == null) {
matcher = new MethodMatcher(signature);
methodMatchers.put(signature, matcher);
}

return matcher;
}
}
}
Loading

0 comments on commit 823f0a9

Please sign in to comment.