Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javadoc tasks supporty BaselineModuleJvmArgs #2137

Merged
merged 3 commits into from
Mar 28, 2022
Merged
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2137.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Javadoc tasks supporty BaselineModuleJvmArgs
links:
- https://github.com/palantir/gradle-baseline/pull/2137
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.api.tasks.testing.Test;
import org.gradle.external.javadoc.CoreJavadocOptions;
import org.gradle.external.javadoc.MinimalJavadocOptions;
import org.gradle.jvm.tasks.Jar;
import org.gradle.process.CommandLineArgumentProvider;
import org.immutables.value.Value;
Expand All @@ -61,6 +64,7 @@ public final class BaselineModuleJvmArgs implements Plugin<Project> {
Splitter.on(' ').trimResults().omitEmptyStrings();

@Override
@SuppressWarnings("checkstyle:MethodLength")
public void apply(Project project) {
project.getPluginManager().withPlugin("java", unused -> {
BaselineModuleJvmArgsExtension extension =
Expand Down Expand Up @@ -92,6 +96,62 @@ public Iterable<String> asArguments() {
return arguments;
}
});

Task maybeJavadocTask = project.getTasks().findByName(sourceSet.getJavadocTaskName());
if (maybeJavadocTask instanceof Javadoc) {
maybeJavadocTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
Javadoc javadoc = (Javadoc) task;

MinimalJavadocOptions options = javadoc.getOptions();
if (options instanceof CoreJavadocOptions) {
CoreJavadocOptions coreOptions = (CoreJavadocOptions) options;
ImmutableList<JarManifestModuleInfo> info = collectClasspathInfo(project, sourceSet);
List<String> exportValues = Stream.concat(
extension.exports().get().stream(),
info.stream().flatMap(item -> item.exports().stream()))
.distinct()
.sorted()
.map(item -> item + "=ALL-UNNAMED")
.collect(ImmutableList.toImmutableList());
List<String> opens = Stream.concat(
extension.opens().get().stream(),
info.stream().flatMap(item -> item.opens().stream()))
.distinct()
.sorted()
.map(item -> item + "=ALL-UNNAMED")
.collect(ImmutableList.toImmutableList());
project.getLogger()
.debug(
"BaselineModuleJvmArgs building {} on {} "
+ "with exports: {} and opens: {}",
javadoc.getName(),
project,
exportValues,
opens);
if (!exportValues.isEmpty()) {
coreOptions
// options are automatically prefixed with '-' internally
.addMultilineStringsOption("-add-exports")
Copy link
Contributor

Choose a reason for hiding this comment

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

Was wondering why we can't use collectClasspathArgs here which does a very similar colelction of the export and open args. Is it because of the -- vs - distinction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sort of, add-exports only occurs once, and the values are collected on the right-hand-side.

.setValue(exportValues);
}
if (!opens.isEmpty()) {
coreOptions
// options are automatically prefixed with '-' internally
.addMultilineStringsOption("-add-opens")
.setValue(opens);
}
} else {
project.getLogger()
.error(
"MinimalJavadocOptions implementation was "
+ "not CoreJavadocOptions, rather '{}'",
options.getClass().getName());
}
}
});
}
});

project.getTasks().withType(Test.class, new Action<Test>() {
Expand Down Expand Up @@ -192,7 +252,29 @@ private static ImmutableList<String> collectAnnotationProcessorArgs(

private static ImmutableList<String> collectClasspathArgs(
Project project, BaselineModuleJvmArgsExtension extension, FileCollection classpath) {
ImmutableList<JarManifestModuleInfo> classpathInfo = classpath.getFiles().stream()
ImmutableList<JarManifestModuleInfo> classpathInfo = collectClasspathInfo(project, classpath);
Stream<String> exports = Stream.concat(
extension.exports().get().stream(),
classpathInfo.stream().flatMap(info -> info.exports().stream()))
.distinct()
.sorted()
.flatMap(BaselineModuleJvmArgs::addExportArg);
Stream<String> opens = Stream.concat(
extension.opens().get().stream(), classpathInfo.stream().flatMap(info -> info.opens().stream()))
.distinct()
.sorted()
.flatMap(BaselineModuleJvmArgs::addOpensArg);
return Stream.concat(exports, opens).collect(ImmutableList.toImmutableList());
}

private static ImmutableList<JarManifestModuleInfo> collectClasspathInfo(Project project, SourceSet sourceSet) {
return collectClasspathInfo(
project, project.getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()));
}

private static ImmutableList<JarManifestModuleInfo> collectClasspathInfo(
Project project, FileCollection classpath) {
return classpath.getFiles().stream()
.map(file -> {
try {
if (file.getName().endsWith(".jar") && file.isFile()) {
Expand All @@ -212,18 +294,6 @@ private static ImmutableList<String> collectClasspathArgs(
})
.filter(Objects::nonNull)
.collect(ImmutableList.toImmutableList());
Stream<String> exports = Stream.concat(
extension.exports().get().stream(),
classpathInfo.stream().flatMap(info -> info.exports().stream()))
.distinct()
.sorted()
.flatMap(BaselineModuleJvmArgs::addExportArg);
Stream<String> opens = Stream.concat(
extension.opens().get().stream(), classpathInfo.stream().flatMap(info -> info.opens().stream()))
.distinct()
.sorted()
.flatMap(BaselineModuleJvmArgs::addOpensArg);
return Stream.concat(exports, opens).collect(ImmutableList.toImmutableList());
}

private static Optional<JarManifestModuleInfo> parseModuleInfo(@Nullable java.util.jar.Manifest jarManifest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ class BaselineModuleJvmArgsIntegrationTest extends IntegrationSpec {
runTasksSuccessfully('compileJava')
}

def 'Builds javadoc with locally defined exports'() {
when:
buildFile << '''
application {
mainClass = 'com.Example'
}
moduleJvmArgs {
exports = ['jdk.compiler/com.sun.tools.javac.code']
}
'''.stripIndent(true)
writeJavaSourceFile('''
package com;
public class Example {
/**
* Javadoc {@link com.sun.tools.javac.code.Symbol}.
* @param args Program arguments
*/
public static void main(String[] args) {
com.sun.tools.javac.code.Symbol.class.toString();
}
}
'''.stripIndent(true))

then:
runTasksSuccessfully('javadoc')
}

def 'Runs with locally defined exports'() {
when:
buildFile << '''
Expand Down