|
| 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 | +} |
0 commit comments