本文整理汇总了Java中com.google.gson.reflect.TypeToken.getType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.getType方法的具体用法?Java TypeToken.getType怎么用?Java TypeToken.getType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.reflect.TypeToken
的用法示例。
在下文中一共展示了TypeToken.getType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Map.class.isAssignableFrom(rawType)) {
return null;
}
Class<?> rawTypeOfSrc = $Gson$Types.getRawType(type);
Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(type, rawTypeOfSrc);
TypeAdapter<?> keyAdapter = getKeyAdapter(gson, keyAndValueTypes[0]);
TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(keyAndValueTypes[1]));
ObjectConstructor<T> constructor = constructorConstructor.get(typeToken);
@SuppressWarnings({"unchecked", "rawtypes"})
// we don't define a type parameter for the key or value types
TypeAdapter<T> result = new Adapter(gson, keyAndValueTypes[0], keyAdapter,
keyAndValueTypes[1], valueAdapter, constructor);
return result;
}
示例2: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!Map.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}
Type[] keyAndValueTypes = C$Gson$Types.getMapKeyAndValueTypes(type, C$Gson$Types
.getRawType(type));
return new Adapter(gson, keyAndValueTypes[0], getKeyAdapter(gson, keyAndValueTypes[0]),
keyAndValueTypes[1], gson.getAdapter(TypeToken.get(keyAndValueTypes[1])), this
.constructorConstructor.get(typeToken));
}
示例3: get
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
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) {
return new ObjectConstructor<T>() {
public T construct() {
return typeCreator.createInstance(type);
}
};
}
final InstanceCreator<T> rawTypeCreator = (InstanceCreator) this.instanceCreators.get
(rawType);
if (rawTypeCreator != null) {
return new ObjectConstructor<T>() {
public T construct() {
return 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);
}
示例4: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType) && (!(type instanceof Class) || !((Class) type).isArray())) {
return null;
}
Type componentType = C$Gson$Types.getArrayComponentType(type);
return new ArrayTypeAdapter(gson, gson.getAdapter(TypeToken.get(componentType)), C$Gson$Types.getRawType(componentType));
}
示例5: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!Map.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}
Type[] keyAndValueTypes = C$Gson$Types.getMapKeyAndValueTypes(type, C$Gson$Types.getRawType(type));
return new Adapter(gson, keyAndValueTypes[0], getKeyAdapter(gson, keyAndValueTypes[0]), keyAndValueTypes[1], gson.getAdapter(TypeToken.get(keyAndValueTypes[1])), this.constructorConstructor.getConstructor(typeToken));
}
示例6: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return null;
}
Type elementType = C$Gson$Types.getCollectionElementType(type, rawType);
return new Adapter(gson, elementType, gson.getAdapter(TypeToken.get(elementType)), this.constructorConstructor.getConstructor(typeToken));
}
示例7: getBoundFields
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) {
Map<String, BoundField> result = new LinkedHashMap<String, BoundField>();
if (raw.isInterface()) {
return result;
}
Type declaredType = type.getType();
while (raw != Object.class) {
Field[] fields = raw.getDeclaredFields();
for (Field field : fields) {
boolean serialize = excludeField(field, true);
boolean deserialize = excludeField(field, false);
if (!serialize && !deserialize) {
continue;
}
field.setAccessible(true);
Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
BoundField boundField = createBoundField(context, field, getFieldName(field),
TypeToken.get(fieldType), serialize, deserialize);
BoundField previous = result.put(boundField.name, boundField);
if (previous != null) {
throw new IllegalArgumentException(declaredType
+ " declares multiple JSON fields named " + previous.name);
}
}
type = TypeToken.get($Gson$Types.resolve(type.getType(), raw, raw.getGenericSuperclass()));
raw = type.getRawType();
}
return result;
}
示例8: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType) && (!(type instanceof Class) || !((Class)
type).isArray())) {
return null;
}
Type componentType = C$Gson$Types.getArrayComponentType(type);
return new ArrayTypeAdapter(gson, gson.getAdapter(TypeToken.get(componentType)),
C$Gson$Types.getRawType(componentType));
}
示例9: getAsParsed
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public void getAsParsed(TypeToken typeToken, ParsedRequestListener parsedRequestListener) {
this.mType = typeToken.getType();
this.mResponseType = ResponseType.PARSED;
this.mParsedRequestListener = parsedRequestListener;
ANRequestQueue.getInstance().addRequest(this);
}
示例10: getAsOkHttpResponseAndParsed
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public void getAsOkHttpResponseAndParsed(TypeToken typeToken, OkHttpResponseAndParsedRequestListener parsedRequestListener) {
this.mType = typeToken.getType();
this.mResponseType = ResponseType.PARSED;
this.mOkHttpResponseAndParsedRequestListener = parsedRequestListener;
ANRequestQueue.getInstance().addRequest(this);
}
示例11: newFactoryWithMatchRawType
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public static TypeAdapterFactory newFactoryWithMatchRawType(TypeToken<?> exactType, Object
typeAdapter) {
return new SingleTypeFactory(typeAdapter, exactType, exactType.getType() == exactType
.getRawType(), null);
}
示例12: getTargetType
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
protected Type getTargetType() {
TypeToken<Map<String, String>> typeToken = new TypeToken<Map<String, String>>() {};
return typeToken.getType();
}
示例13: newFactoryWithMatchRawType
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public static TypeAdapterFactory newFactoryWithMatchRawType(TypeToken<?> exactType, Object typeAdapter) {
return new SingleTypeFactory(typeAdapter, exactType, exactType.getType() == exactType.getRawType(), null);
}
示例14: getTargetType
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
protected Type getTargetType() {
TypeToken<List<Throwable>> typeToken = new TypeToken<List<Throwable>>() {};
return typeToken.getType();
}
示例15: get
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
final Type type = typeToken.getType();
final Class<? super T> rawType = typeToken.getRawType();
// first try an instance creator
@SuppressWarnings("unchecked") // types must agree
final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
if (typeCreator != null) {
return new ObjectConstructor<T>() {
public T construct() {
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) {
return new ObjectConstructor<T>() {
public T construct() {
return rawTypeCreator.createInstance(type);
}
};
}
ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
if (defaultConstructor != null) {
return defaultConstructor;
}
ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType);
if (defaultImplementation != null) {
return defaultImplementation;
}
// finally try unsafe
return newUnsafeAllocator(type, rawType);
}