From 9221d35ae9f00af1f2e2702fac24c1deaf3305d8 Mon Sep 17 00:00:00 2001
From: Jonathan Schneider <jkschneider@gmail.com>
Date: Fri, 3 Mar 2023 16:36:32 -0500
Subject: [PATCH] Parse java files in src/main/resources with JavaParser

---
 .../maven/AbstractRewriteMojo.java            |  3 --
 .../maven/ConfigurableRewriteMojo.java        |  1 +
 .../maven/MavenMojoProjectParser.java         | 10 ++++---
 .../org/openrewrite/maven/ResourceParser.java | 29 +++++++++++++++----
 4 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
index ed366ffb..68d72d67 100644
--- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
+++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
@@ -167,10 +167,7 @@ protected Path repositoryRoot() {
         return maybeBaseDir;
     }
 
-
-
     private void collectBasePaths(MavenProject project, Set<Path> paths, Path localRepository) {
-
         Path baseDir = project.getBasedir() == null ? null : project.getBasedir().toPath().normalize();
         if (baseDir == null || baseDir.startsWith(localRepository) || paths.contains(baseDir)) {
             return;
diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
index f13f6eba..3bfa1e00 100644
--- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
+++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
@@ -106,6 +106,7 @@ protected Set<String> getPlainTextMasks() {
                     "**/*.ksh",
                     "**/*.txt",
                     "**/*.jsp",
+                    "**/*.qute.java",
                     "**/*.sql",
                     "**/Dockerfile",
                     "**/Jenkinsfile"
diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
index fd9154ad..bcaad4a9 100644
--- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
+++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
@@ -81,8 +81,10 @@ public class MavenMojoProjectParser {
     private final Log logger;
     private final Path baseDir;
     private final boolean pomCacheEnabled;
+
     @Nullable
     private final String pomCacheDirectory;
+
     private final boolean skipMavenParsing;
 
     private final BuildTool buildTool;
@@ -131,7 +133,7 @@ public List<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedSty
                 .typeCache(typeCache)
                 .logCompilationWarningsAndErrors(false)
                 .build();
-        ResourceParser rp = new ResourceParser(baseDir, logger, exclusions, plainTextMasks, sizeThresholdMb, pathsToOtherMavenProjects(mavenProject));
+        ResourceParser rp = new ResourceParser(baseDir, logger, exclusions, plainTextMasks, sizeThresholdMb, pathsToOtherMavenProjects(mavenProject), javaParser);
 
         sourceFiles.addAll(processMainSources(mavenProject, javaParser, rp, projectProvenance, alreadyParsed, styles, ctx));
         sourceFiles.addAll(processTestSources(mavenProject, javaParser, rp, projectProvenance, alreadyParsed, styles, ctx));
@@ -210,7 +212,7 @@ private List<SourceFile> processMainSources(
 
         alreadyParsed.addAll(mainJavaSources);
 
-        logInfo(mavenProject, "Parsing Source Files");
+        logInfo(mavenProject, "Parsing source files");
         List<Path> dependencies = mavenProject.getCompileClasspathElements().stream()
                 .distinct()
                 .map(Paths::get)
@@ -311,12 +313,12 @@ public Xml.Document parseMaven(MavenProject mavenProject, List<Marker> projectPr
                 .parse(allPoms, baseDir, ctx);
 
         if (logger.isDebugEnabled()) {
-            logDebug(mavenProject, "Base Directory : '" + baseDir + "'");
+            logDebug(mavenProject, "Base directory : '" + baseDir + "'");
             if (allPoms.isEmpty()) {
                 logDebug(mavenProject, "There were no collected pom paths.");
             } else {
                 for (Path path : allPoms) {
-                    logDebug(mavenProject, "  Collected Pom : '" + path + "'");
+                    logDebug(mavenProject, "  Collected Maven POM : '" + path + "'");
                 }
             }
             if (mavens.isEmpty()) {
diff --git a/src/main/java/org/openrewrite/maven/ResourceParser.java b/src/main/java/org/openrewrite/maven/ResourceParser.java
index cd146a27..f0662950 100644
--- a/src/main/java/org/openrewrite/maven/ResourceParser.java
+++ b/src/main/java/org/openrewrite/maven/ResourceParser.java
@@ -5,6 +5,7 @@
 import org.openrewrite.InMemoryExecutionContext;
 import org.openrewrite.SourceFile;
 import org.openrewrite.hcl.HclParser;
+import org.openrewrite.java.JavaParser;
 import org.openrewrite.json.JsonParser;
 import org.openrewrite.properties.PropertiesParser;
 import org.openrewrite.protobuf.ProtoParser;
@@ -33,9 +34,16 @@ public class ResourceParser {
     private final Collection<Path> excludedDirectories;
     private final Collection<PathMatcher> plainTextMasks;
 
-    public ResourceParser(Path baseDir, Log logger, Collection<String> exclusions, Collection<String> plainTextMasks, int sizeThresholdMb, Collection<Path> excludedDirectories) {
+    /**
+     * Sometimes java files will exist in the src/main/resources directory. For example, Drools:
+     */
+    private final JavaParser javaParser;
+
+    public ResourceParser(Path baseDir, Log logger, Collection<String> exclusions, Collection<String> plainTextMasks, int sizeThresholdMb, Collection<Path> excludedDirectories,
+                          JavaParser javaParser) {
         this.baseDir = baseDir;
         this.logger = logger;
+        this.javaParser = javaParser;
         this.exclusions = pathMatchers(baseDir, exclusions);
         this.sizeThresholdMb = sizeThresholdMb;
         this.excludedDirectories = excludedDirectories;
@@ -59,9 +67,9 @@ public List<SourceFile> parse(Path searchDir, Collection<Path> alreadyParsed) {
         try {
             sourceFiles.addAll(parseSourceFiles(searchDir, alreadyParsed, ctx));
             List<PlainText> parseFailures = ParsingExecutionContextView.view(ctx).pollParseFailures();
-            if(!parseFailures.isEmpty()) {
+            if (!parseFailures.isEmpty()) {
                 logger.warn("There were problems parsing " + parseFailures.size() + " + sources:");
-                for(PlainText parseFailure : parseFailures) {
+                for (PlainText parseFailure : parseFailures) {
                     logger.warn("  " + parseFailure.getSourcePath());
                 }
                 logger.warn("Execution will continue but these files are unlikely to be affected by refactoring recipes");
@@ -96,10 +104,10 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
             @Override
             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                 if (!attrs.isOther() && !attrs.isSymbolicLink() &&
-                        !alreadyParsed.contains(file) && !isExcluded(file)) {
+                    !alreadyParsed.contains(file) && !isExcluded(file)) {
                     if (isOverSizeThreshold(attrs.size())) {
                         logger.info("Parsing as quark " + file + " as its size + " + attrs.size() / (1024L * 1024L) +
-                                "Mb exceeds size threshold " + sizeThresholdMb + "Mb");
+                                    "Mb exceeds size threshold " + sizeThresholdMb + "Mb");
                         quarkPaths.add(file);
                     } else if (isParsedAsPlainText(file)) {
                         plainTextPaths.add(file);
@@ -113,6 +121,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 
         List<S> sourceFiles = new ArrayList<>(resources.size() + quarkPaths.size());
 
+        List<Path> javaPaths = new ArrayList<>();
+
         JsonParser jsonParser = new JsonParser();
         List<Path> jsonPaths = new ArrayList<>();
 
@@ -136,7 +146,11 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
         QuarkParser quarkParser = new QuarkParser();
 
         resources.forEach(path -> {
-            if (jsonParser.accept(path)) {
+            // See https://github.com/quarkusio/quarkus/blob/main/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/resteasy-reactive-codestart/java/src/main/java/org/acme/%7Bresource.class-name%7D.tpl.qute.java
+            // for an example of why we don't want qute files be parsed as java
+            if (javaParser.accept(path) && !path.endsWith(".qute.java")) {
+                javaPaths.add(path);
+            } else if (jsonParser.accept(path)) {
                 jsonPaths.add(path);
             } else if (xmlParser.accept(path)) {
                 xmlPaths.add(path);
@@ -153,6 +167,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
             }
         });
 
+        sourceFiles.addAll((List<S>) javaParser.parse(javaPaths, baseDir, ctx));
+        alreadyParsed.addAll(javaPaths);
+
         sourceFiles.addAll((List<S>) jsonParser.parse(jsonPaths, baseDir, ctx));
         alreadyParsed.addAll(jsonPaths);