Skip to content

Commit

Permalink
Use new ParseError mechanism
Browse files Browse the repository at this point in the history
Parse errors no longer result in `null` values returned by the parsers and a corresponding message in the `ExecutionContext`. Instead, there is a new `ParseError` type extending `SourceFile` for this purpose.
  • Loading branch information
knutwannheden committed Jun 9, 2023
1 parent 4c54f91 commit 83d2903
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ParseError;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
Expand All @@ -35,7 +36,6 @@
import org.openrewrite.maven.internal.RawRepositories;
import org.openrewrite.maven.tree.ProfileActivation;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.text.PlainText;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.openrewrite.xml.tree.Xml;

Expand Down Expand Up @@ -173,23 +173,14 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, @Nullable X
sourceFiles = Stream.concat(sourceFiles, parsedResourceFiles);

// by appending the parse failures to the end of the stream, the code above will be executed last
return Stream.concat(sourceFiles, parseFailures(ctx));
return sourceFiles.map(this::logParseErrors);
}

private Stream<SourceFile> parseFailures(ExecutionContext ctx) {
return Stream.of(ParsingExecutionContextView.view(ctx))
.flatMap(ctxView -> {
List<PlainText> parseFailures = ctxView.pollParseFailures();
if (!parseFailures.isEmpty()) {
logger.warn("There were problems parsing " + parseFailures.size() + " sources:");
for (PlainText parseFailure : parseFailures) {
logger.warn(" " + parseFailure.getSourcePath());
}
logger.warn("Execution will continue but these files are unlikely to be affected by refactoring recipes");
return parseFailures.stream();
}
return Stream.empty();
});
private SourceFile logParseErrors(SourceFile source) {
if (source instanceof ParseError) {
logger.warn("There were problems parsing " + source.getSourcePath());
}
return source;
}

public List<Marker> generateProvenance(MavenProject mavenProject) {
Expand Down Expand Up @@ -338,7 +329,7 @@ private Stream<SourceFile> processTestSources(
alreadyParsed.addAll(testJavaSources);

JavaParser javaParser = javaParserBuilder.build();
Stream<J.CompilationUnit> cus = javaParser.parse(testJavaSources, baseDir, ctx);
Stream<SourceFile> cus = javaParser.parse(testJavaSources, baseDir, ctx);

List<Marker> markers = new ArrayList<>(projectProvenance);
markers.add(sourceSet("test", testDependencies, typeCache));
Expand Down Expand Up @@ -398,7 +389,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
mavenParserBuilder.activeProfiles(activeProfiles.toArray(new String[]{}));
}

List<Xml.Document> mavens = mavenParserBuilder
List<SourceFile> mavens = mavenParserBuilder
.build()
.parse(allPoms, baseDir, ctx).collect(toList());

Expand All @@ -414,19 +405,19 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
if (mavens.isEmpty()) {
logDebug(topLevelProject, "There were no parsed maven source files.");
} else {
for (Xml.Document source : mavens) {
for (SourceFile source : mavens) {
logDebug(topLevelProject, " Maven Source : '" + baseDir.resolve(source.getSourcePath()) + "'");
}
}
}

Map<Path, MavenProject> projectsByPath = mavenProjects.stream().collect(Collectors.toMap(MavenMojoProjectParser::pomPath, Function.identity()));
Map<MavenProject, Xml.Document> projectMap = new HashMap<>();
for (Xml.Document document : mavens) {
for (SourceFile document : mavens) {
Path path = baseDir.resolve(document.getSourcePath());
MavenProject mavenProject = projectsByPath.get(path);
if (mavenProject != null) {
projectMap.put(mavenProject, document);
projectMap.put(mavenProject, (Xml.Document) document);
}
}
for (MavenProject mavenProject : mavenProjects) {
Expand Down

0 comments on commit 83d2903

Please sign in to comment.