예제 #1
0
  public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
    final Type type = typeToken.getType();
    Class<? super T> rawType = typeToken.getRawType();

    final InstanceCreator<T> typeCreator = (InstanceCreator) this.instanceCreators.get(type);
    if (typeCreator != null) {
      new ObjectConstructor() {
        public T construct() {
          return (T) typeCreator.createInstance(type);
        }
      };
    }
    final InstanceCreator<T> rawTypeCreator = (InstanceCreator) this.instanceCreators.get(rawType);
    if (rawTypeCreator != null) {
      new ObjectConstructor() {
        public T construct() {
          return (T) rawTypeCreator.createInstance(type);
        }
      };
    }
    ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
    if (defaultConstructor != null) {
      return defaultConstructor;
    }
    ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType);
    if (defaultImplementation != null) {
      return defaultImplementation;
    }
    return newUnsafeAllocator(type, rawType);
  }
예제 #2
0
 private <T> ObjectConstructor<T> newDefaultImplementationConstructor(
     final Type type, Class<? super T> rawType) {
   if (Collection.class.isAssignableFrom(rawType)) {
     if (SortedSet.class.isAssignableFrom(rawType)) {
       new ObjectConstructor() {
         public T construct() {
           return new TreeSet();
         }
       };
     }
     if (EnumSet.class.isAssignableFrom(rawType)) {
       new ObjectConstructor() {
         public T construct() {
           if ((type instanceof ParameterizedType)) {
             Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
             if ((elementType instanceof Class)) {
               return EnumSet.noneOf((Class) elementType);
             }
             throw new JsonIOException("Invalid EnumSet type: " + type.toString());
           }
           throw new JsonIOException("Invalid EnumSet type: " + type.toString());
         }
       };
     }
     if (Set.class.isAssignableFrom(rawType)) {
       new ObjectConstructor() {
         public T construct() {
           return new LinkedHashSet();
         }
       };
     }
     if (Queue.class.isAssignableFrom(rawType)) {
       new ObjectConstructor() {
         public T construct() {
           return new LinkedList();
         }
       };
     }
     new ObjectConstructor() {
       public T construct() {
         return new ArrayList();
       }
     };
   }
   if (Map.class.isAssignableFrom(rawType)) {
     if (SortedMap.class.isAssignableFrom(rawType)) {
       new ObjectConstructor() {
         public T construct() {
           return new TreeMap();
         }
       };
     }
     if (((type instanceof ParameterizedType))
         && (!String.class.isAssignableFrom(
             TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0])
                 .getRawType()))) {
       new ObjectConstructor() {
         public T construct() {
           return new LinkedHashMap();
         }
       };
     }
     new ObjectConstructor() {
       public T construct() {
         return new LinkedTreeMap();
       }
     };
   }
   return null;
 }