Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ protected LargeSourceSet loadSourceSet(Path repositoryRoot, Environment env, Exe
//Parse and collect source files from each project in the maven session.
MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), repositoryRoot, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule, true);

Stream<SourceFile> sourceFiles = projectParser.listSourceFiles(project, styles, ctx);
List<SourceFile> sourceFileList = sourcesWithAutoDetectedStyles(sourceFiles);
Stream<SourceFile> sourceFiles = projectParser.listSourceFiles(project, ctx);
List<SourceFile> sourceFileList = sourcesWithAutoDetectedStyles(sourceFiles, styles);
return new InMemoryLargeSourceSet(sourceFileList);
}

Expand All @@ -262,7 +262,7 @@ protected List<Result> runRecipe(Recipe recipe, LargeSourceSet sourceSet, Execut
}).collect(toList());
}

private List<SourceFile> sourcesWithAutoDetectedStyles(Stream<SourceFile> sourceFiles) {
private List<SourceFile> sourcesWithAutoDetectedStyles(Stream<SourceFile> sourceFiles, List<NamedStyles> configuredStyles) {
org.openrewrite.java.style.Autodetect.Detector javaDetector = org.openrewrite.java.style.Autodetect.detector();
org.openrewrite.kotlin.style.Autodetect.Detector kotlinDetector = org.openrewrite.kotlin.style.Autodetect.detector();
org.openrewrite.xml.style.Autodetect.Detector xmlDetector = org.openrewrite.xml.style.Autodetect.detector();
Expand All @@ -283,16 +283,21 @@ private List<SourceFile> sourcesWithAutoDetectedStyles(Stream<SourceFile> source
stylesByType.put(K.CompilationUnit.class, kotlinDetector.build());
stylesByType.put(Xml.Document.class, xmlDetector.build());

return ListUtils.map(sourceFileList, applyAutodetectedStyle(stylesByType));
return ListUtils.map(sourceFileList, applyStyles(stylesByType, configuredStyles));
}

private UnaryOperator<SourceFile> applyAutodetectedStyle(Map<Class<? extends Tree>, NamedStyles> stylesByType) {
private UnaryOperator<SourceFile> applyStyles(Map<Class<? extends Tree>, NamedStyles> stylesByType, List<NamedStyles> configuredStyles) {
return before -> {
// Apply auto-detected styles first
for (Map.Entry<Class<? extends Tree>, NamedStyles> styleTypeEntry : stylesByType.entrySet()) {
if (styleTypeEntry.getKey().isAssignableFrom(before.getClass())) {
before = before.withMarkers(before.getMarkers().add(styleTypeEntry.getValue()));
}
}
// Apply configured styles last so they take precedence with "last wins" semantics
for (NamedStyles configuredStyle : configuredStyles) {
before = before.withMarkers(before.getMarkers().add(configuredStyle));
}
return before;
};
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import org.openrewrite.maven.utilities.MavenWrapper;
import org.openrewrite.polyglot.OmniParser;
import org.openrewrite.quark.QuarkParser;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.text.PlainTextParser;
import org.openrewrite.xml.tree.Xml;

Expand Down Expand Up @@ -164,13 +163,13 @@ public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled,
this.parseAdditionalResources = parseAdditionalResources;
}

public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedStyles> styles,
public Stream<SourceFile> listSourceFiles(MavenProject mavenProject,
ExecutionContext ctx) throws DependencyResolutionRequiredException, MojoExecutionException, MojoFailureException {
if (runPerSubmodule) {
//If running per submodule, parse the source files for only the current project.
List<Marker> projectProvenance = generateProvenance(mavenProject);
Xml.Document maven = parseMaven(mavenProject, projectProvenance, ctx);
return listSourceFiles(mavenProject, maven, projectProvenance, styles, ctx);
return listSourceFiles(mavenProject, maven, projectProvenance, ctx);
}
//If running across all projects, iterate and parse source files from each project
Map<MavenProject, List<Marker>> projectProvenances = mavenSession.getProjects().stream()
Expand All @@ -180,20 +179,20 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedS
.flatMap(project -> {
List<Marker> projectProvenance = projectProvenances.get(project);
try {
return listSourceFiles(project, projectMap.get(project), projectProvenance, styles, ctx);
return listSourceFiles(project, projectMap.get(project), projectProvenance, ctx);
} catch (DependencyResolutionRequiredException | MojoExecutionException e) {
throw sneakyThrow(e);
}
});
}

public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullable Document maven, List<Marker> projectProvenance, List<NamedStyles> styles,
public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullable Document maven, List<Marker> projectProvenance,
ExecutionContext ctx) throws DependencyResolutionRequiredException, MojoExecutionException {
return listSourceFiles(mavenProject, maven, projectProvenance, Arrays.asList(MAIN, TEST), styles, ctx);
return listSourceFiles(mavenProject, maven, projectProvenance, Arrays.asList(MAIN, TEST), ctx);
}

public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullable Document maven, List<Marker> projectProvenance, List<MavenScope> scopes,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is used by the CLI, and removing the styles here would cause problems there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some weird interplay here between the Maven plugin detecting styles after and separate from listSourceFiles, whereas the CLI only calls listSourceFiles and itself in a limited capacity loads styles.

Would we need to detect styles as part of listSourceFiles perhaps, and remove the duplicate logic in the CLI? Any ideas here Sam since this follows on from your earlier work?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should make things consistent between the CLI, gradle plugins, and maven plugins. I believe there is some inconsistency with how styles are treated / autodetected in the CLI and between the two plugins. There is likely some redundancy.

List<NamedStyles> styles, ExecutionContext ctx) throws DependencyResolutionRequiredException, MojoExecutionException {
ExecutionContext ctx) throws DependencyResolutionRequiredException, MojoExecutionException {
Stream<SourceFile> sourceFiles = Stream.empty();
Set<Path> alreadyParsed = new HashSet<>();

Expand All @@ -203,10 +202,8 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullab
}

JavaParser.Builder<? extends JavaParser, ?> javaParserBuilder = JavaParser.fromJavaVersion()
.styles(styles)
.logCompilationWarningsAndErrors(false);

// todo, add styles from autoDetect
KotlinParser.Builder kotlinParserBuilder = KotlinParser.builder();

if (scopes.contains(MAIN)) {
Expand Down