@@ -54,6 +54,19 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
54
54
private val HAS_TOOLCHAINS = GradleVersion .current().baseVersion >= GradleVersion .version(" 6.7" )
55
55
56
56
internal const val TOO_OLD_TOOLCHAIN_ERROR_MESSAGE = " Must not enable ErrorProne when compiling with JDK < 8"
57
+
58
+ internal val JVM_ARGS_STRONG_ENCAPSULATION = listOf (
59
+ " --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" ,
60
+ " --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" ,
61
+ " --add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED" ,
62
+ " --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" ,
63
+ " --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED" ,
64
+ " --add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED" ,
65
+ " --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" ,
66
+ " --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" ,
67
+ " --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED" ,
68
+ " --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"
69
+ )
57
70
}
58
71
59
72
override fun apply (project : Project ) {
@@ -101,7 +114,7 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
101
114
.compilerArgumentProviders
102
115
.add(ErrorProneCompilerArgumentProvider (errorproneOptions))
103
116
104
- if (HAS_TOOLCHAINS || JavaVersion .current().isJava8) {
117
+ if (HAS_TOOLCHAINS || JavaVersion .current().run { isJava8 || isJava16Compatible } ) {
105
118
inputs.files(
106
119
providers.provider {
107
120
when {
@@ -125,10 +138,13 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
125
138
when {
126
139
targetVersion < 8 -> throw UnsupportedOperationException (TOO_OLD_TOOLCHAIN_ERROR_MESSAGE )
127
140
targetVersion == 8 -> configureErrorProneJavac()
141
+ targetVersion >= 16 -> configureForJava16plus()
128
142
}
129
143
}
130
144
JavaVersion .current().isJava8 && (! options.isFork || (options.forkOptions.javaHome == null && options.forkOptions.executable == null )) ->
131
145
configureErrorProneJavac()
146
+ JavaVersion .current().isJava16Compatible && (! options.isFork || (options.forkOptions.javaHome == null && options.forkOptions.executable == null )) ->
147
+ configureForJava16plus()
132
148
}
133
149
}
134
150
}
@@ -174,6 +190,16 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
174
190
}
175
191
}
176
192
}
193
+
194
+ private fun JavaCompile.configureForJava16plus () {
195
+ // https://github.com/google/error-prone/issues/1157#issuecomment-769289564
196
+ if (! options.isFork) {
197
+ options.isFork = true
198
+ // reset forkOptions in case they were configured
199
+ options.forkOptions = ForkOptions ()
200
+ }
201
+ options.forkOptions.jvmArgs!! .addAll(JVM_ARGS_STRONG_ENCAPSULATION )
202
+ }
177
203
}
178
204
179
205
internal class ErrorProneCompilerArgumentProvider (
@@ -199,3 +225,6 @@ internal class ErrorProneCompilerArgumentProvider(
199
225
200
226
internal val TEST_SOURCE_SET_NAME_REGEX =
201
227
""" ^(t|.*T)est(\p{javaUpperCase}.*)?$""" .toRegex()
228
+
229
+ internal val JavaVersion .isJava16Compatible: Boolean
230
+ get() = this < JavaVersion .VERSION_HIGHER && this >= JavaVersion .toVersion(16 )
0 commit comments