Skip to content

Commit

Permalink
Compilation uses 'opens' values as 'exports' (#2167)
Browse files Browse the repository at this point in the history
Compilation uses 'opens' values as 'exports'
  • Loading branch information
carterkozak authored Apr 2, 2022
1 parent 6e6a1c6 commit 160ffff
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 40 deletions.
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

0 comments on commit 160ffff

Please sign in to comment.