From 48862b5d6e3ee216eb6ed1a730a22546b05cc731 Mon Sep 17 00:00:00 2001 From: rougsig Date: Thu, 23 Jun 2022 23:05:41 +0300 Subject: [PATCH] Easy part of CompileStatic migration If groovy compiles without CompileStatic - code will be compiled in dynamic way, e.g. All type checks will be in runtime. Erased types in bytecode. With CompileStatic - compiler will save all typed in bytecode. If all types saved, we can do static analysis of bytecode with java 8 capability. More details can be found here: https://github.com/google/protobuf-gradle-plugin/pull/565#issuecomment-1159752656 --- .../protobuf/gradle/ArchiveActionFacade.java | 4 ++ .../protobuf/gradle/CopyActionFacade.java | 4 ++ .../protobuf/gradle/ExecutableLocator.groovy | 4 +- .../protobuf/gradle/ProtobufConvention.groovy | 4 +- .../protobuf/gradle/ProtobufExtract.groovy | 6 +-- .../protobuf/gradle/ToolsLocator.groovy | 37 +++++++++++-------- .../com/google/protobuf/gradle/Utils.groovy | 4 +- 7 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ArchiveActionFacade.java b/src/main/groovy/com/google/protobuf/gradle/ArchiveActionFacade.java index 3d4f2779..f16dee37 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ArchiveActionFacade.java +++ b/src/main/groovy/com/google/protobuf/gradle/ArchiveActionFacade.java @@ -28,18 +28,21 @@ */ package com.google.protobuf.gradle; +import groovy.transform.CompileStatic; import org.gradle.api.Project; import org.gradle.api.file.FileTree; import org.gradle.api.internal.file.FileOperations; import javax.inject.Inject; +@CompileStatic public interface ArchiveActionFacade { FileTree zipTree(Object path); FileTree tarTree(Object path); + @CompileStatic class ProjectBased implements ArchiveActionFacade { private final Project project; @@ -59,6 +62,7 @@ public FileTree tarTree(Object path) { } } + @CompileStatic abstract class ServiceBased implements ArchiveActionFacade { // TODO Use public ArchiveOperations from Gradle 6.6 instead diff --git a/src/main/groovy/com/google/protobuf/gradle/CopyActionFacade.java b/src/main/groovy/com/google/protobuf/gradle/CopyActionFacade.java index 41e8e770..804f3aa7 100644 --- a/src/main/groovy/com/google/protobuf/gradle/CopyActionFacade.java +++ b/src/main/groovy/com/google/protobuf/gradle/CopyActionFacade.java @@ -29,6 +29,7 @@ */ package com.google.protobuf.gradle; +import groovy.transform.CompileStatic; import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.file.CopySpec; @@ -42,9 +43,11 @@ * {@link org.gradle.api.file.FileSystemOperations} if available (Gradle 6.0+) or {@link org.gradle.api.Project#copy} if * the version of Gradle is below 6.0. */ +@CompileStatic interface CopyActionFacade { WorkResult copy(Action var1); + @CompileStatic class ProjectBased implements CopyActionFacade { private final Project project; @@ -58,6 +61,7 @@ public WorkResult copy(Action action) { } } + @CompileStatic abstract class FileSystemOperationsBased implements CopyActionFacade { @Inject public abstract FileSystemOperations getFileSystemOperations(); diff --git a/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy b/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy index 13256367..6927ce95 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy @@ -28,7 +28,7 @@ */ package com.google.protobuf.gradle -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic import org.gradle.api.Named /** @@ -37,7 +37,7 @@ import org.gradle.api.Named * configured, the plugin should try to run the executable from system search * path. */ -@CompileDynamic +@CompileStatic class ExecutableLocator implements Named { private final String name diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy index 0a5c0397..92a0c3c9 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy @@ -29,7 +29,7 @@ */ package com.google.protobuf.gradle -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic import org.gradle.api.Project import org.gradle.api.internal.file.FileResolver import org.gradle.util.ConfigureUtil @@ -37,7 +37,7 @@ import org.gradle.util.ConfigureUtil /** * Adds the protobuf {} block as a property of the project. */ -@CompileDynamic +@CompileStatic class ProtobufConvention { ProtobufConvention(Project project, FileResolver fileResolver) { protobuf = new ProtobufConfigurator(project, fileResolver) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy index fc65bba2..9db9aad5 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy @@ -30,7 +30,7 @@ package com.google.protobuf.gradle import com.google.common.base.Preconditions -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic import org.gradle.api.DefaultTask import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DuplicatesStrategy @@ -54,7 +54,7 @@ import javax.inject.Inject /** * Extracts proto files from a dependency configuration. */ -@CompileDynamic +@CompileStatic abstract class ProtobufExtract extends DefaultTask { /** @@ -171,7 +171,7 @@ abstract class ProtobufExtract extends DefaultTask { } else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) { protoInputs.add(archiveFacade.zipTree(file.path).matching(protoFilter)) } else if (file.path.endsWith('.aar')) { - FileCollection zipTree = archiveFacade.zipTree(file.path).filter { it.path.endsWith('.jar') } + FileCollection zipTree = archiveFacade.zipTree(file.path).filter { File it -> it.path.endsWith('.jar') } zipTree.each { entry -> protoInputs.add(archiveFacade.zipTree(entry).matching(protoFilter)) } diff --git a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy index d3113ed1..ff7cf903 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy @@ -28,7 +28,8 @@ */ package com.google.protobuf.gradle -import groovy.transform.CompileDynamic +import com.google.gradle.osdetector.OsDetector +import groovy.transform.CompileStatic import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.artifacts.Configuration @@ -38,12 +39,12 @@ import org.gradle.api.file.FileCollection /** * Holds locations of all external executables, i.e., protoc and plugins. */ -@CompileDynamic +@CompileStatic class ToolsLocator { private final Project project - private final ExecutableLocator protoc - private final NamedDomainObjectContainer plugins + final ExecutableLocator protoc + final NamedDomainObjectContainer plugins static List artifactParts(String artifactCoordinate) { String artifact @@ -53,11 +54,14 @@ class ToolsLocator { String version String classifier - (artifact, extension) = artifactCoordinate.tokenize('@') + List artifactCoordinateTokenized = artifactCoordinate.tokenize('@') + (artifact, extension) = [artifactCoordinateTokenized[0], artifactCoordinateTokenized[1]] if (extension == null && artifactCoordinate.endsWith('@')) { extension = '' } - (group, name, version, classifier) = artifact.tokenize(':') + List artifactTokenized = artifact.tokenize(':') + (group, name, version, classifier) = + [artifactTokenized[0], artifactTokenized[1], artifactTokenized[2], artifactTokenized[3]] return [group, name, version, classifier, extension] } @@ -92,19 +96,20 @@ class ToolsLocator { void registerDependencyWithTasks(ExecutableLocator locator, Collection protoTasks) { // create a project configuration dependency for the artifact - Configuration config = project.configurations.create("protobufToolsLocator_${locator.name}") { - visible = false - transitive = false - extendsFrom = [] + Configuration config = project.configurations.create("protobufToolsLocator_${locator.name}") { Configuration conf -> + conf.visible = false + conf.transitive = false } String groupId, artifact, version, classifier, extension - (groupId, artifact, version, classifier, extension) = artifactParts(locator.artifact) + OsDetector osdetector = project.extensions.getByName("osdetector") as OsDetector + List parts = artifactParts(locator.artifact) + (groupId, artifact, version, classifier, extension) = [parts[0], parts[1], parts[2], parts[3], parts[4]] Map notation = [ - group:groupId, - name:artifact, - version:version, - classifier:classifier ?: project.osdetector.classifier, - ext:extension ?: 'exe', + group:groupId, + name:artifact, + version:version, + classifier:classifier ?: osdetector.classifier, + ext:extension ?: 'exe', ] Dependency dep = project.dependencies.add(config.name, notation) FileCollection artifactFiles = config.fileCollection(dep) diff --git a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy index 80c84681..2c6e1a3f 100644 --- a/src/main/groovy/com/google/protobuf/gradle/Utils.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/Utils.groovy @@ -29,7 +29,7 @@ package com.google.protobuf.gradle import com.google.common.base.Preconditions -import groovy.transform.CompileDynamic +import groovy.transform.CompileStatic import org.apache.commons.lang.StringUtils import org.gradle.api.GradleException import org.gradle.api.Project @@ -43,7 +43,7 @@ import java.util.regex.Matcher /** * Utility classes. */ -@CompileDynamic +@CompileStatic class Utils { /** * Returns the conventional name of a configuration for a sourceSet