Skip to content

Commit

Permalink
Easy part of CompileStatic migration
Browse files Browse the repository at this point in the history
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: google#565 (comment)
  • Loading branch information
rougsig committed Jun 23, 2022
1 parent e7f9ab4 commit 27fbd82
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 42 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation 'commons-lang:commons-lang:2.6'

testImplementation 'junit:junit:4.12'
testImplementation "com.android.tools.build:gradle:3.5.0"
testImplementation('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude module: 'groovy-all'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,6 +62,7 @@ public FileTree tarTree(Object path) {
}
}

@CompileStatic
abstract class ServiceBased implements ArchiveActionFacade {

// TODO Use public ArchiveOperations from Gradle 6.6 instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? super CopySpec> var1);

@CompileStatic
class ProjectBased implements CopyActionFacade {
private final Project project;

Expand All @@ -58,6 +61,7 @@ public WorkResult copy(Action<? super CopySpec> action) {
}
}

@CompileStatic
abstract class FileSystemOperationsBased implements CopyActionFacade {
@Inject
public abstract FileSystemOperations getFileSystemOperations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
*/
package com.google.protobuf.gradle

import groovy.transform.CompileDynamic

import groovy.transform.CompileStatic
import org.gradle.api.Named

/**
Expand All @@ -37,7 +38,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
*/
package com.google.protobuf.gradle

import static java.nio.charset.StandardCharsets.US_ASCII

import com.android.build.gradle.api.BaseVariant
import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableList
import groovy.transform.CompileDynamic
Expand All @@ -49,25 +48,14 @@ import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.*
import org.gradle.util.ConfigureUtil

import javax.annotation.Nullable
import javax.inject.Inject

import static java.nio.charset.StandardCharsets.US_ASCII

/**
* The task that compiles proto files into Java files.
*/
Expand Down Expand Up @@ -101,7 +89,7 @@ public abstract class GenerateProtoTask extends DefaultTask {
@SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable
transient private SourceSet sourceSet
@SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable
transient private Object variant
transient private BaseVariant variant
private ImmutableList<String> flavors
private String buildType
private boolean isTestVariant
Expand Down Expand Up @@ -317,7 +305,7 @@ public abstract class GenerateProtoTask extends DefaultTask {
}

@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
Object getVariant() {
BaseVariant getVariant() {
Preconditions.checkState(isAndroidProject.get(),
'variant should not be used in a Java project')
Preconditions.checkNotNull(variant, 'variant is not set')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
*/
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.api.tasks.TaskCollection
Expand All @@ -37,7 +38,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
*/
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

/**
* Adds the protobuf {} block as a property of the project.
*/
@CompileDynamic
@CompileStatic
class ProtobufConvention {
ProtobufConvention(Project project, FileResolver fileResolver) {
protobuf = new ProtobufConfigurator(project, fileResolver)
Expand Down
36 changes: 20 additions & 16 deletions src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ExecutableLocator> plugins
final ExecutableLocator protoc
final NamedDomainObjectContainer<ExecutableLocator> plugins

static List<String> artifactParts(String artifactCoordinate) {
String artifact
Expand All @@ -53,11 +54,13 @@ class ToolsLocator {
String version
String classifier

(artifact, extension) = artifactCoordinate.tokenize('@')
def artifactCoordinateTokenized = artifactCoordinate.tokenize('@')
(artifact, extension) = [artifactCoordinateTokenized[0], artifactCoordinateTokenized[1]]
if (extension == null && artifactCoordinate.endsWith('@')) {
extension = ''
}
(group, name, version, classifier) = artifact.tokenize(':')
def artifactTokenized = artifact.tokenize(':')
(group, name, version, classifier) = [artifactTokenized[0], artifactTokenized[1], artifactTokenized[2], artifactTokenized[3]]

return [group, name, version, classifier, extension]
}
Expand Down Expand Up @@ -92,19 +95,20 @@ class ToolsLocator {

void registerDependencyWithTasks(ExecutableLocator locator, Collection<GenerateProtoTask> 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)
def osdetector = project.extensions.getByName("osdetector") as OsDetector
def parts = artifactParts(locator.artifact)
(groupId, artifact, version, classifier, extension) = [parts[0], parts[1], parts[2], parts[3], parts[4]]
Map<String, String> 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)
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 27fbd82

Please sign in to comment.