gson-comments/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java

115 lines
3.7 KiB
Java
Raw Normal View History

2011-07-12 00:26:53 +02:00
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2011-08-03 02:25:10 +02:00
package com.google.gson.internal.bind;
2011-07-12 00:26:53 +02:00
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.ArrayList;
2011-07-12 00:26:53 +02:00
/**
* Adapt an array of objects.
*/
public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
2011-07-12 00:26:53 +02:00
Type type = typeToken.getType();
Fix error prone warnings (#2316) * Fix `OperatorPrecedence` warn in `JsonWriter#close` * Fix `ReferenceEquality` warn in `LinkedTreeMap#replaceInParent` * Fix `UnnecessaryParentheses` warn in `LinkedTreeMap#replaceInParent` * Fix `ReferenceEquality` warn in `LinkedTreeMap#hasNext` * Fix `ReferenceEquality` warn in `LinkedTreeMap#nextNode` * Adds `error_prone_annotations` to the `pom.xml` of `gson` * Fix `InlineMeSuggester` warns in `JsonParser` * Fix `UnnecessaryParentheses` warns in `ConstructorConstructor#newDefaultImplementationConstructor` * Fix `ThreadLocalUsage` warn in `Gson` * Fix `JdkObsolete` warn in `GsonBuilder` * Fix `ReferenceEquality` warn in `LazilyParsedNumber#equals` * Fix `OperatorPrecedence` warn in `TreeTypeAdapter#create` * Fix `OperatorPrecedence` warn in `ArrayTypeAdapter` * Fix `UnnecessaryParentheses` warn in `TypeAdapters` * Adds `-XepExcludedPaths` flag to ErrorProne plugin to exclude tests and proto path * Fix `ClassNewInstance` warn in `InterceptorAdapter` * Fix `ThreadLocalUsage` warn in `GraphAdapterBuilder` * Fix `JdkObsolete` warn in `GraphAdapterBuilder` * Revert "Adds `error_prone_annotations` to the `pom.xml` of `gson`" This reverts commit 14af14dfa23b46a54f4855a70ccf2b0a2cdc3e3f. * Revert "Fix `InlineMeSuggester` warns in `JsonParser`" This reverts commit 095bfd517e06510e4cc9cc6b1aac58ad9bf3038a. * Adds `@SuppressWarnings("ThreadLocalUsage")` * Fix `OperatorPrecedence` in `JsonWriter` * Revert "Fix `ReferenceEquality` warn in `LinkedTreeMap#nextNode`" This reverts commit 387746c7f7e3d0943c8f80501f5d9c3710f4862e. * Adds `@SuppressWarnings("ReferenceEquality")` * Adds `guava-testlib` to the gson `pom.xml` * `@SuppressWarnings("TruthSelfEquals")` removed to use `EqualsTester()`
2023-02-15 14:18:43 +01:00
if (!(type instanceof GenericArrayType || (type instanceof Class && ((Class<?>) type).isArray()))) {
2011-07-12 00:26:53 +02:00
return null;
}
Type componentType = $Gson$Types.getArrayComponentType(type);
2011-12-02 23:57:30 +01:00
TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
@SuppressWarnings({"unchecked", "rawtypes"})
TypeAdapter<T> arrayAdapter = new ArrayTypeAdapter(
2011-12-02 23:57:30 +01:00
gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
return arrayAdapter;
2011-07-12 00:26:53 +02:00
}
};
private final Class<E> componentType;
private final TypeAdapter<E> componentTypeAdapter;
public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class<E> componentType) {
this.componentTypeAdapter =
new TypeAdapterRuntimeTypeWrapper<>(context, componentTypeAdapter, componentType);
2011-07-12 00:26:53 +02:00
this.componentType = componentType;
}
@Override public Object read(JsonReader in) throws IOException {
2011-12-03 20:46:25 +01:00
if (in.peek() == JsonToken.NULL) {
in.nextNull();
2011-07-12 00:26:53 +02:00
return null;
}
2022-05-17 21:20:10 +02:00
if (in.isLenient() && in.peek() != JsonToken.BEGIN_ARRAY) {
// Coerce
Object array = Array.newInstance(componentType, 1);
2022-05-17 21:20:10 +02:00
Array.set(array, 0, componentTypeAdapter.read(in));
return array;
}
2011-07-12 00:26:53 +02:00
ArrayList<E> list = new ArrayList<>();
2011-12-03 20:46:25 +01:00
in.beginArray();
while (in.hasNext()) {
E instance = componentTypeAdapter.read(in);
2011-07-12 00:26:53 +02:00
list.add(instance);
}
2011-12-03 20:46:25 +01:00
in.endArray();
int size = list.size();
// Have to copy primitives one by one to primitive array
if (componentType.isPrimitive()) {
Object array = Array.newInstance(componentType, size);
for (int i = 0; i < size; i++) {
Array.set(array, i, list.get(i));
}
return array;
}
// But for Object[] can use ArrayList.toArray
else {
@SuppressWarnings("unchecked")
E[] array = (E[]) Array.newInstance(componentType, size);
return list.toArray(array);
2011-07-12 00:26:53 +02:00
}
}
2011-12-03 20:46:25 +01:00
@Override public void write(JsonWriter out, Object array) throws IOException {
2011-07-12 00:26:53 +02:00
if (array == null) {
2011-12-03 20:46:25 +01:00
out.nullValue();
2011-07-12 00:26:53 +02:00
return;
}
2011-12-03 20:46:25 +01:00
out.beginArray();
2011-07-12 00:26:53 +02:00
for (int i = 0, length = Array.getLength(array); i < length; i++) {
@SuppressWarnings("unchecked")
E value = (E) Array.get(array, i);
2011-12-03 20:46:25 +01:00
componentTypeAdapter.write(out, value);
2011-07-12 00:26:53 +02:00
}
2011-12-03 20:46:25 +01:00
out.endArray();
2011-07-12 00:26:53 +02:00
}
}