Skip to content

Commit 9cc04ec

Browse files
eamonnmcmanusronshapiro
authored andcommitted
Ensure that GwtSerialization support works in the presence of AutoValue extensions.
RELNOTES=GwtSerialization support now works in the presence of AutoValue extensions. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=205880303
1 parent 588bc05 commit 9cc04ec

File tree

3 files changed

+146
-7
lines changed

3 files changed

+146
-7
lines changed

value/src/it/functional/pom.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
<artifactId>auto-value</artifactId>
4444
<version>${project.version}</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.google.auto.service</groupId>
48+
<artifactId>auto-service</artifactId>
49+
<version>1.0-rc4</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.google.guava</groupId>
53+
<artifactId>guava</artifactId>
54+
<version>23.5-jre</version>
55+
</dependency>
4656
<dependency>
4757
<groupId>com.google.code.findbugs</groupId>
4858
<artifactId>jsr305</artifactId>
@@ -60,12 +70,6 @@
6070
<version>4.12</version>
6171
<scope>test</scope>
6272
</dependency>
63-
<dependency>
64-
<groupId>com.google.guava</groupId>
65-
<artifactId>guava</artifactId>
66-
<version>23.5-jre</version>
67-
<scope>test</scope>
68-
</dependency>
6973
<dependency>
7074
<groupId>com.google.guava</groupId>
7175
<artifactId>guava-testlib</artifactId>
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (C) 2018 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value.gwt;
17+
18+
import static java.util.stream.Collectors.joining;
19+
20+
import com.google.auto.service.AutoService;
21+
import com.google.auto.value.extension.AutoValueExtension;
22+
import com.google.common.base.Joiner;
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.common.collect.ImmutableMap;
25+
import com.google.escapevelocity.Template;
26+
import java.io.IOException;
27+
import java.io.StringReader;
28+
import java.util.List;
29+
import javax.lang.model.element.TypeElement;
30+
import javax.lang.model.element.TypeParameterElement;
31+
import javax.lang.model.type.TypeMirror;
32+
33+
/**
34+
* An AutoValue extension that generates a subclass that does nothing useful.
35+
*/
36+
@AutoService(AutoValueExtension.class)
37+
public class EmptyExtension extends AutoValueExtension {
38+
// TODO(emcmanus): it is way too difficult to write a trivial extension. Problems we have here:
39+
// (1) We have to generate a constructor that calls the superclass constructor, which means
40+
// declaring the appropriate constructor parameters and then forwarding them to a super
41+
// call.
42+
// (2) We have to avoid generating variable names that are keywords (we append $ here
43+
// to avoid that).
44+
// (3) We have to concoct appropriate type parameter strings, for example
45+
// final class AutoValue_Foo<K extends Comparable<K>, V> extends $AutoValue_Foo<K, V>.
46+
// These problems show up with the template approach here, but also using JavaPoet as the
47+
// Memoize extension does.
48+
private static final ImmutableList<String> TEMPLATE_LINES =
49+
ImmutableList.of(
50+
"package $package;",
51+
"\n",
52+
"#if ($isFinal) final #end class ${className}${formalTypes}"
53+
+ " extends ${classToExtend}${actualTypes} {\n",
54+
" ${className}(",
55+
" #foreach ($property in $properties.keySet())",
56+
" $properties[$property].returnType ${property}$ #if ($foreach.hasNext) , #end",
57+
" #end",
58+
" ) {",
59+
" super(",
60+
" #foreach ($property in $properties.keySet())",
61+
" ${property}$ #if ($foreach.hasNext) , #end",
62+
" #end",
63+
" );",
64+
" }",
65+
"}");
66+
67+
@Override
68+
public boolean applicable(Context context) {
69+
return true;
70+
}
71+
72+
@Override
73+
public String generateClass(
74+
Context context, String className, String classToExtend, boolean isFinal) {
75+
String templateString = Joiner.on('\n').join(TEMPLATE_LINES);
76+
StringReader templateReader = new StringReader(templateString);
77+
Template template;
78+
try {
79+
template = Template.parseFrom(templateReader);
80+
} catch (IOException e) {
81+
throw new RuntimeException(e);
82+
}
83+
TypeElement autoValueClass = context.autoValueClass();
84+
ImmutableMap<String, Object> vars =
85+
ImmutableMap.<String, Object>builder()
86+
.put("package", context.packageName())
87+
.put("className", className)
88+
.put("classToExtend", classToExtend)
89+
.put("isFinal", isFinal)
90+
.put("properties", context.properties())
91+
.put("formalTypes", formalTypeParametersString(autoValueClass))
92+
.put("actualTypes", actualTypeParametersString(autoValueClass))
93+
.build();
94+
return template.evaluate(vars);
95+
}
96+
97+
private static String actualTypeParametersString(TypeElement type) {
98+
List<? extends TypeParameterElement> typeParameters = type.getTypeParameters();
99+
if (typeParameters.isEmpty()) {
100+
return "";
101+
}
102+
return typeParameters
103+
.stream()
104+
.map(e -> e.getSimpleName().toString())
105+
.collect(joining(", ", "<", ">"));
106+
}
107+
108+
private static String formalTypeParametersString(TypeElement type) {
109+
List<? extends TypeParameterElement> typeParameters = type.getTypeParameters();
110+
if (typeParameters.isEmpty()) {
111+
return "";
112+
}
113+
StringBuilder sb = new StringBuilder("<");
114+
String sep = "";
115+
for (TypeParameterElement typeParameter : typeParameters) {
116+
sb.append(sep);
117+
sep = ", ";
118+
appendTypeParameterWithBounds(typeParameter, sb);
119+
}
120+
return sb.append(">").toString();
121+
}
122+
123+
private static void appendTypeParameterWithBounds(
124+
TypeParameterElement typeParameter, StringBuilder sb) {
125+
sb.append(typeParameter.getSimpleName());
126+
String sep = " extends ";
127+
for (TypeMirror bound : typeParameter.getBounds()) {
128+
if (!bound.toString().equals("java.lang.Object")) {
129+
sb.append(sep);
130+
sep = " & ";
131+
sb.append(bound);
132+
}
133+
}
134+
}
135+
}

value/src/main/java/com/google/auto/value/processor/GwtSerialization.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void maybeWriteGwtSerializer(AutoValueTemplateVars autoVars) {
8383
if (shouldWriteGwtSerializer()) {
8484
GwtTemplateVars vars = new GwtTemplateVars();
8585
vars.pkg = autoVars.pkg;
86-
vars.subclass = autoVars.subclass;
86+
vars.subclass = autoVars.finalSubclass;
8787
vars.formalTypes = autoVars.formalTypes;
8888
vars.actualTypes = autoVars.actualTypes;
8989
vars.useBuilder = !autoVars.builderTypeName.isEmpty();

0 commit comments

Comments
 (0)