public class TypeCast extends Object implements Function<TypeRef,Optional<ClassRef>>
Types.isInstanceOf(TypeRef, TypeDef, Function) with added bonus that generic
arguments
are resolved.
For example, when TypeCast.to(Map<?,?>) is called on HashMap<String, Integer, the result will be
Optional.of(Map<String, Integer>).
This works also for complex hierarchies with non-trivial type argument substitutions.
Limitation: Arguments involving wildcards are currently not supported.
| Modifier and Type | Method and Description |
|---|---|
Optional<ClassRef> |
apply(TypeRef type)
Perform the type cast, if possible.
|
static TypeCast |
to(ClassRef expectedType)
Create the function which casts to the specified target type.
|
public static TypeCast to(ClassRef expectedType)
expectedType - The type to which to cast. It must not be an array, and all type arguments (if any) must be unbounded wildcardspublic Optional<ClassRef> apply(TypeRef type)
apply in interface Function<TypeRef,Optional<ClassRef>>type - The type which will be castOptional.of(targetType<...>) is returned with the type arguments
resolved. If the
type cannot be cast, Optional.empty() is returned.IllegalStateException - when the type implements or extends target type multiple times with different arguments. Currently, this may also
apply to multiple inheritance of wildcard types, even if they were compatible.Copyright © 2023. All rights reserved.