Skip to content

Commit c9278a1

Browse files
Migrate AutoValue to Records (#1415)
**A description about what and why you are contributing:** Migrate AutoValue to Java Records and for some place direct replacement as there are some strict guidelines to use Records **The issue number:** #1400 Fixes #1400 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Modernized several internal data types to Java records and simplified construction patterns for maintainability. * Removed some annotation-processor libraries and bumped static-analysis tool versions. * **Tests** * Consolidated test sources by inlining test cases and removed separate test-data files. * **Behavior** * Static-analysis warnings for generated code may now be reported where they were previously suppressed. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Manu Sridharan <msridhar@gmail.com>
1 parent 2e04fb5 commit c9278a1

File tree

11 files changed

+195
-156
lines changed

11 files changed

+195
-156
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ subprojects { project ->
8484
"-parameters",
8585
]
8686
options.errorprone {
87-
// disable warnings in generated code; AutoValue code fails UnnecessaryParentheses check
88-
disableWarningsInGeneratedCode = true
8987

9088
// https://github.com/uber/NullAway/issues/1398
9189
check("MisformattedTestData", CheckSeverity.OFF)

gradle/libs.versions.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ check-framework = "3.53.0"
44
support = "27.1.1"
55
wala = "1.6.12"
66
commons-cli = "1.4"
7-
auto-value = "1.11.1"
87
auto-service = "1.1.1"
98
google-java-format = "1.30.0"
109
android-gradle-plugin = "8.13.0"
@@ -59,8 +58,6 @@ android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref
5958
gradle-maven-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-maven-publish-plugin" }
6059

6160
# --- APT (Annotation Processing) ---
62-
auto-value = { module = "com.google.auto.value:auto-value", version.ref = "auto-value" }
63-
auto-value-annotations = { module = "com.google.auto.value:auto-value-annotations", version.ref = "auto-value" }
6461
auto-service = { module = "com.google.auto.service:auto-service", version.ref = "auto-service" }
6562
auto-service-annotations = { module = "com.google.auto.service:auto-service-annotations", version.ref = "auto-service" }
6663
jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version.ref = "jakarta-inject" }

library-model/library-model-generator/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,5 @@ plugins {
2121
dependencies {
2222
implementation libs.guava
2323

24-
compileOnly libs.auto.value.annotations
25-
annotationProcessor libs.auto.value
26-
2724
testImplementation libs.junit4
2825
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package com.uber.nullaway.libmodel;
22

3-
import com.google.auto.value.AutoValue;
43
import com.google.common.collect.ImmutableMap;
54
import com.google.common.collect.ImmutableSet;
65

76
/** A record describing the annotations associated with a java method and its arguments. */
8-
@AutoValue
9-
public abstract class MethodAnnotationsRecord {
7+
public record MethodAnnotationsRecord(
8+
ImmutableSet<String> methodAnnotations,
9+
ImmutableSet<Integer> typeParamNullableUpperbounds,
10+
ImmutableMap<Integer, ImmutableSet<String>> argumentAnnotations) {
1011

1112
public static MethodAnnotationsRecord create(
1213
ImmutableSet<String> methodAnnotations,
1314
ImmutableSet<Integer> typeParamNullableUpperbounds,
1415
ImmutableMap<Integer, ImmutableSet<String>> argumentAnnotations) {
15-
return new AutoValue_MethodAnnotationsRecord(
16+
return new MethodAnnotationsRecord(
1617
methodAnnotations, typeParamNullableUpperbounds, argumentAnnotations);
1718
}
18-
19-
abstract ImmutableSet<String> methodAnnotations();
20-
21-
abstract ImmutableSet<Integer> typeParamNullableUpperbounds();
22-
23-
abstract ImmutableMap<Integer, ImmutableSet<String>> argumentAnnotations();
2419
}

nullaway/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ configurations {
3333

3434
dependencies {
3535
compileOnly project(":annotations")
36-
compileOnly libs.auto.value.annotations
37-
annotationProcessor libs.auto.value
3836
compileOnly libs.auto.service.annotations
3937
annotationProcessor libs.auto.service
4038
// Using api following the guidance at https://jspecify.dev/docs/using#gradle

nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
package com.uber.nullaway;
2424

25-
import com.google.auto.value.AutoValue;
2625
import com.google.common.base.Joiner;
2726
import com.google.common.base.Preconditions;
2827
import com.google.common.collect.ImmutableSet;
@@ -613,11 +612,10 @@ public boolean warnOnGenericInferenceFailure() {
613612
return warnOnInferenceFailure;
614613
}
615614

616-
@AutoValue
617-
abstract static class MethodClassAndName {
615+
record MethodClassAndName(String enclosingClass, String methodName) {
618616

619617
static MethodClassAndName create(String enclosingClass, String methodName) {
620-
return new AutoValue_ErrorProneCLIFlagsConfig_MethodClassAndName(enclosingClass, methodName);
618+
return new MethodClassAndName(enclosingClass, methodName);
621619
}
622620

623621
static MethodClassAndName fromClassDotMethod(String classDotMethod) {
@@ -626,9 +624,5 @@ static MethodClassAndName fromClassDotMethod(String classDotMethod) {
626624
String className = classDotMethod.substring(0, lastDot);
627625
return MethodClassAndName.create(className, methodName);
628626
}
629-
630-
abstract String enclosingClass();
631-
632-
abstract String methodName();
633627
}
634628
}

nullaway/src/main/java/com/uber/nullaway/LibraryModels.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
package com.uber.nullaway;
2424

25-
import com.google.auto.value.AutoValue;
2625
import com.google.common.collect.ImmutableList;
2726
import com.google.common.collect.ImmutableMap;
2827
import com.google.common.collect.ImmutableSet;
@@ -325,15 +324,10 @@ public String toString() {
325324
}
326325

327326
/** Representation of a field as a qualified class name + a field name */
328-
@AutoValue
329-
abstract class FieldRef {
330-
331-
public abstract String getEnclosingClassName();
332-
333-
public abstract String getFieldName();
327+
record FieldRef(String enclosingClassName, String fieldName) {
334328

335329
public static FieldRef fieldRef(String enclosingClass, String fieldName) {
336-
return new AutoValue_LibraryModels_FieldRef(enclosingClass, fieldName);
330+
return new FieldRef(enclosingClass, fieldName);
337331
}
338332
}
339333
}

nullaway/src/main/java/com/uber/nullaway/NullAway.java

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import static java.lang.annotation.ElementType.TYPE_USE;
3434

3535
import com.google.auto.service.AutoService;
36-
import com.google.auto.value.AutoValue;
3736
import com.google.common.base.Verify;
3837
import com.google.common.collect.ImmutableList;
3938
import com.google.common.collect.ImmutableMultimap;
@@ -2782,9 +2781,45 @@ public void setComputedNullness(ExpressionTree e, Nullness nullness) {
27822781
computedNullnessMap.put(e, nullness);
27832782
}
27842783

2785-
@AutoValue
2786-
abstract static class FieldInitEntities {
2784+
/**
2785+
* Aggregates all entities involved in field initialization for a class.
2786+
*
2787+
* <p>This record captures information needed to reason about initialization of {@code @NonNull}
2788+
* fields, including initializer blocks, constructors, and initializer methods, both
2789+
* instance-level and static.
2790+
*
2791+
* <p>The data is extracted from a class symbol and used during NullAway analysis to verify that
2792+
* all required fields are properly initialized.
2793+
*
2794+
* @param classSymbol the symbol of the class being analyzed
2795+
* @param nonnullInstanceFields {@code @NonNull} instance fields not directly initialized at
2796+
* declaration
2797+
* @param nonnullStaticFields {@code @NonNull} static fields not directly initialized at
2798+
* declaration
2799+
* @param instanceInitializerBlocks instance initializer blocks, in source order
2800+
* @param staticInitializerBlocks static initializer blocks, in source order
2801+
* @param constructors constructors declared in the class
2802+
* @param instanceInitializerMethods non-static initializer methods, including those annotated
2803+
* with {@code @Initializer} or specified via NullAway options
2804+
* @param staticInitializerMethods static initializer methods, including those annotated with
2805+
* {@code @Initializer} or specified via NullAway options
2806+
*/
2807+
record FieldInitEntities(
2808+
Symbol.ClassSymbol classSymbol,
2809+
ImmutableSet<Symbol> nonnullInstanceFields,
2810+
ImmutableSet<Symbol> nonnullStaticFields,
2811+
ImmutableList<BlockTree> instanceInitializerBlocks,
2812+
ImmutableList<BlockTree> staticInitializerBlocks,
2813+
ImmutableSet<MethodTree> constructors,
2814+
ImmutableSet<MethodTree> instanceInitializerMethods,
2815+
ImmutableSet<MethodTree> staticInitializerMethods) {
27872816

2817+
/**
2818+
* Creates an immutable {@link FieldInitEntities} instance from mutable inputs.
2819+
*
2820+
* <p>This factory exists for convenience when collecting data incrementally. The returned
2821+
* record is fully immutable.
2822+
*/
27882823
static FieldInitEntities create(
27892824
Symbol.ClassSymbol classSymbol,
27902825
Set<Symbol> nonnullInstanceFields,
@@ -2794,7 +2829,7 @@ static FieldInitEntities create(
27942829
Set<MethodTree> constructors,
27952830
Set<MethodTree> instanceInitializerMethods,
27962831
Set<MethodTree> staticInitializerMethods) {
2797-
return new AutoValue_NullAway_FieldInitEntities(
2832+
return new FieldInitEntities(
27982833
classSymbol,
27992834
ImmutableSet.copyOf(nonnullInstanceFields),
28002835
ImmutableSet.copyOf(nonnullStaticFields),
@@ -2804,48 +2839,5 @@ static FieldInitEntities create(
28042839
ImmutableSet.copyOf(instanceInitializerMethods),
28052840
ImmutableSet.copyOf(staticInitializerMethods));
28062841
}
2807-
2808-
/** Returns symbol for class. */
2809-
abstract Symbol.ClassSymbol classSymbol();
2810-
2811-
/**
2812-
* Returns <code>@NonNull</code> instance fields that are not directly initialized at
2813-
* declaration.
2814-
*/
2815-
abstract ImmutableSet<Symbol> nonnullInstanceFields();
2816-
2817-
/**
2818-
* Returns <code>@NonNull</code> static fields that are not directly initialized at declaration.
2819-
*/
2820-
abstract ImmutableSet<Symbol> nonnullStaticFields();
2821-
2822-
/**
2823-
* Returns the list of instance initializer blocks (e.g. blocks of the form `class X { { //Code
2824-
* } } ), in the order in which they appear in the class.
2825-
*/
2826-
abstract ImmutableList<BlockTree> instanceInitializerBlocks();
2827-
2828-
/**
2829-
* Returns the list of static initializer blocks (e.g. blocks of the form `class X { static {
2830-
* //Code } } ), in the order in which they appear in the class.
2831-
*/
2832-
abstract ImmutableList<BlockTree> staticInitializerBlocks();
2833-
2834-
/** Returns constructors in the class. */
2835-
abstract ImmutableSet<MethodTree> constructors();
2836-
2837-
/**
2838-
* Returns the list of non-static (instance) initializer methods. This includes methods
2839-
* annotated @Initializer, as well as those specified by -XepOpt:NullAway:KnownInitializers or
2840-
* annotated with annotations passed to -XepOpt:NullAway:CustomInitializerAnnotations.
2841-
*/
2842-
abstract ImmutableSet<MethodTree> instanceInitializerMethods();
2843-
2844-
/**
2845-
* Returns the list of static initializer methods. This includes static methods
2846-
* annotated @Initializer, as well as those specified by -XepOpt:NullAway:KnownInitializers or
2847-
* annotated with annotations passed to -XepOpt:NullAway:CustomInitializerAnnotations.
2848-
*/
2849-
abstract ImmutableSet<MethodTree> staticInitializerMethods();
28502842
}
28512843
}

0 commit comments

Comments
 (0)