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()) } // @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(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
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()) } // @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(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
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()) } // @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(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
public void testAnnotationConstructorFieldNamingStrategyCPAndNamed() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class), ImmutableSet.of(new ExtractNamed())); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 4); assertEquals(strategy.translateName(constructor, 0), "aardvark"); assertEquals(strategy.translateName(constructor, 1), "bat"); assertEquals(strategy.translateName(constructor, 2), "coyote"); // Note: @Named overrides the ConstructorProperties setting assertEquals(strategy.translateName(constructor, 3), "dingo"); Invokable<MixedConstructorTest, MixedConstructorTest> mixedCtor = strategy.getDeserializer(typeToken(MixedConstructorTest.class)); assertNotNull(mixedCtor); assertEquals(mixedCtor.getParameters().size(), 4); assertEquals(strategy.translateName(mixedCtor, 0), "aardvark"); assertEquals(strategy.translateName(mixedCtor, 1), "bat"); assertEquals(strategy.translateName(mixedCtor, 2), "cat"); assertEquals(strategy.translateName(mixedCtor, 3), "dog"); }
public void testAnnotationConstructorFieldNamingStrategyCP() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class), ImmutableSet.<NameExtractor<?>>of()); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 4); assertEquals(strategy.translateName(constructor, 0), "aardvark"); assertEquals(strategy.translateName(constructor, 1), "bat"); assertEquals(strategy.translateName(constructor, 2), "coyote"); assertEquals(strategy.translateName(constructor, 3), "dog"); Invokable<MixedConstructorTest, MixedConstructorTest> mixedCtor = strategy.getDeserializer(typeToken(MixedConstructorTest.class)); assertNotNull(mixedCtor); assertEquals(mixedCtor.getParameters().size(), 4); assertEquals(strategy.translateName(mixedCtor, 0), "thiscanbeoverriddenbyNamed"); assertNull(strategy.translateName(mixedCtor, 1)); assertNull(strategy.translateName(mixedCtor, 2)); assertNull(strategy.translateName(mixedCtor, 3)); }
public void testAnnotationConstructorFieldNamingStrategyInject() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(Inject.class), ImmutableSet.of(new ExtractNamed())); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 5); assertEquals(strategy.translateName(constructor, 0), "aa"); assertEquals(strategy.translateName(constructor, 1), "bb"); assertEquals(strategy.translateName(constructor, 2), "cc"); assertEquals(strategy.translateName(constructor, 3), "dd"); Invokable<MixedConstructorTest, MixedConstructorTest> mixedCtor = strategy.getDeserializer(typeToken(MixedConstructorTest.class)); assertNotNull(mixedCtor); assertEquals(mixedCtor.getParameters().size(), 4); assertEquals(strategy.translateName(mixedCtor, 0), "aardvark"); assertEquals(strategy.translateName(mixedCtor, 1), "bat"); assertEquals(strategy.translateName(mixedCtor, 2), "cat"); assertEquals(strategy.translateName(mixedCtor, 3), "dog"); }
public void testAnnotationConstructorFieldNamingStrategyCP() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class), ImmutableSet.<NameExtractor<?>>of()); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 4); assertEquals(strategy.translateName(constructor, 0), "aardvark"); assertEquals(strategy.translateName(constructor, 1), "bat"); assertEquals(strategy.translateName(constructor, 2), "coyote"); assertEquals(strategy.translateName(constructor, 3), "dog"); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }
public void testAnnotationConstructorFieldNamingStrategyInject() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(Inject.class), ImmutableSet.of(new ExtractNamed())); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 5); assertEquals(strategy.translateName(constructor, 0), "aa"); assertEquals(strategy.translateName(constructor, 1), "bb"); assertEquals(strategy.translateName(constructor, 2), "cc"); assertEquals(strategy.translateName(constructor, 3), "dd"); } }
public void testAnnotationConstructorFieldNamingStrategyCPAndNamed() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class), ImmutableSet.of(new ExtractNamed())); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 4); assertEquals(strategy.translateName(constructor, 0), "aardvark"); assertEquals(strategy.translateName(constructor, 1), "bat"); assertEquals(strategy.translateName(constructor, 2), "coyote"); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }