Skip to content

Commit

Permalink
Collect Refaster rules across all provided files
Browse files Browse the repository at this point in the history
Fixes google#552.
  • Loading branch information
Stephan202 committed Aug 29, 2024
1 parent ce72e21 commit 7a28964
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* TaskListener that receives compilation of a Refaster rule class and outputs a serialized analyzer
* to the specified path.
*/
public class RefasterRuleCompilerAnalyzer implements TaskListener {
private final List<CodeTransformer> allRules = Collections.synchronizedList(new ArrayList<>());
private final Context context;
private final Path destinationPath;

Expand All @@ -46,6 +48,10 @@ public class RefasterRuleCompilerAnalyzer implements TaskListener {

@Override
public void finished(TaskEvent taskEvent) {
if (taskEvent.getKind() == Kind.COMPILATION) {
persist();
return;
}
if (taskEvent.getKind() != Kind.ANALYZE) {
return;
}
Expand All @@ -67,9 +73,13 @@ public Void visitClass(ClassTree node, Context context) {
if (rules.isEmpty()) {
throw new IllegalArgumentException("Did not find any Refaster templates");
}
allRules.addAll(rules);
}

private void persist() {
try (ObjectOutputStream output =
new ObjectOutputStream(Files.newOutputStream(destinationPath))) {
output.writeObject(CompositeCodeTransformer.compose(rules));
output.writeObject(CompositeCodeTransformer.compose(allRules));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 7a28964

Please sign in to comment.