what-the-bank/sources/com/google/gson/internal/Excluder.java

213 lines
8.0 KiB
Java

package com.google.gson.internal;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/* loaded from: classes.dex */
public final class Excluder implements TypeAdapterFactory, Cloneable {
public static final Excluder DEFAULT = new Excluder();
public static final double IGNORE_VERSIONS = -1.0d;
public boolean requireExpose;
public double version = -1.0d;
public int modifiers = 136;
public boolean serializeInnerClasses = true;
public List<ExclusionStrategy> serializationStrategies = Collections.emptyList();
public List<ExclusionStrategy> deserializationStrategies = Collections.emptyList();
/* JADX INFO: Access modifiers changed from: protected */
/* renamed from: clone, reason: merged with bridge method [inline-methods] */
public final Excluder m264clone() {
try {
return (Excluder) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
public final Excluder withVersion(double d) {
Excluder m264clone = m264clone();
m264clone.version = d;
return m264clone;
}
public final Excluder withModifiers(int... iArr) {
Excluder m264clone = m264clone();
m264clone.modifiers = 0;
for (int i : iArr) {
m264clone.modifiers = i | m264clone.modifiers;
}
return m264clone;
}
public final Excluder disableInnerClassSerialization() {
Excluder m264clone = m264clone();
m264clone.serializeInnerClasses = false;
return m264clone;
}
public final Excluder excludeFieldsWithoutExposeAnnotation() {
Excluder m264clone = m264clone();
m264clone.requireExpose = true;
return m264clone;
}
public final Excluder withExclusionStrategy(ExclusionStrategy exclusionStrategy, boolean z, boolean z2) {
Excluder m264clone = m264clone();
if (z) {
ArrayList arrayList = new ArrayList(this.serializationStrategies);
m264clone.serializationStrategies = arrayList;
arrayList.add(exclusionStrategy);
}
if (z2) {
ArrayList arrayList2 = new ArrayList(this.deserializationStrategies);
m264clone.deserializationStrategies = arrayList2;
arrayList2.add(exclusionStrategy);
}
return m264clone;
}
@Override // com.google.gson.TypeAdapterFactory
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
boolean excludeClassChecks = excludeClassChecks(rawType);
boolean z = excludeClassChecks || excludeClassInStrategy(rawType, true);
boolean z2 = excludeClassChecks || excludeClassInStrategy(rawType, false);
if (z || z2) {
return new TypeAdapter<T>(this, z2, z, gson, typeToken) { // from class: com.google.gson.internal.Excluder.1
private TypeAdapter<T> delegate;
final Excluder this$0;
final Gson val$gson;
final boolean val$skipDeserialize;
final boolean val$skipSerialize;
final TypeToken val$type;
{
this.this$0 = this;
this.val$skipDeserialize = z2;
this.val$skipSerialize = z;
this.val$gson = gson;
this.val$type = typeToken;
}
@Override // com.google.gson.TypeAdapter
/* renamed from: read */
public T read2(JsonReader jsonReader) throws IOException {
if (this.val$skipDeserialize) {
jsonReader.skipValue();
return null;
}
return delegate().read2(jsonReader);
}
@Override // com.google.gson.TypeAdapter
public void write(JsonWriter jsonWriter, T t) throws IOException {
if (this.val$skipSerialize) {
jsonWriter.nullValue();
} else {
delegate().write(jsonWriter, t);
}
}
private TypeAdapter<T> delegate() {
TypeAdapter<T> typeAdapter = this.delegate;
if (typeAdapter != null) {
return typeAdapter;
}
TypeAdapter<T> delegateAdapter = this.val$gson.getDelegateAdapter(this.this$0, this.val$type);
this.delegate = delegateAdapter;
return delegateAdapter;
}
};
}
return null;
}
public final boolean excludeField(Field field, boolean z) {
Expose expose;
if ((this.modifiers & field.getModifiers()) != 0) {
return true;
}
if ((this.version != -1.0d && !isValidVersion((Since) field.getAnnotation(Since.class), (Until) field.getAnnotation(Until.class))) || field.isSynthetic()) {
return true;
}
if (this.requireExpose && ((expose = (Expose) field.getAnnotation(Expose.class)) == null || (!z ? expose.deserialize() : expose.serialize()))) {
return true;
}
if ((!this.serializeInnerClasses && isInnerClass(field.getType())) || isAnonymousOrNonStaticLocal(field.getType())) {
return true;
}
List<ExclusionStrategy> list = z ? this.serializationStrategies : this.deserializationStrategies;
if (list.isEmpty()) {
return false;
}
FieldAttributes fieldAttributes = new FieldAttributes(field);
Iterator<ExclusionStrategy> it = list.iterator();
while (it.hasNext()) {
if (it.next().shouldSkipField(fieldAttributes)) {
return true;
}
}
return false;
}
private boolean excludeClassChecks(Class<?> cls) {
if (this.version == -1.0d || isValidVersion((Since) cls.getAnnotation(Since.class), (Until) cls.getAnnotation(Until.class))) {
return (!this.serializeInnerClasses && isInnerClass(cls)) || isAnonymousOrNonStaticLocal(cls);
}
return true;
}
public final boolean excludeClass(Class<?> cls, boolean z) {
return excludeClassChecks(cls) || excludeClassInStrategy(cls, z);
}
private boolean excludeClassInStrategy(Class<?> cls, boolean z) {
Iterator<ExclusionStrategy> it = (z ? this.serializationStrategies : this.deserializationStrategies).iterator();
while (it.hasNext()) {
if (it.next().shouldSkipClass(cls)) {
return true;
}
}
return false;
}
private boolean isAnonymousOrNonStaticLocal(Class<?> cls) {
return (Enum.class.isAssignableFrom(cls) || isStatic(cls) || (!cls.isAnonymousClass() && !cls.isLocalClass())) ? false : true;
}
private boolean isInnerClass(Class<?> cls) {
return cls.isMemberClass() && !isStatic(cls);
}
private boolean isStatic(Class<?> cls) {
return (cls.getModifiers() & 8) != 0;
}
private boolean isValidVersion(Since since, Until until) {
return isValidSince(since) && isValidUntil(until);
}
private boolean isValidSince(Since since) {
return since == null || since.value() <= this.version;
}
private boolean isValidUntil(Until until) {
return until == null || until.value() > this.version;
}
}