首页

关于google-gson源码包中Primitives类实现了对类属性的作用域进行处理说明

标签:google,gson,Primitives,作用域     发布时间:2018-05-16   

一、前言

关于google-gson源码包中com.google.gson.Primitives类作用域处理类,进行类型判断isPrimitive、作用域处理转换wrap及还原unwrap,详情参见源码说明。

二、源码说明

package com.google.gson;@b@@b@import com.google.gson.internal..Gson.Preconditions;@b@import java.lang.reflect.Type;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@final class Primitives@b@{@b@  private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;@b@  private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;@b@@b@  private static void add(Map<Class<?>, Class<?>> forward, Map<Class<?>, Class<?>> backward, Class<?> key, Class<?> value)@b@  {@b@    forward.put(key, value);@b@    backward.put(value, key);@b@  }@b@@b@  public static boolean isPrimitive(Type type)@b@  {@b@    return PRIMITIVE_TO_WRAPPER_TYPE.containsKey(type);@b@  }@b@@b@  public static boolean isWrapperType(Class<?> type)@b@  {@b@    return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(.Gson.Preconditions.checkNotNull(type));@b@  }@b@@b@  public static <T> Class<T> wrap(Class<T> type)@b@  {@b@    Class wrapped = (Class)PRIMITIVE_TO_WRAPPER_TYPE.get(.Gson.Preconditions.checkNotNull(type));@b@@b@    return ((wrapped == null) ? type : wrapped);@b@  }@b@@b@  public static <T> Class<T> unwrap(Class<T> type)@b@  {@b@    Class unwrapped = (Class)WRAPPER_TO_PRIMITIVE_TYPE.get(.Gson.Preconditions.checkNotNull(type));@b@@b@    return ((unwrapped == null) ? type : unwrapped);@b@  }@b@@b@  static@b@  {@b@    Map primToWrap = new HashMap(16);@b@    Map wrapToPrim = new HashMap(16);@b@@b@    add(primToWrap, wrapToPrim, Boolean.TYPE, Boolean.class);@b@    add(primToWrap, wrapToPrim, Byte.TYPE, Byte.class);@b@    add(primToWrap, wrapToPrim, Character.TYPE, Character.class);@b@    add(primToWrap, wrapToPrim, Double.TYPE, Double.class);@b@    add(primToWrap, wrapToPrim, Float.TYPE, Float.class);@b@    add(primToWrap, wrapToPrim, Integer.TYPE, Integer.class);@b@    add(primToWrap, wrapToPrim, Long.TYPE, Long.class);@b@    add(primToWrap, wrapToPrim, Short.TYPE, Short.class);@b@    add(primToWrap, wrapToPrim, Void.TYPE, Void.class);@b@@b@    PRIMITIVE_TO_WRAPPER_TYPE = Collections.unmodifiableMap(primToWrap);@b@    WRAPPER_TO_PRIMITIVE_TYPE = Collections.unmodifiableMap(wrapToPrim);@b@  }@b@}