diff options
author | Éamonn McManus <emcmanus@google.com> | 2021-04-12 11:22:06 -0700 |
---|---|---|
committer | Google Java Core Libraries <java-libraries-firehose+copybara@google.com> | 2021-04-12 11:22:40 -0700 |
commit | 754827459bd2eb8a36a11af77c887817e0215760 (patch) | |
tree | 22bc69b22fc7165ca23cf5bb07263db43201ef35 | |
parent | 27bc525531a1928fadef173ded1d0855a9c4ebe8 (diff) | |
download | auto-754827459bd2eb8a36a11af77c887817e0215760.tar.gz |
Handle AutoBuilder parameter names that are reserved words in Java.
Obviously Java code can't have such parameter names, but Kotlin code might, for example.
RELNOTES=n/a
PiperOrigin-RevId: 368044975
3 files changed, 19 insertions, 9 deletions
diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderKotlinTest.java b/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderKotlinTest.java index 42177bb6..a1c47474 100644 --- a/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderKotlinTest.java +++ b/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderKotlinTest.java @@ -29,18 +29,18 @@ public final class AutoBuilderKotlinTest { return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataBuilder(); } - abstract KotlinDataBuilder setAnInt(int x); + abstract KotlinDataBuilder setInt(int x); - abstract KotlinDataBuilder setAString(String x); + abstract KotlinDataBuilder setString(String x); abstract KotlinData build(); } @Test public void simpleKotlin() { - KotlinData x = KotlinDataBuilder.builder().setAnInt(23).setAString("skidoo").build(); - assertThat(x.getAnInt()).isEqualTo(23); - assertThat(x.getAString()).isEqualTo("skidoo"); + KotlinData x = KotlinDataBuilder.builder().setInt(23).setString("skidoo").build(); + assertThat(x.getInt()).isEqualTo(23); + assertThat(x.getString()).isEqualTo("skidoo"); } @AutoBuilder(ofClass = KotlinDataWithNullable.class) diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/KotlinData.kt b/value/src/it/functional/src/test/java/com/google/auto/value/KotlinData.kt index 8906c25b..f3318890 100644 --- a/value/src/it/functional/src/test/java/com/google/auto/value/KotlinData.kt +++ b/value/src/it/functional/src/test/java/com/google/auto/value/KotlinData.kt @@ -15,7 +15,7 @@ */ package com.google.auto.value -data class KotlinData(val anInt: Int, val aString: String) +data class KotlinData(val int: Int, val string: String) data class KotlinDataWithNullable(val anInt: Int?, val aString: String?) diff --git a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java index bce7f49f..b2310875 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoBuilderProcessor.java @@ -23,6 +23,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableSet.toImmutableSet; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toMap; import static javax.lang.model.util.ElementFilter.constructorsIn; import static javax.lang.model.util.ElementFilter.methodsIn; @@ -39,6 +40,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.lang.reflect.Field; import java.util.List; +import java.util.Map; import java.util.NavigableSet; import java.util.Optional; import java.util.Set; @@ -144,13 +146,21 @@ public class AutoBuilderProcessor extends AutoValueishProcessor { } private ImmutableSet<Property> propertySet(ExecutableElement executable) { - return executable.getParameters().stream().map(this::newProperty).collect(toImmutableSet()); + // Fix any parameter names that are reserved words in Java. Java source code can't have + // such parameter names, but Kotlin code might, for example. + Map<VariableElement, String> identifiers = + executable.getParameters().stream() + .collect(toMap(v -> v, v -> v.getSimpleName().toString())); + fixReservedIdentifiers(identifiers); + return executable.getParameters().stream() + .map(v -> newProperty(v, identifiers.get(v))) + .collect(toImmutableSet()); } - private Property newProperty(VariableElement var) { + private Property newProperty(VariableElement var, String identifier) { String name = var.getSimpleName().toString(); TypeMirror type = var.asType(); - return new Property(name, name, TypeEncoder.encode(type), type, Optional.empty()); + return new Property(name, identifier, TypeEncoder.encode(type), type, Optional.empty()); } private ExecutableElement findExecutable( |