Skip to content

Commit

Permalink
Parse java files in src/main/resources with JavaParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Mar 3, 2023
1 parent 8cea2e3 commit 9221d35
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected Set<String> getPlainTextMasks() {
"**/*.ksh",
"**/*.txt",
"**/*.jsp",
"**/*.qute.java",
"**/*.sql",
"**/Dockerfile",
"**/Jenkinsfile"
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()) {
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/org/openrewrite/maven/ResourceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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<>();

Expand All @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 9221d35

Please sign in to comment.