Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash when combining generics and records #1442

Merged
merged 1 commit into from Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -156,6 +156,21 @@ public void genericBoundedRecord() throws IOException {
assertThat(adapter.fromJson("{\"value\":4}")).isEqualTo(new GenericBoundedRecord<>(4));
}

@Test
public void indirectGenerics() throws IOException {
var value =
new HasIndirectGenerics(
new IndirectGenerics<>(1L, List.of(2L, 3L, 4L), Map.of("five", 5L)));
var jsonAdapter = moshi.adapter(HasIndirectGenerics.class);
var json = "{\"value\":{\"single\":1,\"list\":[2,3,4],\"map\":{\"five\":5}}}";
assertThat(jsonAdapter.toJson(value)).isEqualTo(json);
assertThat(jsonAdapter.fromJson(json)).isEqualTo(value);
}

public static record IndirectGenerics<T>(T single, List<T> list, Map<String, T> map) {}

public static record HasIndirectGenerics(IndirectGenerics<Long> value) {}

@Test
public void qualifiedValues() throws IOException {
var adapter = moshi.newBuilder().add(new ColorAdapter()).build().adapter(QualifiedValues.class);
Expand Down
35 changes: 5 additions & 30 deletions moshi/src/main/java16/com/squareup/moshi/RecordJsonAdapter.java
Expand Up @@ -17,15 +17,14 @@

import static java.lang.invoke.MethodType.methodType;

import com.squareup.moshi.internal.Util;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.RecordComponent;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -54,39 +53,15 @@ final class RecordJsonAdapter<T> extends JsonAdapter<T> {
return null;
}

Map<String, Type> mappedTypeArgs = null;
if (type instanceof ParameterizedType parameterizedType) {
Type[] typeArgs = parameterizedType.getActualTypeArguments();
var typeVars = rawType.getTypeParameters();
mappedTypeArgs = new LinkedHashMap<>(typeArgs.length);
for (int i = 0; i < typeArgs.length; ++i) {
var typeVarName = typeVars[i].getName();
var materialized = typeArgs[i];
mappedTypeArgs.put(typeVarName, materialized);
}
}
var components = rawType.getRecordComponents();
var bindings = new LinkedHashMap<String, ComponentBinding<?>>();
var constructorParams = new Class<?>[components.length];
var componentRawTypes = new Class<?>[components.length];
var lookup = MethodHandles.lookup();
for (int i = 0, componentsLength = components.length; i < componentsLength; i++) {
RecordComponent component = components[i];
constructorParams[i] = component.getType();
componentRawTypes[i] = component.getType();
var name = component.getName();
var componentType = component.getGenericType();
if (componentType instanceof TypeVariable<?> typeVariable) {
var typeVarName = typeVariable.getName();
if (mappedTypeArgs == null) {
throw new AssertionError(
"No mapped type arguments found for type '" + typeVarName + "'");
}
var mappedType = mappedTypeArgs.get(typeVarName);
if (mappedType == null) {
throw new AssertionError(
"No materialized type argument found for type '" + typeVarName + "'");
}
componentType = mappedType;
}
var componentType = Util.resolve(type, rawType, component.getGenericType());
var jsonName = name;
Set<Annotation> qualifiers = null;
for (var annotation : component.getDeclaredAnnotations()) {
Expand Down Expand Up @@ -129,7 +104,7 @@ final class RecordJsonAdapter<T> extends JsonAdapter<T> {

MethodHandle constructor;
try {
constructor = lookup.findConstructor(rawType, methodType(void.class, constructorParams));
constructor = lookup.findConstructor(rawType, methodType(void.class, componentRawTypes));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError(e);
}
Expand Down