List of usage examples for com.google.gson.reflect TypeToken getRawType
public final Class<? super T> getRawType()
From source file:org.eclipse.smarthome.storage.json.PropertiesTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings({ "unused", "unchecked" }) @Override/*w w w . j a va 2 s.c om*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!PropertiesTypeAdapter.TOKEN.equals(typeToken)) { return null; } return (TypeAdapter<T>) new PropertiesTypeAdapter(gson); }
From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractConfiguredExtraPropertiesJsonAdapterFactory.java
License:LGPL
private static Field getField(TypeToken<?> type, String fieldName) { Class<?> rawType = type.getRawType(); while (rawType != null) { try {/*from w ww .ja va 2 s . c om*/ Field field = rawType.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException ignored) { } rawType = rawType.getSuperclass(); } throw new AssertionFailure("Missing or inaccessible field " + fieldName + " on type " + type); }
From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractConfiguredExtraPropertiesJsonAdapterFactory.java
License:LGPL
private static <T> ExtraPropertyAdapter<T> getExtraPropertyAdapter(Gson gson, TypeToken<T> type) { Class<?> rawType = type.getRawType(); while (rawType != null) { for (Field field : rawType.getDeclaredFields()) { SerializeExtraProperties annotation = field.getAnnotation(SerializeExtraProperties.class); if (annotation != null) { field.setAccessible(true); return new ReflectiveExtraPropertyAdapter<>(field, gson); }/* w w w . j av a 2 s. c om*/ } rawType = rawType.getSuperclass(); } throw new AssertionFailure("Missing or inaccessible field annotated with " + SerializeExtraProperties.class + " on type " + type); }
From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractConfiguredExtraPropertiesJsonAdapterFactory.java
License:LGPL
@SuppressWarnings("unchecked") private static <T> Constructor<T> getConstructor(TypeToken<T> type) { try {/* w w w. j a v a 2 s . co m*/ return (Constructor<T>) type.getRawType().getConstructor(); } catch (NoSuchMethodException | SecurityException e) { throw new AssertionFailure("Missing or inaccessible no-arg constructor on type " + type); } }
From source file:org.immutables.gson.adapter.ExpectedSubtypesAdapter.java
License:Apache License
@Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue();/* w w w . j a v a2s.c o m*/ return; } for (int i = 0; i < subtypes.length; i++) { TypeToken<? extends T> subtype = subtypes[i]; if (subtype.getRawType().isInstance(value)) { // safe unchecked, type is checked at runtime @SuppressWarnings("unchecked") TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) adapters.get(i); typeAdapter.write(out, value); return; } } gson.toJson(value, value.getClass(), out); }
From source file:org.immutables.mongo.bson4gson.GsonCodecs.java
License:Apache License
/** * Gson Factory which gives preference to existing adapters from {@code gson} instance. However, * if type is not supported it will query {@link CodecRegistry} to create one (if possible). * * <p>This allows supporting Bson types by Gson natively (eg. for {@link org.bson.types.ObjectId}). * * @param registry existing registry which will be used if type is unknown to {@code gson}. * @return factory which delegates to {@code registry} for unknown types. *//*from ww w. j a v a2 s . co m*/ public static TypeAdapterFactory delegatingTypeAdapterFactory(final CodecRegistry registry) { Preconditions.checkNotNull(registry, "registry"); return new TypeAdapterFactory() { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { boolean hasAdapter; try { TypeAdapter<T> adapter = gson.getDelegateAdapter(this, type); hasAdapter = !isReflectiveTypeAdapter(adapter); } catch (IllegalArgumentException e) { hasAdapter = false; } if (hasAdapter) { return null; } try { @SuppressWarnings("unchecked") Codec<T> codec = (Codec<T>) registry.get(type.getRawType()); return typeAdapterFromCodec(codec); } catch (CodecConfigurationException e1) { return null; } } }; }
From source file:org.jclouds.googlecloud.config.ListPageAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> ownerType) { Type type = ownerType.getType(); if (ownerType.getRawType() != ListPage.class || !(type instanceof ParameterizedType)) { return null; }//from w w w. j a v a 2 s . c o m Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; TypeAdapter<?> itemAdapter = gson.getAdapter(TypeToken.get(elementType)); return (TypeAdapter<T>) new ListPageAdapter(itemAdapter); }
From source file:org.jclouds.json.internal.DeserializationConstructorAndReflectiveTypeAdapterFactory.java
License:Apache License
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationTarget = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationTarget == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) }/*from w w w . ja v a2 s .c o m*/ // @AutoValue is SOURCE retention, which means it cannot be looked up at runtime. // Assume abstract types built by static methods are AutoValue. if (Modifier.isAbstract(type.getRawType().getModifiers()) && deserializationTarget.isStatic()) { // Lookup the generated AutoValue class, whose fields must be read for serialization. String packageName = type.getRawType().getPackage().getName(); String autoClassName = type.getRawType().getName().replace('$', '_').replace(packageName + ".", packageName + ".AutoValue_"); try { type = (TypeToken<T>) TypeToken.get(Class.forName(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
From source file:org.jclouds.json.internal.IgnoreNullFluentIterableTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (typeToken.getRawType() != FluentIterable.class || !(type instanceof ParameterizedType)) { return null; }// w ww. j a v a2 s .com Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType)); return (TypeAdapter<T>) newFluentIterableAdapter(elementAdapter); }
From source file:org.jclouds.json.internal.IgnoreNullIterableTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (typeToken.getRawType() != Iterable.class || !(type instanceof ParameterizedType)) { return null; }/*from ww w .j av a 2 s.co m*/ Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType)); return (TypeAdapter<T>) newIterableAdapter(elementAdapter); }