Skip to content

Commit 982638c

Browse files
authored
Revert "Add FieldDescriptor support to DataEncoder. (#1608)" (#1648)
This reverts commit d0a55d4.
1 parent a4f5620 commit 982638c

File tree

18 files changed

+49
-481
lines changed

18 files changed

+49
-481
lines changed

encoders/firebase-encoders-json/api.txt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package com.google.firebase.encoders {
1515
method @NonNull public static com.google.firebase.encoders.FieldDescriptor.Builder builder(@NonNull String);
1616
method @NonNull public String getName();
1717
method @Nullable public <T extends java.lang.annotation.Annotation> T getProperty(@NonNull Class<T>);
18-
method @NonNull public static com.google.firebase.encoders.FieldDescriptor of(@NonNull String);
1918
}
2019

2120
public static final class FieldDescriptor.Builder {
@@ -27,19 +26,13 @@ package com.google.firebase.encoders {
2726
}
2827

2928
public interface ObjectEncoderContext {
30-
method @Deprecated @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, @Nullable Object) throws java.io.IOException;
31-
method @Deprecated @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, double) throws java.io.IOException;
32-
method @Deprecated @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, int) throws java.io.IOException;
33-
method @Deprecated @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, long) throws java.io.IOException;
34-
method @Deprecated @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, boolean) throws java.io.IOException;
35-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull com.google.firebase.encoders.FieldDescriptor, @Nullable Object) throws java.io.IOException;
36-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull com.google.firebase.encoders.FieldDescriptor, double) throws java.io.IOException;
37-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull com.google.firebase.encoders.FieldDescriptor, int) throws java.io.IOException;
38-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull com.google.firebase.encoders.FieldDescriptor, long) throws java.io.IOException;
39-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull com.google.firebase.encoders.FieldDescriptor, boolean) throws java.io.IOException;
29+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, @Nullable Object) throws java.io.IOException;
30+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, double) throws java.io.IOException;
31+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, int) throws java.io.IOException;
32+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, long) throws java.io.IOException;
33+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, boolean) throws java.io.IOException;
4034
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext inline(@Nullable Object) throws java.io.IOException;
4135
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext nested(@NonNull String) throws java.io.IOException;
42-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext nested(@NonNull com.google.firebase.encoders.FieldDescriptor) throws java.io.IOException;
4336
}
4437

4538
public interface ValueEncoder<T> {

encoders/firebase-encoders-json/src/json/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContext.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.Nullable;
2121
import com.google.firebase.encoders.EncodingException;
22-
import com.google.firebase.encoders.FieldDescriptor;
2322
import com.google.firebase.encoders.ObjectEncoder;
2423
import com.google.firebase.encoders.ObjectEncoderContext;
2524
import com.google.firebase.encoders.ValueEncoder;
@@ -103,38 +102,6 @@ public JsonValueObjectEncoderContext add(@NonNull String name, boolean value) th
103102
return add(value);
104103
}
105104

