/* * Copyright (C) 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.gson.internal; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; import com.google.gson.InstanceCreator; import com.google.gson.JsonIOException; import com.google.gson.internal.reflect.ReflectionHelper; import com.google.gson.reflect.TypeToken; /** * Returns a function that can construct an instance of a requested type. */ public final class ConstructorConstructor { private final Map> instanceCreators; private final boolean useJdkUnsafe; public ConstructorConstructor(Map> instanceCreators, boolean useJdkUnsafe) { this.instanceCreators = instanceCreators; this.useJdkUnsafe = useJdkUnsafe; } public ObjectConstructor get(TypeToken typeToken) { final Type type = typeToken.getType(); final Class rawType = typeToken.getRawType(); // first try an instance creator @SuppressWarnings("unchecked") // types must agree final InstanceCreator typeCreator = (InstanceCreator) instanceCreators.get(type); if (typeCreator != null) { return new ObjectConstructor() { @Override public T construct() { return typeCreator.createInstance(type); } }; } // Next try raw type match for instance creators @SuppressWarnings("unchecked") // types must agree final InstanceCreator rawTypeCreator = (InstanceCreator) instanceCreators.get(rawType); if (rawTypeCreator != null) { return new ObjectConstructor() { @Override public T construct() { return rawTypeCreator.createInstance(type); } }; } ObjectConstructor defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor defaultImplementation = newDefaultImplementationConstructor(type, rawType); if (defaultImplementation != null) { return defaultImplementation; } // finally try unsafe return newUnsafeAllocator(rawType); } private ObjectConstructor newDefaultConstructor(Class rawType) { final Constructor constructor; try { constructor = rawType.getDeclaredConstructor(); } catch (NoSuchMethodException e) { return null; } final String exceptionMessage = ReflectionHelper.tryMakeAccessible(constructor); if (exceptionMessage != null) { /* * Create ObjectConstructor which throws exception. * This keeps backward compatibility (compared to returning `null` which * would then choose another way of creating object). * And it supports types which are only serialized but not deserialized * (compared to directly throwing exception here), e.g. when runtime type * of object is inaccessible, but compile-time type is accessible. */ return new ObjectConstructor() { @Override public T construct() { // New exception is created every time to avoid keeping reference // to exception with potentially long stack trace, causing a // memory leak throw new JsonIOException(exceptionMessage); } }; } return new ObjectConstructor() { @Override public T construct() { try { @SuppressWarnings("unchecked") // T is the same raw type as is requested T newInstance = (T) constructor.newInstance(); return newInstance; } catch (InstantiationException e) { // TODO: JsonParseException ? throw new RuntimeException("Failed to invoke " + constructor + " with no args", e); } catch (InvocationTargetException e) { // TODO: don't wrap if cause is unchecked! // TODO: JsonParseException ? throw new RuntimeException("Failed to invoke " + constructor + " with no args", e.getTargetException()); } catch (IllegalAccessException e) { throw new AssertionError(e); } } }; } /** * Constructors for common interface types like Map and List and their * subtypes. */ @SuppressWarnings("unchecked") // use runtime checks to guarantee that 'T' is what it is private ObjectConstructor newDefaultImplementationConstructor( final Type type, Class rawType) { if (Collection.class.isAssignableFrom(rawType)) { if (SortedSet.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new TreeSet(); } }; } else if (EnumSet.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @SuppressWarnings("rawtypes") @Override public T construct() { if (type instanceof ParameterizedType) { Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (elementType instanceof Class) { return (T) EnumSet.noneOf((Class)elementType); } else { throw new JsonIOException("Invalid EnumSet type: " + type.toString()); } } else { throw new JsonIOException("Invalid EnumSet type: " + type.toString()); } } }; } else if (Set.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new LinkedHashSet(); } }; } else if (Queue.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new ArrayDeque(); } }; } else { return new ObjectConstructor() { @Override public T construct() { return (T) new ArrayList(); } }; } } if (Map.class.isAssignableFrom(rawType)) { if (ConcurrentNavigableMap.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new ConcurrentSkipListMap(); } }; } else if (ConcurrentMap.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new ConcurrentHashMap(); } }; } else if (SortedMap.class.isAssignableFrom(rawType)) { return new ObjectConstructor() { @Override public T construct() { return (T) new TreeMap(); } }; } else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom( TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) { return new ObjectConstructor() { @Override public T construct() { return (T) new LinkedHashMap(); } }; } else { return new ObjectConstructor() { @Override public T construct() { return (T) new LinkedTreeMap(); } }; } } return null; } private ObjectConstructor newUnsafeAllocator(final Class rawType) { if (useJdkUnsafe) { return new ObjectConstructor() { private final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create(); @Override public T construct() { try { @SuppressWarnings("unchecked") T newInstance = (T) unsafeAllocator.newInstance(rawType); return newInstance; } catch (Exception e) { throw new RuntimeException(("Unable to create instance of " + rawType + ". " + "Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args " + "constructor may fix this problem."), e); } } }; } else { final String exceptionMessage = "Unable to create instance of " + rawType + "; usage of JDK Unsafe " + "is disabled. Registering an InstanceCreator or a TypeAdapter for this type, adding a no-args " + "constructor, or enabling usage of JDK Unsafe may fix this problem."; return new ObjectConstructor() { @Override public T construct() { throw new JsonIOException(exceptionMessage); } }; } } @Override public String toString() { return instanceCreators.toString(); } }