Skip to content

Commit

Permalink
Add more events to ParsingEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 10, 2023
1 parent b065a6e commit 8158227
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,28 @@ public static PlainText convert(SourceFile sourceFile) {
public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo,
ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return StreamSupport.stream(sources.spliterator(), false).map(source -> {
Path path = source.getRelativePath(relativeTo);
return StreamSupport.stream(sources.spliterator(), false).map(input -> {
Path path = input.getRelativePath(relativeTo);
parsingListener.startedParsing(input);
try {
EncodingDetectingInputStream is = source.getSource(ctx);
EncodingDetectingInputStream is = input.getSource(ctx);
String sourceStr = is.readFully();
PlainText plainText = new PlainText(
randomId(),
path,
Markers.EMPTY,
is.getCharset().name(),
is.isCharsetBomMarked(),
source.getFileAttributes(),
input.getFileAttributes(),
null,
sourceStr,
null
);
parsingListener.parsed(source, plainText);
parsingListener.parsed(input, plainText);
return plainText;
} catch (Throwable t) {
ctx.getOnError().accept(t);
return ParseError.build(this, source, relativeTo, ctx, t);
return ParseError.build(this, input, relativeTo, ctx, t);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@
import org.openrewrite.SourceFile;

public interface ParsingEventListener {
ParsingEventListener NOOP = (input, sourceFile) -> {};
ParsingEventListener NOOP = new ParsingEventListener() {
};

void parsed(Parser.Input input, SourceFile sourceFile);
/**
* Parsers may call this one or more times before parsing begins on any one source file
* to indicate to listeners preparatory steps that are about to be taken.
* @param stateMessage The message to the listener.
*/
default void intermediateMessage(String stateMessage) {
}

default void startedParsing(Parser.Input input) {
}

default void parsed(Parser.Input input, SourceFile sourceFile) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
errorCollector
);

pctx.getParsingListener().startedParsing(input);
CompilationUnit compUnit = new CompilationUnit(configuration, null, classLoader, classLoader);
compUnit.addSource(unit);
compUnit.compile(Phases.CANONICALIZATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).map(input -> {
try {
parsingListener.startedParsing(input);
EncodingDetectingInputStream is = input.getSource(ctx);
String sourceStr = is.readFully();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ public static Builder builder() {
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
LinkedHashMap<Input, JCTree.JCCompilationUnit> cus = parseInputsToCompilerAst(sourceFiles, ctx);

parsingListener.intermediateMessage("Compiling Java source files");
return cus.entrySet().stream().map(cuByPath -> {
Input input = cuByPath.getKey();
parsingListener.startedParsing(input);
try {
ReloadableJava11ParserVisitor parser = new ReloadableJava11ParserVisitor(
input.getRelativePath(relativeTo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public static Builder builder() {
@Override
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
parsingListener.intermediateMessage("Compiling Java source files");
LinkedHashMap<Input, JCTree.JCCompilationUnit> cus = parseInputsToCompilerAst(sourceFiles, ctx);
return cus.entrySet().stream().map(cuByPath -> {
Input input = cuByPath.getKey();
parsingListener.startedParsing(input);
try {
ReloadableJava17ParserVisitor parser = new ReloadableJava17ParserVisitor(
input.getRelativePath(relativeTo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void close() {
@Override
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();

parsingListener.intermediateMessage("Compiling Java source files");
if (classpath != null) { // override classpath
if (context.get(JavaFileManager.class) != pfm) {
throw new IllegalStateException("JavaFileManager has been forked unexpectedly");
Expand Down Expand Up @@ -180,6 +180,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat

return cus.entrySet().stream().map(cuByPath -> {
Input input = cuByPath.getKey();
parsingListener.startedParsing(input);
try {
ReloadableJava8ParserVisitor parser = new ReloadableJava8ParserVisitor(
input.getRelativePath(relativeTo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class JsonParser implements Parser {
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).map(input -> {
parsingListener.startedParsing(input);
try (InputStream sourceStream = input.getSource(ctx)) {
JSON5Parser parser = new JSON5Parser(new CommonTokenStream(new JSON5Lexer(
CharStreams.fromStream(sourceStream))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.tree.ParseError;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.openrewrite.xml.XmlParser;
import org.openrewrite.xml.tree.Xml;

Expand Down Expand Up @@ -94,6 +95,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
}
}

ParsingExecutionContextView.view(ctx).getParsingListener().intermediateMessage("Resolving Maven dependencies");
MavenPomDownloader downloader = new MavenPomDownloader(projectPomsByPath, ctx);

MavenExecutionContextView mavenCtx = MavenExecutionContextView.view(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public Stream<SourceFile> parse(@Language("properties") String... sources) {
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).map(input -> {
parsingListener.startedParsing(input);
Path path = input.getRelativePath(relativeTo);
try (EncodingDetectingInputStream is = input.getSource(ctx)) {
Properties.File file = parseFromInput(path, is)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ProtoParser implements Parser {
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).map(input -> {
parsingListener.startedParsing(input);
Path path = input.getRelativePath(relativeTo);
try {
EncodingDetectingInputStream is = input.getSource(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class XmlParser implements Parser {
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles).map(input -> {
parsingListener.startedParsing(input);
Path path = input.getRelativePath(relativeTo);
try (EncodingDetectingInputStream is = input.getSource(ctx)) {
String sourceStr = is.readFully();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
return acceptedInputs(sourceFiles)
.map(input -> {
parsingListener.startedParsing(input);
Path path = input.getRelativePath(relativeTo);
try (EncodingDetectingInputStream is = input.getSource(ctx)) {
Yaml.Documents yaml = parseFromInput(path, is);
Expand Down

0 comments on commit 8158227

Please sign in to comment.