2011-09-11 09:04:56 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2012-08-15 16:58:26 +02:00
|
|
|
import java.lang.reflect.ParameterizedType;
|
2011-09-11 09:04:56 +02:00
|
|
|
import java.lang.reflect.Type;
|
2016-04-27 06:37:28 +02:00
|
|
|
import java.util.ArrayDeque;
|
2011-09-11 09:04:56 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2013-04-04 23:15:50 +02:00
|
|
|
import java.util.EnumSet;
|
2011-09-11 09:04:56 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Queue;
|
|
|
|
import java.util.Set;
|
2012-10-11 05:15:49 +02:00
|
|
|
import java.util.SortedMap;
|
2011-09-11 09:04:56 +02:00
|
|
|
import java.util.SortedSet;
|
2012-10-11 05:15:49 +02:00
|
|
|
import java.util.TreeMap;
|
2011-09-11 09:04:56 +02:00
|
|
|
import java.util.TreeSet;
|
2015-11-05 06:25:15 +01:00
|
|
|
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;
|
2021-11-09 16:16:35 +01:00
|
|
|
import com.google.gson.internal.reflect.ReflectionHelper;
|
2015-11-05 06:25:15 +01:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-09-11 09:04:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a function that can construct an instance of a requested type.
|
|
|
|
*/
|
|
|
|
public final class ConstructorConstructor {
|
2011-11-23 07:16:55 +01:00
|
|
|
private final Map<Type, InstanceCreator<?>> instanceCreators;
|
2021-12-31 00:08:18 +01:00
|
|
|
private final boolean useJdkUnsafe;
|
2011-09-11 09:04:56 +02:00
|
|
|
|
2021-12-31 00:08:18 +01:00
|
|
|
public ConstructorConstructor(Map<Type, InstanceCreator<?>> instanceCreators, boolean useJdkUnsafe) {
|
2011-09-11 09:04:56 +02:00
|
|
|
this.instanceCreators = instanceCreators;
|
2021-12-31 00:08:18 +01:00
|
|
|
this.useJdkUnsafe = useJdkUnsafe;
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
|
2012-01-01 14:42:44 +01:00
|
|
|
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
|
2011-09-11 09:04:56 +02:00
|
|
|
final Type type = typeToken.getType();
|
|
|
|
final Class<? super T> rawType = typeToken.getRawType();
|
|
|
|
|
|
|
|
// first try an instance creator
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") // types must agree
|
2012-10-24 22:40:44 +02:00
|
|
|
final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
|
|
|
|
if (typeCreator != null) {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2012-10-24 22:40:44 +02:00
|
|
|
return typeCreator.createInstance(type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next try raw type match for instance creators
|
|
|
|
@SuppressWarnings("unchecked") // types must agree
|
|
|
|
final InstanceCreator<T> rawTypeCreator =
|
|
|
|
(InstanceCreator<T>) instanceCreators.get(rawType);
|
|
|
|
if (rawTypeCreator != null) {
|
2011-09-11 09:04:56 +02:00
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2012-10-24 22:40:44 +02:00
|
|
|
return rawTypeCreator.createInstance(type);
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
|
|
|
|
if (defaultConstructor != null) {
|
|
|
|
return defaultConstructor;
|
|
|
|
}
|
|
|
|
|
2012-08-15 16:58:26 +02:00
|
|
|
ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType);
|
2011-09-11 09:04:56 +02:00
|
|
|
if (defaultImplementation != null) {
|
|
|
|
return defaultImplementation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally try unsafe
|
2021-12-31 00:08:18 +01:00
|
|
|
return newUnsafeAllocator(rawType);
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private <T> ObjectConstructor<T> newDefaultConstructor(Class<? super T> rawType) {
|
2021-11-09 16:16:35 +01:00
|
|
|
final Constructor<? super T> constructor;
|
2011-09-11 09:04:56 +02:00
|
|
|
try {
|
2021-11-09 16:16:35 +01:00
|
|
|
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.
|
|
|
|
*/
|
2011-09-11 09:04:56 +02:00
|
|
|
return new ObjectConstructor<T>() {
|
2021-11-09 16:16:35 +01:00
|
|
|
@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);
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-11-09 16:16:35 +01:00
|
|
|
|
|
|
|
return new ObjectConstructor<T>() {
|
|
|
|
@Override public T construct() {
|
|
|
|
try {
|
2021-12-31 00:08:18 +01:00
|
|
|
@SuppressWarnings("unchecked") // T is the same raw type as is requested
|
|
|
|
T newInstance = (T) constructor.newInstance();
|
|
|
|
return newInstance;
|
2021-11-09 16:16:35 +01:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors for common interface types like Map and List and their
|
2015-09-27 12:31:27 +02:00
|
|
|
* subtypes.
|
2011-09-11 09:04:56 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked") // use runtime checks to guarantee that 'T' is what it is
|
2012-08-15 16:58:26 +02:00
|
|
|
private <T> ObjectConstructor<T> newDefaultImplementationConstructor(
|
2013-04-04 23:15:50 +02:00
|
|
|
final Type type, Class<? super T> rawType) {
|
2011-09-11 09:04:56 +02:00
|
|
|
if (Collection.class.isAssignableFrom(rawType)) {
|
|
|
|
if (SortedSet.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2011-09-11 09:04:56 +02:00
|
|
|
return (T) new TreeSet<Object>();
|
|
|
|
}
|
|
|
|
};
|
2013-04-04 23:15:50 +02:00
|
|
|
} else if (EnumSet.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
|
|
|
@SuppressWarnings("rawtypes")
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2013-04-04 23:15:50 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-09-11 09:04:56 +02:00
|
|
|
} else if (Set.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2011-09-11 09:04:56 +02:00
|
|
|
return (T) new LinkedHashSet<Object>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if (Queue.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2016-04-27 06:37:28 +02:00
|
|
|
return (T) new ArrayDeque<Object>();
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2011-09-11 09:04:56 +02:00
|
|
|
return (T) new ArrayList<Object>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Map.class.isAssignableFrom(rawType)) {
|
2015-11-05 06:25:15 +01:00
|
|
|
if (ConcurrentNavigableMap.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
|
|
|
@Override public T construct() {
|
|
|
|
return (T) new ConcurrentSkipListMap<Object, Object>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if (ConcurrentMap.class.isAssignableFrom(rawType)) {
|
|
|
|
return new ObjectConstructor<T>() {
|
|
|
|
@Override public T construct() {
|
|
|
|
return (T) new ConcurrentHashMap<Object, Object>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if (SortedMap.class.isAssignableFrom(rawType)) {
|
2012-08-15 16:58:26 +02:00
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2012-10-11 05:15:49 +02:00
|
|
|
return (T) new TreeMap<Object, Object>();
|
2012-08-15 16:58:26 +02:00
|
|
|
}
|
|
|
|
};
|
2012-10-11 05:15:49 +02:00
|
|
|
} else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom(
|
|
|
|
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) {
|
2012-08-15 16:58:26 +02:00
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2012-08-15 16:58:26 +02:00
|
|
|
return (T) new LinkedHashMap<Object, Object>();
|
|
|
|
}
|
|
|
|
};
|
2012-10-11 05:15:49 +02:00
|
|
|
} else {
|
|
|
|
return new ObjectConstructor<T>() {
|
2015-11-05 06:25:15 +01:00
|
|
|
@Override public T construct() {
|
2013-05-03 18:11:16 +02:00
|
|
|
return (T) new LinkedTreeMap<String, Object>();
|
2012-10-11 05:15:49 +02:00
|
|
|
}
|
|
|
|
};
|
2012-08-15 16:58:26 +02:00
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-31 00:08:18 +01:00
|
|
|
private <T> ObjectConstructor<T> newUnsafeAllocator(final Class<? super T> rawType) {
|
|
|
|
if (useJdkUnsafe) {
|
|
|
|
return new ObjectConstructor<T>() {
|
|
|
|
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);
|
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
2021-12-31 00:08:18 +01:00
|
|
|
};
|
|
|
|
} 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<T>() {
|
|
|
|
@Override public T construct() {
|
|
|
|
throw new JsonIOException(exceptionMessage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-09-11 09:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override public String toString() {
|
|
|
|
return instanceCreators.toString();
|
|
|
|
}
|
|
|
|
}
|