本文整理汇总了Java中com.google.gson.reflect.TypeToken.getRawType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.getRawType方法的具体用法?Java TypeToken.getRawType怎么用?Java TypeToken.getRawType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.reflect.TypeToken
的用法示例。
在下文中一共展示了TypeToken.getRawType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConstructor
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> ObjectConstructor<T> getConstructor(TypeToken<T> typeToken) {
final Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
final InstanceCreator<T> creator = (InstanceCreator) this.instanceCreators.get(type);
if (creator != null) {
return new ObjectConstructor<T>() {
public T construct() {
return creator.createInstance(type);
}
};
}
ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
if (defaultConstructor != null) {
return defaultConstructor;
}
ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(rawType);
if (defaultImplementation != null) {
return defaultImplementation;
}
return newUnsafeAllocator(type, rawType);
}
示例2: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Timestamp.class) {
return null;
}
final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
return (TypeAdapter<T>) new TypeAdapter<Timestamp>() {
@Override public Timestamp read(JsonReader in) throws IOException {
Date date = dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
@Override public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例3: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Timestamp.class) {
return null;
}
final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
return new TypeAdapter<Timestamp>() {
public Timestamp read(JsonReader in) throws IOException {
Date date = (Date) dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例4: getActualTypeArguments
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
/** creates a map that maps generic type argument names to type tokens */
private static Map<String, TypeToken> getActualTypeArguments(TypeToken<?> typeToken) {
Class<?> rawClass = typeToken.getRawType();
Type type = typeToken.getType();
TypeVariable<? extends Class<?>>[] typeParameters = rawClass.getTypeParameters();
if (typeParameters==null || !(type instanceof ParameterizedType)) {
return null;
}
Map<String, TypeToken> genericTypes = new HashMap<>();
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (int i=0; i<typeParameters.length; i++) {
String typeParameterName = typeParameters[i].getName();
TypeToken<?> actualType = TypeToken.get(actualTypeArguments[i]);
genericTypes.put(typeParameterName, actualType);
}
return genericTypes;
}
示例5: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();
TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class);
TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class);
if (Location.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new LocationTypeAdapter();
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter);
} else if (GUISignature.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter);
} else if (MaterialAndData.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new MaterialAndDataAdapter();
}
return null;
}
示例6: responseBodyConverter
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
TypeToken<?> typeToken = TypeToken.get(type);
if (typeToken.getRawType() == JSONObject.class) {
return new JsonResponseBodyConverter<JSONObject>();
} else if (typeToken.getRawType() == JSONArray.class) {
return new JsonArrayResponseBodyConverter<JSONArray>();
} else {
try {
throw new Throwable("非法类型");
} catch (Throwable throwable) {
throwable.printStackTrace();
return null;
}
}
}
示例7: typeName
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public PolymorphicTypeAdapterFactory typeName(TypeToken<?> type, String name) {
typeNames.put(type, name);
matchingTypes.add(type);
Class<?> rawClass = type.getRawType();
TypeToken<?> rawType = TypeToken.get(rawClass);
if (!rawType.equals(type)) {
matchingTypes.add(rawType);
}
return this;
}
示例8: newFactory
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == unboxed || rawType == boxed) ? typeAdapter : null;
}
public String toString() {
return "Factory[type=" + boxed.getName() + "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例9: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
Class<? super T> raw = type.getRawType();
if (!Object.class.isAssignableFrom(raw)) {
return null; // it's a primitive!
}
ObjectConstructor<T> constructor = constructorConstructor.get(type);
return new Adapter<T>(constructor, getBoundFields(gson, type, raw));
}
示例10: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if(Enum.class.isAssignableFrom(type.getRawType())) {
return (TypeAdapter<T>) new StrictEnumTypeAdapter(type.getRawType());
}
return null;
}
示例11: 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();
if (!raw.isInterface()) {
Type declaredType = type.getType();
while (raw != Object.class) {
for (Field field : raw.getDeclaredFields()) {
boolean serialize = excludeField(field, true);
boolean deserialize = excludeField(field, false);
if (serialize || deserialize) {
field.setAccessible(true);
BoundField boundField = createBoundField(context, field, getFieldName
(field), TypeToken.get(C$Gson$Types.resolve(type.getType(), raw,
field.getGenericType())), serialize, deserialize);
BoundField previous = (BoundField) result.put(boundField.name, boundField);
if (previous != null) {
throw new IllegalArgumentException(declaredType + " declares multiple" +
" JSON fields named " + previous.name);
}
}
}
type = TypeToken.get(C$Gson$Types.resolve(type.getType(), raw, raw
.getGenericSuperclass()));
raw = type.getRawType();
}
}
return result;
}
示例12: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type){
Class<T> rawType = (Class<T>)type.getRawType();
boolean hasOptionals = Arrays.stream(rawType.getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.anyMatch(field -> field.getType() == Optional.class);
return hasOptionals ? (TypeAdapter<T>)getClassAdapter(gson, type) : null;
}
示例13: newFactoryForMultipleTypes
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(final Class<TT> base, final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == base || rawType == sub) ? typeAdapter : null;
}
public String toString() {
return "Factory[type=" + base.getName() + "+" + sub.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例14: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
boolean matches = this.exactType != null ? this.exactType.equals(type) || (this
.matchRawType && this.exactType.getType() == type.getRawType()) : this
.hierarchyType.isAssignableFrom(type.getRawType());
if (matches) {
return new TreeTypeAdapter(this.serializer, this.deserializer, gson, type, this);
}
return null;
}
示例15: create
import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
Class<?> rawType = type.getRawType();
final boolean skipSerialize = excludeClass(rawType, true);
final boolean skipDeserialize = excludeClass(rawType, false);
if (!skipSerialize && !skipDeserialize) {
return null;
}
return new TypeAdapter<T>() {
/** The delegate is lazily created because it may not be needed, and creating it may fail. */
private TypeAdapter<T> delegate;
@Override public T read(JsonReader in) throws IOException {
if (skipDeserialize) {
in.skipValue();
return null;
}
return delegate().read(in);
}
@Override public void write(JsonWriter out, T value) throws IOException {
if (skipSerialize) {
out.nullValue();
return;
}
delegate().write(out, value);
}
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(Excluder.this, type));
}
};
}