All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eg.google.gson.internal.Excluder Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.eg.google.gson.internal;

import com.eg.google.gson.ExclusionStrategy;
import com.eg.google.gson.FieldAttributes;
import com.eg.google.gson.Gson;
import com.eg.google.gson.TypeAdapter;
import com.eg.google.gson.TypeAdapterFactory;
import com.eg.google.gson.annotations.Expose;
import com.eg.google.gson.annotations.Since;
import com.eg.google.gson.annotations.Until;
import com.eg.google.gson.reflect.TypeToken;
import com.eg.google.gson.stream.JsonReader;
import com.eg.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;

public final class Excluder implements TypeAdapterFactory, Cloneable {
    private static final double IGNORE_VERSIONS = -1.0D;
    public static final Excluder DEFAULT = new Excluder();
    private double version = -1.0D;
    private int modifiers = 136;
    private boolean serializeInnerClasses = true;
    private boolean requireExpose;
    private List serializationStrategies = Collections.emptyList();
    private List deserializationStrategies = Collections.emptyList();

    public Excluder() {
    }

    protected Excluder clone() {
        try {
            return (Excluder)super.clone();
        } catch (CloneNotSupportedException var2) {
            throw new AssertionError();
        }
    }

    public Excluder withVersion(double ignoreVersionsAfter) {
        Excluder result = this.clone();
        result.version = ignoreVersionsAfter;
        return result;
    }

    public Excluder withModifiers(int... modifiers) {
        Excluder result = this.clone();
        result.modifiers = 0;
        int[] arr$ = modifiers;
        int len$ = modifiers.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            int modifier = arr$[i$];
            result.modifiers |= modifier;
        }

        return result;
    }

    public Excluder disableInnerClassSerialization() {
        Excluder result = this.clone();
        result.serializeInnerClasses = false;
        return result;
    }

    public Excluder excludeFieldsWithoutExposeAnnotation() {
        Excluder result = this.clone();
        result.requireExpose = true;
        return result;
    }

    public Excluder withExclusionStrategy(ExclusionStrategy exclusionStrategy, boolean serialization, boolean deserialization) {
        Excluder result = this.clone();
        if(serialization) {
            result.serializationStrategies = new ArrayList(this.serializationStrategies);
            result.serializationStrategies.add(exclusionStrategy);
        }

        if(deserialization) {
            result.deserializationStrategies = new ArrayList(this.deserializationStrategies);
            result.deserializationStrategies.add(exclusionStrategy);
        }

        return result;
    }

    public  TypeAdapter create(final Gson gson, final TypeToken type) {
        Class rawType = type.getRawType();
        final boolean skipSerialize = this.excludeClass(rawType, true);
        final boolean skipDeserialize = this.excludeClass(rawType, false);
        return !skipSerialize && !skipDeserialize?null:new TypeAdapter() {
            private TypeAdapter delegate;

            public T read(JsonReader in) throws IOException {
                if(skipDeserialize) {
                    in.skipValue();
                    return null;
                } else {
                    return this.delegate().read(in);
                }
            }

            public void write(JsonWriter out, T value) throws IOException {
                if(skipSerialize) {
                    out.nullValue();
                } else {
                    this.delegate().write(out, value);
                }
            }

            private TypeAdapter delegate() {
                TypeAdapter d = this.delegate;
                return d != null?d:(this.delegate = gson.getDelegateAdapter(Excluder.this, type));
            }
        };
    }

    public boolean excludeField(Field field, boolean serialize) {
        if((this.modifiers & field.getModifiers()) != 0) {
            return true;
        } else if(this.version != -1.0D && !this.isValidVersion((Since)field.getAnnotation(Since.class), (Until)field.getAnnotation(Until.class))) {
            return true;
        } else if(field.isSynthetic()) {
            return true;
        } else {
            if(this.requireExpose) {
                label69: {
                    Expose list = (Expose)field.getAnnotation(Expose.class);
                    if(list != null) {
                        if(serialize) {
                            if(list.serialize()) {
                                break label69;
                            }
                        } else if(list.deserialize()) {
                            break label69;
                        }
                    }

                    return true;
                }
            }

            if(!this.serializeInnerClasses && this.isInnerClass(field.getType())) {
                return true;
            } else if(this.isAnonymousOrLocal(field.getType())) {
                return true;
            } else {
                List list1 = serialize?this.serializationStrategies:this.deserializationStrategies;
                if(!list1.isEmpty()) {
                    FieldAttributes fieldAttributes = new FieldAttributes(field);
                    Iterator i$ = list1.iterator();

                    while(i$.hasNext()) {
                        ExclusionStrategy exclusionStrategy = (ExclusionStrategy)i$.next();
                        if(exclusionStrategy.shouldSkipField(fieldAttributes)) {
                            return true;
                        }
                    }
                }

                return false;
            }
        }
    }

    public boolean excludeClass(Class clazz, boolean serialize) {
        if(this.version != -1.0D && !this.isValidVersion((Since)clazz.getAnnotation(Since.class), (Until)clazz.getAnnotation(Until.class))) {
            return true;
        } else if(!this.serializeInnerClasses && this.isInnerClass(clazz)) {
            return true;
        } else if(this.isAnonymousOrLocal(clazz)) {
            return true;
        } else {
            List list = serialize?this.serializationStrategies:this.deserializationStrategies;
            Iterator i$ = list.iterator();

            ExclusionStrategy exclusionStrategy;
            do {
                if(!i$.hasNext()) {
                    return false;
                }

                exclusionStrategy = (ExclusionStrategy)i$.next();
            } while(!exclusionStrategy.shouldSkipClass(clazz));

            return true;
        }
    }

    private boolean isAnonymousOrLocal(Class clazz) {
        return !Enum.class.isAssignableFrom(clazz) && (clazz.isAnonymousClass() || clazz.isLocalClass());
    }

    private boolean isInnerClass(Class clazz) {
        return clazz.isMemberClass() && !this.isStatic(clazz);
    }

    private boolean isStatic(Class clazz) {
        return (clazz.getModifiers() & 8) != 0;
    }

    private boolean isValidVersion(Since since, Until until) {
        return this.isValidSince(since) && this.isValidUntil(until);
    }

    private boolean isValidSince(Since annotation) {
        if(annotation != null) {
            double annotationVersion = annotation.value();
            if(annotationVersion > this.version) {
                return false;
            }
        }

        return true;
    }

    private boolean isValidUntil(Until annotation) {
        if(annotation != null) {
            double annotationVersion = annotation.value();
            if(annotationVersion <= this.version) {
                return false;
            }
        }

        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy