@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); return findGenericParameter(containerType, erasedType); }
/** * Same as {@link #findGenericParameter(Type, Class, int)} with n = 0. * * @param type the type * @param parameterizedSupertype the parameterized supertype * @return the first parameter type * @see #findGenericParameter(Type, Class, int) */ public static Optional<Type> findGenericParameter(Type type, Class<?> parameterizedSupertype) { return findGenericParameter(type, parameterizedSupertype, 0); }
/** * Given a subtype of {@code Map<K,V>}, returns the corresponding map entry type {@code Map.Entry<K,V>}. * @param mapType the map subtype * @return the map entry type */ public static Type resolveMapEntryType(Type mapType) { Type keyType = resolveType(KEY, mapType); Type valueType = resolveType(VALUE, mapType); return resolveMapEntryType(keyType, valueType); }
private Class<?> getCollectionType(Type containerType) { return getErasedType(containerType); }
/** * Creates a {@code QualifiedType<T>} for a {@code GenericType<T>} with no qualifiers. * @param type the unqualified type * @return the unqualified QualifiedType * @see #with(Annotation...) to then qualify your type */ @SuppressWarnings("unchecked") public static <T> QualifiedType<T> of(GenericType<T> type) { return (QualifiedType<T>) of(type.getType()); }
@Test public void findMultipleGenericParameters() throws NoSuchMethodException { assertThat(GenericTypes.findGenericParameter(methodReturnType(Xyz.class, "sample"), Xyz.class, 0)) .contains(String.class); assertThat(GenericTypes.findGenericParameter(methodReturnType(Xyz.class, "sample"), Xyz.class, 1)) .contains(Integer.class); assertThat(GenericTypes.findGenericParameter(methodReturnType(Xyz.class, "sample"), Xyz.class, 2)) .contains(Void.class); }
private static Type resolveMaplikeEntryType(Type maplikeType, Type keyParam, Type valueParam) { Type keyType = GenericTypes.resolveType(keyParam, maplikeType); Type valueType = GenericTypes.resolveType(valueParam, maplikeType); return resolveMapEntryType(keyType, valueType); }
/** * Given a key and value type, returns the map entry type {@code Map.Entry<keyType,valueType>}. * @param keyType the key type * @param valueType the value type * @return the map entry type */ public static Type resolveMapEntryType(Type keyType, Type valueType) { return resolveMapEntryType(TypeToken.of(keyType), TypeToken.of(valueType)); }
@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); return findGenericParameter(containerType, erasedType); }
@Override public boolean accepts(Type containerType) { Class<?> erasedType = getErasedType(containerType); return collectors.containsKey(erasedType) && containerType instanceof ParameterizedType; }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param <T> the type to map * @param column the column name * @param type the GenericType of the type * @return the materialized T */ @SuppressWarnings("unchecked") public <T> T getColumn(String column, GenericType<T> type) { return (T) getColumn(column, type.getType()); }
@Override public Optional<Type> elementType(Type containerType) { return findGenericParameter(containerType, EnumSet.class); }
@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); return findGenericParameter(containerType, erasedType); }
@Override public boolean accepts(Type containerType) { // compiler ensures that elements of EnumSet<E extends Enum<E>> are always enums return EnumSet.class.isAssignableFrom(getErasedType(containerType)) && containerType instanceof ParameterizedType; }
/** * Bind an argument dynamically by the generic type passed in. * * @param name token name to bind the parameter to * @param value to bind * @param argumentType type token for value argument * * @return the same Query instance */ public final This bindByType(String name, Object value, GenericType<?> argumentType) { return bindByType(name, value, argumentType.getType()); }
@Override public boolean accepts(Type containerType) { Class<?> erasedType = getErasedType(containerType); return containerType instanceof ParameterizedType && collectors.containsKey(erasedType); }
/** * Use a row mapper to extract a type from the current ResultSet row. * @param <T> the type to map * @param rowType the GenericType of the type * @return the materialized T */ @SuppressWarnings("unchecked") public <T> T getRow(GenericType<T> rowType) { return (T) getRow(rowType.getType()); }
@Override public Collector<?, ?, ?> build(Type containerType) { Class<?> erasedType = getErasedType(containerType); return collectors.get(erasedType); } }
/** * Bind an argument dynamically by the generic type passed in. * * @param position position to bind the parameter at, starting at 0 * @param value to bind * @param argumentType type token for value argument * * @return the same Query instance */ public final This bindByType(int position, Object value, GenericType<?> argumentType) { return bindByType(position, value, argumentType.getType()); }
/** * Use a column mapper to extract a type from the current ResultSet row. * @param <T> the type to map * @param column the column index * @param type the GenericType of the type * @return the materialized T */ @SuppressWarnings("unchecked") public <T> T getColumn(int column, GenericType<T> type) { return (T) getColumn(column, type.getType()); }