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

io.gsonfire.postprocessors.MethodInvokerPostProcessor Maven / Gradle / Ivy

Go to download

A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!

There is a newer version: 1.9.0
Show newest version
package io.gsonfire.postprocessors;

import io.gsonfire.PostProcessor;
import io.gsonfire.annotations.ExposeMethodResult;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.gsonfire.gson.FireExclusionStrategy;
import io.gsonfire.gson.FireExclusionStrategyComposite;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @autor: julio
 */
public class MethodInvokerPostProcessor implements PostProcessor {

    private static Map methodMap = new ConcurrentHashMap();

    private final FireExclusionStrategy serializationExclusionStrategy;

    public MethodInvokerPostProcessor() {
        this(new FireExclusionStrategyComposite());
    }

    public MethodInvokerPostProcessor(FireExclusionStrategy serializationExclusionStrategy) {
        this.serializationExclusionStrategy = serializationExclusionStrategy;
    }

    @Override
    public void postDeserialize(T result, JsonElement src, Gson gson) {
        //nothing here
    }

    @Override
    public void postSerialize(JsonElement result, T src, Gson gson) {
        if(result.isJsonObject()){
            JsonObject jsonObject = result.getAsJsonObject();
            for(MappedMethod m: getMappedMethods((Class) src.getClass())){
                if(!serializationExclusionStrategy.shouldSkipMethod(m)) {
                    try {
                        if (m.getConflictResolutionStrategy() == ExposeMethodResult.ConflictResolutionStrategy.OVERWRITE || (m.getConflictResolutionStrategy() == ExposeMethodResult.ConflictResolutionStrategy.SKIP && !jsonObject.has(m.getSerializedName()))) {
                            Object value = m.getMethod().invoke(src);
                            jsonObject.add(m.getSerializedName(), gson.toJsonTree(value));
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private synchronized MappedMethod[] getMappedMethods(Class clazz){
        MappedMethod[] methods = methodMap.get(clazz);
        if(methods == null){
            List methodList = new ArrayList();

            //public members
            for(Method m: this.getAllMethods(clazz)){
                if(m.isAnnotationPresent(ExposeMethodResult.class)){

                    if(m.getParameterTypes().length > 0){
                        throw new InvalidParameterException("The methods annotated with ExposeMethodResult should have no arguments");
                    }

                    ExposeMethodResult exposeMethodResult = m.getAnnotation(ExposeMethodResult.class);

                    m.setAccessible(true);
                    MappedMethod mm = new MappedMethod(m, exposeMethodResult.value(), exposeMethodResult.conflictResolution());
                    methodList.add(mm);
                }
            }

            methods = new MappedMethod[methodList.size()];
            methodList.toArray(methods);
            methodMap.put(clazz, methods);
        }
        return methods;
    }

    private Collection getAllMethods(Class clazz){
        HashSet allMethods = new HashSet();

        if(clazz == null || clazz == Object.class){
            return allMethods;
        }

        //Add methods declared in the class
        for(Method m: clazz.getDeclaredMethods()){
            allMethods.add(m);
        }

        //Add methods from super class
        allMethods.addAll(getAllMethods(clazz.getSuperclass()));

        //Add methods from interfaces
        for(Class interfaceClass: clazz.getInterfaces()){
            allMethods.addAll(getAllMethods(interfaceClass));
        }

        return allMethods;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy