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)); }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
public AnnotationConstructorNamingStrategy(Collection<? extends Class<? extends Annotation>> markers, Iterable<? extends NameExtractor<?>> extractors) { super(extractors); this.markers = checkNotNull(markers, "you must supply at least one annotation to mark deserialization constructors"); this.hasMarker = hasAnnotationIn(markers); }
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)); }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
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"); }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
static DeserializationConstructorAndReflectiveTypeAdapterFactory parameterizedCtorFactory() { FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(ImmutableSet.of( new ExtractSerializedName(), new ExtractNamed())); AnnotationConstructorNamingStrategy deserializationPolicy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class, SerializedNames.class, Inject.class), ImmutableSet.of(new ExtractNamed())); return new DeserializationConstructorAndReflectiveTypeAdapterFactory(new ConstructorConstructor(ImmutableMap.<Type, InstanceCreator<?>>of()), serializationPolicy, Excluder.DEFAULT, deserializationPolicy); }
private <T> Map<String, ParameterReader<?>> getParameterReaders(Gson context, Invokable<T, T> deserializationCtor) { Builder<String, ParameterReader<?>> result = ImmutableMap.builder(); for (Parameter param : deserializationCtor.getParameters()) { TypeAdapter<?> adapter = context.getAdapter(TypeToken.get(param.getType().getType())); String parameterName = constructorFieldNamingPolicy.translateName(deserializationCtor, param.hashCode()); checkArgument(parameterName != null, deserializationCtor + " parameter " + 0 + " failed to be named by " + constructorFieldNamingPolicy); @SuppressWarnings({ "rawtypes", "unchecked" }) ParameterReader<?> parameterReader = new ParameterReader(param.hashCode(), parameterName, adapter); result.put(parameterReader.name, parameterReader); } return result.build(); } }
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 AnnotationConstructorNamingStrategy(Collection<? extends Class<? extends Annotation>> markers, Iterable<? extends NameExtractor<?>> extractors) { super(extractors); this.markers = checkNotNull(markers, "you must supply at least one annotation to mark deserialization constructors"); this.hasMarker = hasAnnotationIn(markers); }
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"); }