Skip to content

Commit 843bd7c

Browse files
authored
feat(gapic-generator): add JSpecify Null annotations to the generator classes (#13769)
This PR continues the onboarding of the gapic-generator-java project to JSpecify null-safety. Specifically, it applies the @NullMarked annotation to all modified source and test classes in this branch to declare them non-null by default. **Annotations Applied** - @NullMarked Class Declarations: Applied at the class level to all modified source and test classes to establish a non-null by default context. - @nullable Type Annotations: Applied to specific methods, fields, and parameters to explicitly denote where null values are accepted/returned. **Automation Method** To apply the class-level annotations efficiently, we leveraged Chris's Error Prone automation tool (custom BugChecker plugin under com.google.errorprone.bugpatterns.nullness) to run AST-based refactoring and automatically insert the JSpecify @NullMarked annotations across the codebase. Verification/Testing - Please refer to this doc: https://docs.google.com/document/d/18c7NzbTpREqqb_qvLUZ1bCaMYGQQnczyo6vrVPmx60Q/edit?resourcekey=0-EOZQxK66NvVhJPF1jg-LQA&tab=t.0
1 parent f6f24d4 commit 843bd7c

183 files changed

Lines changed: 524 additions & 192 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/Main.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
2222
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
2323
import java.io.IOException;
24+
import org.jspecify.annotations.NullMarked;
25+
import org.jspecify.annotations.Nullable;
2426

27+
@NullMarked
2528
public class Main {
2629
public static void main(String[] args) throws IOException {
2730
ExtensionRegistry registry = ExtensionRegistry.newInstance();
2831
ProtoRegistry.registerAllExtensions(registry);
2932
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(System.in, registry);
30-
CodeGeneratorResponse response = Generator.generateGapic(request);
33+
@Nullable CodeGeneratorResponse response = Generator.generateGapic(request);
3134
if (response != EMPTY_RESPONSE) {
3235
response.writeTo(System.out);
3336
}

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/ProtoRegistry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.google.cloud.ExtendedOperationsProto;
2424
import com.google.longrunning.OperationsProto;
2525
import com.google.protobuf.ExtensionRegistry;
26+
import org.jspecify.annotations.NullMarked;
2627

28+
@NullMarked
2729
public class ProtoRegistry {
2830
/** Register all extensions needed to process API protofiles. */
2931
public static void registerAllExtensions(ExtensionRegistry extensionRegistry) {

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/debug/CodeGeneratorRequestDumper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
2020
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
2121
import java.io.IOException;
22+
import org.jspecify.annotations.NullMarked;
2223

2324
// Request dumper class, which dumps the CodeGeneratorRequest to a file on disk which will be
2425
// identical to the one passed to the Main class during normal execution. The dumped file then can
2526
// be used to run this gapic-generator directly (instead of relying on protoc to start the process),
2627
// which would give much greater flexibility in terms of debugging features, like attaching a
2728
// debugger, easier work with stdout and stderr etc.
29+
@NullMarked
2830
public class CodeGeneratorRequestDumper {
2931
public static void main(String[] args) throws IOException {
3032
ExtensionRegistry registry = ExtensionRegistry.newInstance();

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/debug/CodeGeneratorRequestFileToGapicMain.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
27+
import org.jspecify.annotations.NullMarked;
28+
import org.jspecify.annotations.Nullable;
2729

2830
// A generator entry point class, similar to Main but reads the CodeGeneratorRequest directly from a
2931
// file instead of relying on protoc to pipe it in.
32+
@NullMarked
3033
public class CodeGeneratorRequestFileToGapicMain {
3134
public static void main(String[] args) throws IOException {
3235
ExtensionRegistry registry = ExtensionRegistry.newInstance();
@@ -38,7 +41,7 @@ public static void main(String[] args) throws IOException {
3841
try (InputStream inputStream = new FileInputStream(inputFile);
3942
OutputStream outputStream = new FileOutputStream(outputFile)) {
4043
CodeGeneratorRequest request = CodeGeneratorRequest.parseFrom(inputStream, registry);
41-
CodeGeneratorResponse response = Generator.generateGapic(request);
44+
@Nullable CodeGeneratorResponse response = Generator.generateGapic(request);
4245
response.writeTo(outputStream);
4346
}
4447
}

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/AnnotationNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22-
import javax.annotation.Nullable;
22+
import org.jspecify.annotations.NullMarked;
23+
import org.jspecify.annotations.Nullable;
2324

25+
@NullMarked
2426
@AutoValue
2527
public abstract class AnnotationNode implements AstNode {
2628
public static AnnotationNode OVERRIDE =
@@ -34,8 +36,7 @@ private static TypeNode annotationType(Class<?> clazz) {
3436

3537
public abstract TypeNode type();
3638

37-
@Nullable
38-
public abstract List<Expr> descriptionExprs();
39+
public abstract @Nullable List<Expr> descriptionExprs();
3940

4041
@Override
4142
public void accept(AstNodeVisitor visitor) {

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/AnonymousClassExpr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.util.Arrays;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import org.jspecify.annotations.NullMarked;
2324

25+
@NullMarked
2426
@AutoValue
2527
public abstract class AnonymousClassExpr implements Expr {
2628
@Override

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/ArithmeticOperationExpr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.common.base.Preconditions;
19+
import org.jspecify.annotations.NullMarked;
1920

21+
@NullMarked
2022
@AutoValue
2123
public abstract class ArithmeticOperationExpr implements OperationExpr {
2224

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/ArrayExpr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.google.common.collect.ImmutableList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import org.jspecify.annotations.NullMarked;
2223

24+
@NullMarked
2325
@AutoValue
2426
public abstract class ArrayExpr implements Expr {
2527

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/AssignmentExpr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.common.base.Preconditions;
19+
import org.jspecify.annotations.NullMarked;
1920

21+
@NullMarked
2022
@AutoValue
2123
public abstract class AssignmentExpr implements Expr {
2224
public abstract VariableExpr variableExpr();

sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/AssignmentOperationExpr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import com.google.auto.value.AutoValue;
1818
import com.google.common.base.Preconditions;
19+
import org.jspecify.annotations.NullMarked;
1920

21+
@NullMarked
2022
@AutoValue
2123
public abstract class AssignmentOperationExpr implements OperationExpr {
2224
public abstract VariableExpr variableExpr();

0 commit comments

Comments
 (0)