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

Compilation uses 'opens' values as 'exports' #2167

Merged
merged 3 commits into from
Apr 2, 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-2167.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Compilation uses 'opens' values as 'exports'
links:
- https://github.com/palantir/gradle-baseline/pull/2167
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public Iterable<String> asArguments() {
project);
return ImmutableList.of();
}
// Annotation processors are executed at compile time
ImmutableList<String> arguments =
collectAnnotationProcessorArgs(project, extension, sourceSet);
ImmutableList<String> arguments = collectCompilationArgs(project, extension, sourceSet);
project.getLogger()
.debug(
"BaselineModuleJvmArgs compiling {} on {} with exports: {}",
Expand Down Expand Up @@ -130,39 +128,29 @@ public void execute(Task task) {
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()))
// Compilation only supports exports, so we union with opens.
Stream.concat(
extension.exports().get().stream(),
extension.opens().get().stream()),
info.stream()
.flatMap(item -> Stream.concat(
item.exports().stream(), item.opens().stream())))
.distinct()
.sorted()
.map(item -> item + "=ALL-UNNAMED")
.collect(ImmutableList.toImmutableList());
project.getLogger()
.debug(
"BaselineModuleJvmArgs building {} on {} "
+ "with exports: {} and opens: {}",
"BaselineModuleJvmArgs building {} on {} " + "with exports: {}",
javadoc.getName(),
project,
exportValues,
opens);
exportValues);
if (!exportValues.isEmpty()) {
coreOptions
// options are automatically prefixed with '-' internally
.addMultilineStringsOption("-add-exports")
.setValue(exportValues);
}
if (!opens.isEmpty()) {
coreOptions
// options are automatically prefixed with '-' internally
.addMultilineStringsOption("-add-opens")
.setValue(opens);
}
} else {
project.getLogger()
.error(
Expand All @@ -184,7 +172,7 @@ public void execute(Test test) {
@Override
public Iterable<String> asArguments() {
ImmutableList<String> arguments =
collectClasspathArgs(project, extension, test.getClasspath());
collectClasspathArgs(project, extension, test.getClasspath(), OpensMode.RUNTIME);
project.getLogger()
.debug(
"BaselineModuleJvmArgs executing {} on {} with exports: {}",
Expand All @@ -205,8 +193,8 @@ public void execute(JavaExec javaExec) {

@Override
public Iterable<String> asArguments() {
ImmutableList<String> arguments =
collectClasspathArgs(project, extension, javaExec.getClasspath());
ImmutableList<String> arguments = collectClasspathArgs(
project, extension, javaExec.getClasspath(), OpensMode.RUNTIME);
project.getLogger()
.debug(
"BaselineModuleJvmArgs executing {} on {} with exports: {}",
Expand Down Expand Up @@ -263,29 +251,35 @@ private static void addManifestAttribute(
}
}

private static ImmutableList<String> collectAnnotationProcessorArgs(
private static ImmutableList<String> collectCompilationArgs(
Project project, BaselineModuleJvmArgsExtension extension, SourceSet sourceSet) {
return collectClasspathArgs(
project,
extension,
project.getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()));
project.getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName()),
OpensMode.COMPILATION);
}

private static ImmutableList<String> collectClasspathArgs(
Project project, BaselineModuleJvmArgsExtension extension, FileCollection classpath) {
Project project, BaselineModuleJvmArgsExtension extension, FileCollection classpath, OpensMode mode) {
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());
Stream<String> allExports = Stream.concat(
extension.exports().get().stream(), classpathInfo.stream().flatMap(info -> info.exports().stream()));
Stream<String> allOpens = Stream.concat(
extension.opens().get().stream(), classpathInfo.stream().flatMap(info -> info.opens().stream()));
switch (mode) {
case COMPILATION:
return Stream.concat(allExports, allOpens)
.distinct()
.sorted()
.flatMap(BaselineModuleJvmArgs::addExportArg)
.collect(ImmutableList.toImmutableList());
case RUNTIME:
Stream<String> exports = allExports.distinct().sorted().flatMap(BaselineModuleJvmArgs::addExportArg);
Stream<String> opens = allOpens.distinct().sorted().flatMap(BaselineModuleJvmArgs::addOpensArg);
return Stream.concat(exports, opens).collect(ImmutableList.toImmutableList());
}
throw new IllegalStateException("unknown mode: " + mode);
}

private static ImmutableList<JarManifestModuleInfo> collectClasspathInfo(Project project, SourceSet sourceSet) {
Expand Down Expand Up @@ -361,4 +355,9 @@ static Builder builder() {

class Builder extends ImmutableJarManifestModuleInfo.Builder {}
}

enum OpensMode {
COMPILATION,
RUNTIME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ class BaselineModuleJvmArgsIntegrationTest extends IntegrationSpec {
runTasksSuccessfully('compileJava')
}

def 'Compiles with locally defined opens'() {
when:
buildFile << '''
application {
mainClass = 'com.Example'
}
moduleJvmArgs {
opens = ['jdk.compiler/com.sun.tools.javac.code']
}
'''.stripIndent(true)
writeJavaSourceFile('''
package com;
public class Example {
public static void main(String[] args) {
com.sun.tools.javac.code.Symbol.class.toString();
}
}
'''.stripIndent(true))

then:
runTasksSuccessfully('compileJava')
}

def 'Builds javadoc with locally defined exports'() {
when:
buildFile << '''
Expand Down Expand Up @@ -102,6 +125,33 @@ class BaselineModuleJvmArgsIntegrationTest extends IntegrationSpec {
runTasksSuccessfully('javadoc')
}

def 'Builds javadoc with locally defined opens'() {
when:
buildFile << '''
application {
mainClass = 'com.Example'
}
moduleJvmArgs {
opens = ['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