106-
@NonNull
107-
@Override
108-
public ObjectEncoderContext add(@NonNull FieldDescriptor field, @Nullable Object obj)
109-
throws IOException {
110-
return add(field.getName(), obj);
111-
}
112-
113-
@NonNull
114-
@Override
115-
public ObjectEncoderContext add(@NonNull FieldDescriptor field, double value) throws IOException {
116-
return add(field.getName(), value);
117-
}
118-
119-
@NonNull
120-
@Override
121-
public ObjectEncoderContext add(@NonNull FieldDescriptor field, int value) throws IOException {
122-
return add(field.getName(), value);
123-
}
124-
125-
@NonNull
126-
@Override
127-
public ObjectEncoderContext add(@NonNull FieldDescriptor field, long value) throws IOException {
128-
return add(field.getName(), value);
129-
}
130-
131-
@NonNull
132-
@Override
133-
public ObjectEncoderContext add(@NonNull FieldDescriptor field, boolean value)
134-
throws IOException {
135-
return add(field.getName(), value);
136-
}
137-
138105
@NonNull
139106
@Override
140107
public ObjectEncoderContext inline(@Nullable Object value) throws IOException {
@@ -151,12 +118,6 @@ public ObjectEncoderContext nested(@NonNull String name) throws IOException {
151118
return childContext;
152119
}
153120

154-
@NonNull
155-
@Override
156-
public ObjectEncoderContext nested(@NonNull FieldDescriptor field) throws IOException {
157-
return nested(field.getName());
158-
}
159-
160121
@NonNull
161122
@Override
162123
public JsonValueObjectEncoderContext add(@Nullable String value) throws IOException {

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/FieldDescriptor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ public <T extends Annotation> T getProperty(@NonNull Class<T> type) {
6767
return (T) properties.get(type);
6868
}
6969

70-
@NonNull
71-
public static FieldDescriptor of(@NonNull String name) {
72-
return new FieldDescriptor(name, Collections.emptyMap());
73-
}
74-
7570
@NonNull
7671
public static Builder builder(@NonNull String name) {
7772
return new Builder(name);

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/ObjectEncoderContext.java

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -35,99 +35,37 @@ public interface ObjectEncoderContext {
3535
*
3636
* <p>{@code obj} can be an array. If the elements of the array are primitive types, they will be
3737
* directly encoded. Otherwise, the matching {@code Encoder} registered for the type will be used.
38-
* In this case, the value of the entry will be an encoded array obtained by sequentially applying
38+
* In this case, the value of the entry will be an encoded array obtained by sequencially applying
3939
* the encoder to each element of the array. Nested arrays are supported.
4040
*
4141
* <p>{@code obj} can be a {@link java.util.Collection}. The matching {@code Encoder} registered
4242
* for the type contained within the collection will be used. In this case, the value of the entry
43-
* will be an encoded array obtained by sequentially applying the encoder to each element of the
43+
* will be an encoded array obtained by sequencially applying the encoder to each element of the
4444
* array. Nested collections are supported.
4545
*
4646
* <p>If {@code obj} does not match any of the criteria above, or if there's no matching {@code
4747
* Encoder} for the type, an {@code EncodingException} will be thrown. Also, any exceptions thrown
4848
* by the encoders will be propagated.
49-
*
50-
* @deprecated Use {@link #add(FieldDescriptor, Object)} instead.
5149
*/
52-
@Deprecated
5350
@NonNull
5451
ObjectEncoderContext add(@NonNull String name, @Nullable Object obj) throws IOException;
5552

56-
/**
57-
* Add an entry with {@code name} mapped to the encoded primitive type of {@code value}.
58-
*
59-
* @deprecated Use {@link #add(FieldDescriptor, double)} instead.
60-
*/
61-
@Deprecated
53+
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
6254
@NonNull
6355
ObjectEncoderContext add(@NonNull String name, double value) throws IOException;
6456

65-
/**
66-
* Add an entry with {@code name} mapped to the encoded primitive type of {@code value}.
67-
*
68-
* @deprecated Use {@link #add(FieldDescriptor, double)} instead.
69-
*/
70-
@Deprecated
57+
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
7158
@NonNull
7259
ObjectEncoderContext add(@NonNull String name, int value) throws IOException;
7360

74-
/**
75-
* Add an entry with {@code name} mapped to the encoded primitive type of {@code value}.
76-
*
77-
* @deprecated Use {@link #add(FieldDescriptor, double)} instead.
78-
*/
79-
@Deprecated
61+
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
8062
@NonNull
8163
ObjectEncoderContext add(@NonNull String name, long value) throws IOException;
8264

83-
/**
84-
* Add an entry with {@code name} mapped to the encoded primitive type of {@code value}.
85-
*
86-
* @deprecated Use {@link #add(FieldDescriptor, double)} instead.
87-
*/
88-
@Deprecated
65+
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
8966
@NonNull
9067
ObjectEncoderContext add(@NonNull String name, boolean value) throws IOException;
9168

92-
/**
93-
* Add an entry with {@code field} mapped to the encoded version of {@code obj}.
94-
*
95-
* <p>{@code obj} can be a regular type with a matching {@code Encoder} registered. In this case,
96-
* the value of the entry will be the encoded version of {@code obj}.
97-
*
98-
* <p>{@code obj} can be an array. If the elements of the array are primitive types, they will be
99-
* directly encoded. Otherwise, the matching {@code Encoder} registered for the type will be used.
100-
* In this case, the value of the entry will be an encoded array obtained by sequentially applying
101-
* the encoder to each element of the array. Nested arrays are supported.
102-
*
103-
* <p>{@code obj} can be a {@link java.util.Collection}. The matching {@code Encoder} registered
104-
* for the type contained within the collection will be used. In this case, the value of the entry
105-
* will be an encoded array obtained by sequentially applying the encoder to each element of the
106-
* array. Nested collections are supported.
107-
*
108-
* <p>If {@code obj} does not match any of the criteria above, or if there's no matching {@code
109-
* Encoder} for the type, an {@code EncodingException} will be thrown. Also, any exceptions thrown
110-
* by the encoders will be propagated.
111-
*/
112-
@NonNull
113-
ObjectEncoderContext add(@NonNull FieldDescriptor field, @Nullable Object obj) throws IOException;
114-
115-
/** Add an entry with {@code field} mapped to the encoded primitive type of {@code value}. */
116-
@NonNull
117-
ObjectEncoderContext add(@NonNull FieldDescriptor field, double value) throws IOException;
118-
119-
/** Add an entry with {@code field} mapped to the encoded primitive type of {@code value}. */
120-
@NonNull
121-
ObjectEncoderContext add(@NonNull FieldDescriptor field, int value) throws IOException;
122-
123-
/** Add an entry with {@code field} mapped to the encoded primitive type of {@code value}. */
124-
@NonNull
125-
ObjectEncoderContext add(@NonNull FieldDescriptor field, long value) throws IOException;
126-
127-
/** Add an entry with {@code field} mapped to the encoded primitive type of {@code value}. */
128-
@NonNull
129-
ObjectEncoderContext add(@NonNull FieldDescriptor field, boolean value) throws IOException;
130-
13169
/**
13270
* Encodes a given object inline in current context.
13371
*
@@ -171,28 +109,4 @@ public interface ObjectEncoderContext {
171109
*/
172110
@NonNull
173111
ObjectEncoderContext nested(@NonNull String name) throws IOException;
174-
175-
/**
176-
* Begin a nested JSON object.
177-
*
178-
* <p>Unlike {@code add()} methods, this method returns a new "child" context that's used to
179-
* populate the nested JSON object. This context can only be used until the parent context is
180-
* mutated by calls to {@code add()} or {@code nested()}, violating this will result in a {@link
181-
* IllegalStateException}.
182-
*
183-
* <p>Nesting can be arbitrarily deep.
184-
*
185-
* <p>Example:
186-
*
187-
* <pre>{@code
188-
* ctx.add("key", "value");
189-
* ObjectEncoderContext nested = ctx.nested("nested");
190-
* nested.add("key", "value");
191-
*
192-
* // After this call the above nested context is invalid.
193-
* ctx.add("anotherKey", 1);
194-
* }</pre>
195-
*/
196-
@NonNull
197-
ObjectEncoderContext nested(@NonNull FieldDescriptor field) throws IOException;
198112
}

encoders/firebase-encoders-processor/src/main/java/com/google/firebase/encoders/processor/EncodableProcessor.java

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
import com.google.auto.service.AutoService;
1919
import com.google.auto.value.AutoValue;
2020
import com.google.firebase.encoders.annotations.Encodable;
21-
import com.google.firebase.encoders.processor.getters.AnnotationDescriptor;
22-
import com.google.firebase.encoders.processor.getters.AnnotationProperty;
2321
import com.google.firebase.encoders.processor.getters.Getter;
2422
import com.google.firebase.encoders.processor.getters.GetterFactory;
2523
import com.squareup.javapoet.ClassName;
26-
import com.squareup.javapoet.CodeBlock;
2724
import com.squareup.javapoet.FieldSpec;
2825
import com.squareup.javapoet.JavaFile;
2926
import com.squareup.javapoet.MethodSpec;
@@ -60,7 +57,6 @@
6057
@SupportedSourceVersion(SourceVersion.RELEASE_8)
6158
public class EncodableProcessor extends AbstractProcessor {
6259

63-
private static final String CODEGEN_VERSION = "2";
6460
static final String ENCODABLE_ANNOTATION = "com.google.firebase.encoders.annotations.Encodable";
6561
private Elements elements;
6662
private Types types;
@@ -88,11 +84,6 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
8884
}
8985

9086
private void processClass(Element element) {
91-
if (types.isAssignable(
92-
element.asType(), elements.getTypeElement("java.lang.annotation.Annotation").asType())) {
93-
return;
94-
}
95-
9687
// generates class of the following shape:
9788
//
9889
// public class AutoFooEncoder implements Configurator {
@@ -116,7 +107,7 @@ private void processClass(Element element) {
116107
Modifier.PUBLIC,
117108
Modifier.STATIC,
118109
Modifier.FINAL)
119-
.initializer(CODEGEN_VERSION)
110+
.initializer("1")
120111
.build())
121112
.addField(
122113
FieldSpec.builder(
@@ -268,47 +259,12 @@ public VisitResult<Encoder> visit(TypeMirror type) {
268259
.addAnnotation(Override.class);
269260

270261
Set<TypeMirror> result = new LinkedHashSet<>();
271-
Set<FieldSpec> descriptorFields = new LinkedHashSet<>();
272-
ClassName fieldDescriptor = ClassName.get("com.google.firebase.encoders", "FieldDescriptor");
273262
for (Getter getter : getterFactory.allGetters((DeclaredType) type)) {
274263
result.addAll(getTypesToVisit(getter.getUnderlyingType()));
275264
if (getter.inline()) {
276265
methodBuilder.addCode("ctx.inline(value.$L);\n", getter.expression());
277266
} else {
278-
CodeBlock.Builder codeBuilder;
279-
if (getter.annotationDescriptors().isEmpty()) {
280-
codeBuilder = CodeBlock.builder().add("$T.of($S)", fieldDescriptor, getter.name());
281-
} else {
282-
codeBuilder =
283-
CodeBlock.builder()
284-
.add("$T.builder($S)\n", fieldDescriptor, getter.name())
285-
.indent()
286-
.indent();
287-
for (AnnotationDescriptor desc : getter.annotationDescriptors()) {
288-
ClassName annotationBuilder =
289-
builderName(
290-
ClassName.get((TypeElement) desc.type().getAnnotationType().asElement()));
291-
codeBuilder.add(".withProperty($T.builder()\n", annotationBuilder);
292-
for (AnnotationProperty property : desc.properties()) {
293-
codeBuilder.add("$>.$L($L)\n$<", property.name(), property.value());
294-
}
295-
codeBuilder.add("$>.build())\n$<");
296-
}
297-
codeBuilder.add(".build()").unindent().unindent();
298-
}
299-
descriptorFields.add(
300-
FieldSpec.builder(
301-
fieldDescriptor,
302-
getter.name().toUpperCase() + "_DESCRIPTOR",
303-
Modifier.PRIVATE,
304-
Modifier.FINAL,
305-
Modifier.STATIC)
306-
.initializer(codeBuilder.build())
307-
.build());
308-
methodBuilder.addCode(
309-
"ctx.add($L_DESCRIPTOR, value.$L);\n",
310-
getter.name().toUpperCase(),
311-
getter.expression());
267+
methodBuilder.addCode("ctx.add($S, value.$L);\n", getter.name(), getter.expression());
312268
}
313269
}
314270

@@ -325,25 +281,12 @@ public VisitResult<Encoder> visit(TypeMirror type) {
325281
FieldSpec.builder(className, "INSTANCE", Modifier.FINAL, Modifier.STATIC)
326282
.initializer("new $T()", className)
327283
.build())
328-
.addFields(descriptorFields)
329284
.addMethod(methodBuilder.build())
330285
.build();
331286
encoded.put(types.erasure(type), encoder);
332287
return VisitResult.of(result, Encoder.create(types.erasure(type), encoder));
333288
}
334289

335-
private ClassName builderName(ClassName annotation) {
336-
return ClassName.get(annotation.packageName(), compositeName(annotation));
337-
}
338-
339-
private String compositeName(ClassName annotation) {
340-
ClassName parentName = annotation.enclosingClassName();
341-
if (parentName == null) {
342-
return "At" + annotation.simpleName();
343-
}
344-
return compositeName(parentName) + annotation.simpleName();
345-
}
346-
347290
private Set<TypeMirror> getTypesToVisit(TypeMirror type) {
348291
TypeMirror date = elements.getTypeElement("java.util.Date").asType();
349292
TypeMirror enumType = types.erasure(elements.getTypeElement("java.lang.Enum").asType());

encoders/firebase-encoders-processor/src/main/java/com/google/firebase/encoders/processor/annotations/AnnotationBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.squareup.javapoet.ParameterizedTypeName;
2222
import com.squareup.javapoet.TypeName;
2323
import com.squareup.javapoet.TypeSpec;
24+
import com.squareup.javapoet.TypeSpec.Builder;
2425
import com.squareup.javapoet.WildcardTypeName;
2526
import java.lang.annotation.Annotation;
2627
import java.util.ArrayList;
@@ -79,7 +80,7 @@ public static TypeSpec generate(TypeElement element) {
7980

8081
private static TypeSpec createBuilder(
8182
ClassName builderName, ClassName annotationName, AnnotationImpl annotationImpl) {
82-
TypeSpec.Builder annotationBuilder =
83+
Builder annotationBuilder =
8384
TypeSpec.classBuilder(builderName)
8485
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
8586
.addType(annotationImpl.typeSpec);
@@ -171,7 +172,7 @@ private static String compositeName(ClassName annotation) {
171172

172173
private static AnnotationImpl createAnnotationImpl(TypeElement annotation) {
173174
String implName = annotation.getSimpleName().toString() + "Impl";
174-
TypeSpec.Builder annotationImpl =
175+
Builder annotationImpl =
175176
TypeSpec.classBuilder(implName)
176177
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
177178
.addSuperinterface(TypeName.get(annotation.asType()))

encoders/firebase-encoders-processor/src/main/java/com/google/firebase/encoders/processor/annotations/ToStringMethod.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.squareup.javapoet.ClassName;
1818
import com.squareup.javapoet.CodeBlock;
1919
import com.squareup.javapoet.MethodSpec;
20+
import com.squareup.javapoet.MethodSpec.Builder;
2021
import java.util.List;
2122
import javax.lang.model.element.ExecutableElement;
2223
import javax.lang.model.element.Modifier;
@@ -26,7 +27,7 @@
2627
final class ToStringMethod {
2728
static MethodSpec generate(TypeElement element) {
2829
ClassName.get(element).reflectionName();
29-
MethodSpec.Builder result =
30+
Builder result =
3031
MethodSpec.methodBuilder("toString")
3132
.addModifiers(Modifier.PUBLIC)
3233
.returns(String.class)

0 commit comments

Comments
 (0)