aboutsummaryrefslogtreecommitdiff
path: root/value/src
diff options
context:
space:
mode:
authoremcmanus <emcmanus@google.com>2019-09-26 13:12:10 -0700
committerDavid P. Baker <dpb@google.com>2019-09-27 14:16:56 -0400
commit6dfa04e3b636a5cf0ea3ec263b4fc991244f6fcb (patch)
treede481340c9467a476f97e6c4701a5c619240d925 /value/src
parent39662804c334d11cd3caecc5d056759fbfd74b1a (diff)
downloadauto-6dfa04e3b636a5cf0ea3ec263b4fc991244f6fcb.tar.gz
Allow @AutoValue getters to define properties that are not valid Java identifiers.
For example, get1st() defines a property called 1st. This is consistent with JavaBeans, freaky as it is. RELNOTES=Allow @AutoValue getters to define properties that are not valid Java identifiers, for example get1st(). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=271415092
Diffstat (limited to 'value/src')
-rw-r--r--value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java24
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java12
2 files changed, 34 insertions, 2 deletions
diff --git a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
index 41b45018..04d9218e 100644
--- a/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
+++ b/value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java
@@ -178,6 +178,30 @@ public class AutoValueTest {
}
@AutoValue
+ abstract static class StrangeGetters {
+ abstract int get1st();
+ abstract int get_1st(); // by default we'll use _1st where identifiers are needed, so foil that.
+
+ @AutoValue.Builder
+ abstract static class Builder {
+ abstract Builder set1st(int x);
+ abstract Builder set_1st(int x);
+ abstract StrangeGetters build();
+ }
+
+ static Builder builder() {
+ return new AutoValue_AutoValueTest_StrangeGetters.Builder();
+ }
+ }
+
+ @Test
+ public void testStrangeGetters() {
+ StrangeGetters instance = StrangeGetters.builder().set1st(17).set_1st(23).build();
+ String expectedString = omitIdentifiers ? "{17, 23}" : "StrangeGetters{1st=17, _1st=23}";
+ assertThat(instance.toString()).isEqualTo(expectedString);
+ }
+
+ @AutoValue
abstract static class GettersAndConcreteNonGetters {
abstract int getFoo();
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
index 1418c737..2987fcf9 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
@@ -633,13 +633,21 @@ abstract class AutoValueOrOneOfProcessor extends AbstractProcessor {
*/
static void fixReservedIdentifiers(Map<?, String> methodToIdentifier) {
for (Map.Entry<?, String> entry : methodToIdentifier.entrySet()) {
- if (SourceVersion.isKeyword(entry.getValue())) {
- entry.setValue(disambiguate(entry.getValue(), methodToIdentifier.values()));
+ String name = entry.getValue();
+ if (SourceVersion.isKeyword(name) || !Character.isJavaIdentifierStart(name.codePointAt(0))) {
+ entry.setValue(disambiguate(name, methodToIdentifier.values()));
}
}
}
private static String disambiguate(String name, Collection<String> existingNames) {
+ if (!Character.isJavaIdentifierStart(name.codePointAt(0))) {
+ // You've defined a getter called get1st(). What were you thinking?
+ name = "_" + name;
+ if (!existingNames.contains(name)) {
+ return name;
+ }
+ }
for (int i = 0; ; i++) {
String candidate = name + i;
if (!existingNames.contains(candidate)) {