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: #565 (comment)
  • Loading branch information
rougsig authored and ejona86 committed Jun 24, 2022
1 parent e7f9ab4 commit 20d4d51
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 25 deletions.
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,7 @@
*/
package com.google.protobuf.gradle

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

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,7 +54,7 @@ import javax.inject.Inject
/**
* Extracts proto files from a dependency configuration.
*/
@CompileDynamic
@CompileStatic
abstract class ProtobufExtract extends DefaultTask {

/**
Expand Down Expand Up @@ -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))
}
Expand Down
37 changes: 21 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,14 @@ class ToolsLocator {
String version
String classifier

(artifact, extension) = artifactCoordinate.tokenize('@')
List<String> artifactCoordinateTokenized = artifactCoordinate.tokenize('@')
(artifact, extension) = [artifactCoordinateTokenized[0], artifactCoordinateTokenized[1]]
if (extension == null && artifactCoordinate.endsWith('@')) {
extension = ''
}
(group, name, version, classifier) = artifact.tokenize(':')
List<String> 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 +96,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)
OsDetector osdetector = project.extensions.getByName("osdetector") as OsDetector
List<String> 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 20d4d51

Please sign in to comment.