Skip to content

Commit

Permalink
Update BeanAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Oct 14, 2024
1 parent 53dea85 commit 706d578
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions kilo-client/src/main/java/org/httprpc/kilo/beans/BeanAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,32 +418,6 @@ public String toString() {
}
}

// Container type
private static class ContainerType implements ParameterizedType {
Type[] actualTypeArguments;
Type rawType;

ContainerType(Type[] actualTypeArguments, Type rawType) {
this.actualTypeArguments = actualTypeArguments;
this.rawType = rawType;
}

@Override
public Type[] getActualTypeArguments() {
return actualTypeArguments;
}

@Override
public Type getRawType() {
return rawType;
}

@Override
public Type getOwnerType() {
return null;
}
}

private Object bean;

private Map<String, Property> properties;
Expand Down Expand Up @@ -749,7 +723,22 @@ public static <T> T coerce(Object value, Class<T> type) {
*/
@SuppressWarnings("unchecked")
public static <E> List<E> coerceList(Collection<?> collection, Class<E> elementType) {
return (List<E>)toGenericType(collection, new ContainerType(new Type[] {elementType}, List.class));
return (List<E>)toGenericType(collection, new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[] {elementType};
}

@Override
public Type getRawType() {
return List.class;
}

@Override
public Type getOwnerType() {
return null;
}
});
}

/**
Expand All @@ -775,7 +764,22 @@ public static <E> List<E> coerceList(Collection<?> collection, Class<E> elementT
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> coerceMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) {
return (Map<K, V>)toGenericType(map, new ContainerType(new Type[] {keyType, valueType}, Map.class));
return (Map<K, V>)toGenericType(map, new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[] {keyType, valueType};
}

@Override
public Type getRawType() {
return Map.class;
}

@Override
public Type getOwnerType() {
return null;
}
});
}

/**
Expand All @@ -795,7 +799,22 @@ public static <K, V> Map<K, V> coerceMap(Map<?, ?> map, Class<K> keyType, Class<
*/
@SuppressWarnings("unchecked")
public static <E> Set<E> coerceSet(Collection<?> collection, Class<E> elementType) {
return (Set<E>)toGenericType(collection, new ContainerType(new Type[] {elementType}, Set.class));
return (Set<E>)toGenericType(collection, new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[] {elementType};
}

@Override
public Type getRawType() {
return Set.class;
}

@Override
public Type getOwnerType() {
return null;
}
});
}

/**
Expand Down

0 comments on commit 706d578

Please sign in to comment.