From 2758817ada096df240d94c8995fa528a1c164a18 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 24 Jun 2022 15:48:48 -0700 Subject: [PATCH] Convert more classes to CompileStatic (#569) As a solution to use AnimalSniffer to detect incompatibilities with Java 8, as discussed at https://github.com/google/protobuf-gradle-plugin/pull/565#issuecomment-1159752656 --- .../protobuf/gradle/GenerateProtoTask.groovy | 27 +++++++++++-------- .../gradle/ProtobufConfigurator.groovy | 7 +++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index cbf51b5b..840960ed 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -33,7 +33,9 @@ import static java.nio.charset.StandardCharsets.US_ASCII import com.google.common.base.Preconditions import com.google.common.collect.ImmutableList -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic +import groovy.transform.TypeChecked +import groovy.transform.TypeCheckingMode import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.Named @@ -72,7 +74,7 @@ import javax.inject.Inject * The task that compiles proto files into Java files. */ // TODO(zhangkun83): add per-plugin output dir reconfiguraiton. -@CompileDynamic +@CompileStatic @CacheableTask public abstract class GenerateProtoTask extends DefaultTask { // Windows CreateProcess has command line limit of 32768: @@ -180,7 +182,7 @@ public abstract class GenerateProtoTask extends DefaultTask { static List> generateCmds(List baseCmd, List protoFiles, int cmdLengthLimit) { List> cmds = [] if (!protoFiles.isEmpty()) { - int baseCmdLength = baseCmd.sum { it.length() + CMD_ARGUMENT_EXTRA_LENGTH } + int baseCmdLength = baseCmd.sum { it.length() + CMD_ARGUMENT_EXTRA_LENGTH } as int List currentArgs = [] int currentArgsLength = 0 for (File proto: protoFiles) { @@ -361,7 +363,8 @@ public abstract class GenerateProtoTask extends DefaultTask { @InputFiles @PathSensitive(PathSensitivity.NONE) FileCollection getSnapshotArtifacts() { - Provider> snapshotArtifacts = locatorToAlternativePathsMapping.map { map -> + Provider> snapshotArtifacts = locatorToAlternativePathsMapping.map { + Map map -> Set releaseArtifactKeys = releaseDependenciesMapping.get().keySet() map.findAll { entry -> !releaseArtifactKeys.contains(entry.key) @@ -372,6 +375,7 @@ public abstract class GenerateProtoTask extends DefaultTask { } @Internal + @TypeChecked(TypeCheckingMode.SKIP) // entry.value always returns an Object for an unknown reason Provider> getReleaseDependenciesMapping() { providerFactory.provider { locatorToDependencyMapping.get() @@ -386,7 +390,7 @@ public abstract class GenerateProtoTask extends DefaultTask { protocLocator.getOrNull()?.path }.from { pluginsExecutableLocators.get().values() - .collect { it.path } + .collect { ((ExecutableLocator) it).path } .findAll { it } } } @@ -415,6 +419,7 @@ public abstract class GenerateProtoTask extends DefaultTask { return flavors } + @TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP @Internal("Not an actual input to the task, only used to find tasks belonging to a variant") String getBuildType() { Preconditions.checkState(isAndroidProject.get(), @@ -619,7 +624,7 @@ public abstract class GenerateProtoTask extends DefaultTask { // to avoid triggering unnecessary rebuilds downstream List protoFiles = sourceFiles.files.sort() - [builtins, plugins]*.each { plugin -> + [builtins, plugins]*.forEach { PluginOptions plugin -> String outputPath = getOutputDir(plugin) File outputDir = new File(outputPath) // protoc is capable of output generated files directly to a JAR file @@ -632,7 +637,7 @@ public abstract class GenerateProtoTask extends DefaultTask { // The source directory designated from sourceSet may not actually exist on disk. // "include" it only when it exists, so that Gradle and protoc won't complain. - List dirs = includeDirs.filter { it.exists() }*.path.collect { "-I${it}" } + List dirs = includeDirs.filter { File it -> it.exists() }*.path.collect { "-I${it}".toString() } logger.debug "ProtobufCompile using directories ${dirs}" logger.debug "ProtobufCompile using files ${protoFiles}" @@ -643,7 +648,7 @@ public abstract class GenerateProtoTask extends DefaultTask { // Handle code generation built-ins builtins.each { builtin -> String outPrefix = makeOptionsPrefix(builtin.options) - baseCmd += "--${builtin.name}_out=${outPrefix}${getOutputDir(builtin)}" + baseCmd += "--${builtin.name}_out=${outPrefix}${getOutputDir(builtin)}".toString() } Map executableLocations = pluginsExecutableLocators.get() @@ -652,12 +657,12 @@ public abstract class GenerateProtoTask extends DefaultTask { String name = plugin.name ExecutableLocator locator = executableLocations.get(name) if (locator != null) { - baseCmd += "--plugin=protoc-gen-${name}=${computeExecutablePath(locator)}" + baseCmd += "--plugin=protoc-gen-${name}=${computeExecutablePath(locator)}".toString() } else { logger.warn "protoc plugin '${name}' not defined. Trying to use 'protoc-gen-${name}' from system path" } String pluginOutPrefix = makeOptionsPrefix(plugin.options) - baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}" + baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}".toString() } if (generateDescriptorSet) { @@ -668,7 +673,7 @@ public abstract class GenerateProtoTask extends DefaultTask { if (!folder.exists()) { folder.mkdirs() } - baseCmd += "--descriptor_set_out=${path}" + baseCmd += "--descriptor_set_out=${path}".toString() if (descriptorSetOptions.includeImports) { baseCmd += "--include_imports" } diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy index 23b08df5..1e8dbe15 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy @@ -28,7 +28,9 @@ */ package com.google.protobuf.gradle -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic +import groovy.transform.TypeChecked +import groovy.transform.TypeCheckingMode import org.gradle.api.Project import org.gradle.api.internal.file.FileResolver import org.gradle.api.tasks.TaskCollection @@ -37,7 +39,7 @@ import org.gradle.util.ConfigureUtil /** * The main configuration block exposed as {@code protobuf} in the build script. */ -@CompileDynamic +@CompileStatic public class ProtobufConfigurator { private final Project project private final GenerateProtoTaskCollection tasks @@ -135,6 +137,7 @@ public class ProtobufConfigurator { } } + @TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP public TaskCollection ofVariant(String variant) { return all().matching { GenerateProtoTask task -> task.variant.name == variant