/** * 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); }
InferredRowMapperFactory(RowMapper<?> mapper) { this.maps = findGenericParameter(mapper.getClass(), RowMapper.class) .orElseThrow(() -> new UnsupportedOperationException("Must use a concretely typed RowMapper here")); this.mapper = mapper; }
InferredSqlArrayTypeFactory(SqlArrayType<?> arrayType) { this.elementType = findGenericParameter(arrayType.getClass(), SqlArrayType.class) .orElseThrow(() -> new UnsupportedOperationException("Must use a concretely typed SqlArrayType here")); this.arrayType = arrayType; }
@Override public Optional<RowMapper<?>> build(Type mapType, ConfigRegistry config) { return Optional.of(mapType) .filter(ParameterizedType.class::isInstance) .map(ParameterizedType.class::cast) .filter(maybeMap -> Map.class.equals(maybeMap.getRawType())) .filter(map -> String.class.equals(GenericTypes.findGenericParameter(map, Map.class, 0).orElse(null))) .flatMap(map -> GenericTypes.findGenericParameter(map, Map.class, 1)) .filter(value -> !Object.class.equals(value)) .flatMap(config.get(ColumnMappers.class)::findFor) .map(GenericMapMapper::new); }
@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); return findGenericParameter(containerType, erasedType); }
ResultIteratorReturner(QualifiedType<?> returnType) { this.elementType = returnType.mapType(type -> GenericTypes.findGenericParameter(type, Iterator.class)) .orElseThrow(() -> new IllegalStateException( "Cannot reflect ResultIterator<T> element type T in method return type " + returnType)); }
private static Type findOptionalType(Type wrapperType, Object nestedValue) { if (getErasedType(wrapperType).equals(Optional.class)) { Optional<Type> nestedType = findGenericParameter(wrapperType, Optional.class); if (nestedType.isPresent()) { return nestedType.get(); } } return nestedValue == null ? Object.class : nestedValue.getClass(); } }
private Type findOptionalType(Type wrapperType, Object nestedValue) { if (getErasedType(wrapperType).equals(com.google.common.base.Optional.class)) { Optional<Type> nestedType = findGenericParameter(wrapperType, com.google.common.base.Optional.class); if (nestedType.isPresent()) { return nestedType.get(); } } return nestedValue == null ? Object.class : nestedValue.getClass(); }
StreamReturner(QualifiedType<?> returnType) { elementType = returnType.mapType(type -> GenericTypes.findGenericParameter(type, Stream.class)) .orElseThrow(() -> new IllegalStateException( "Cannot reflect Stream<T> element type T in method return type " + returnType)); }
@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); return findGenericParameter(containerType, erasedType); }
ResultIterableReturner(QualifiedType<?> returnType) { // extract T from Query<T> elementType = returnType.mapType(type -> GenericTypes.findGenericParameter(type, ResultIterable.class)) .orElseThrow(() -> new IllegalStateException( "Cannot reflect ResultIterable<T> element type T in method return type " + returnType)); }
InferredColumnMapperFactory(ColumnMapper<?> mapper) { this.maps = QualifiedType.of( findGenericParameter(mapper.getClass(), ColumnMapper.class) .orElseThrow(() -> new UnsupportedOperationException("Must use a concretely typed ColumnMapper here"))) .with(getQualifiers(mapper.getClass())); this.mapper = mapper; }
@Override public Optional<Type> elementType(Type containerType) { Class<?> erasedType = getCollectionType(containerType); if (Map.class.isAssignableFrom(erasedType)) { return Optional.of(VavrGenericMapUtil.resolveMapEntryType(containerType)); } else if (Multimap.class.isAssignableFrom(erasedType)) { return Optional.of(VavrGenericMapUtil.resolveMultimapEntryType(containerType)); } return findGenericParameter(containerType, erasedType); }
@Override public java.util.Optional<Type> elementType(Type containerType) { Class<?> erasedType = getErasedType(containerType); if (Multimap.class.isAssignableFrom(erasedType)) { Type keyType = resolveType(multimapKey, containerType); Type valueType = resolveType(multimapValue, containerType); return java.util.Optional.of(resolveMapEntryType(keyType, valueType)); } else if (Map.class.isAssignableFrom(erasedType)) { return java.util.Optional.of(resolveMapEntryType(containerType)); } return findGenericParameter(containerType, erasedType); }
private Optional<Argument> buildValueArgument(Type type, ConfigRegistry config, Value<?> value) { Type nestedType = findGenericParameter(type, Value.class).orElseGet(() -> extractTypeOfValue(value)); Object nestedValue = value.getOrNull(); return resolveNestedFromConfigured(config, nestedType, nestedValue); }
@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); }
@Test public void findGenericParameterOfSuperClass() throws NoSuchMethodException { assertThat(GenericTypes.findGenericParameter(methodReturnType(Bar.class, "subTypeGeneric"), Foo.class)) .isEqualTo(Optional.of(Integer.class)); }
@Test public void findGenericParameterOfAncestorClass() throws NoSuchMethodException { assertThat(GenericTypes.findGenericParameter(methodReturnType(Baz.class, "descendentTypeGeneric"), Foo.class)) .contains(String.class); }
@Test public void findGenericParameterOfGeneric() throws NoSuchMethodException { assertThat(GenericTypes.findGenericParameter(methodReturnType(Foo.class, "generic"), Foo.class)) .contains(String.class); }
@Test public void findGenericParameterOfNestedGeneric() throws NoSuchMethodException { assertThat(GenericTypes.findGenericParameter(methodReturnType(Foo.class, "nestedGeneric"), Foo.class)) .contains(methodReturnType(Foo.class, "generic")); }