Package com.cedarsoftware.util
Class TypeUtilities
java.lang.Object
com.cedarsoftware.util.TypeUtilities
Useful APIs for working with Java types, including resolving type variables and generic types.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Type
Extracts the component type of an array type.static Class<?>
getRawClass
(Type type) Extracts the raw Class from a given Type.static boolean
hasUnresolvedType
(Type type) Determines whether the provided type (including its nested types) contains an unresolved type variable, like T, V, etc. that needs to be bound (resolved).static Type
inferElementType
(Type container, Type fieldGenericType) Infers the element type contained within a generic container type.static Type
resolveType
(Type rootContext, Type typeToResolve) Public API: Resolves type variables in typeToResolve using the rootContext, which should be the most concrete type (for example, Child.class).static Type
resolveTypeUsingInstance
(Object target, Type typeToResolve) Resolves a generic field type using the actual class of the target instance.static void
Sets a custom cache implementation for holding results of getAllSuperTypes().
-
Method Details
-
setTypeResolveCache
Sets a custom cache implementation for holding results of getAllSuperTypes(). The Map implementation must be thread-safe, like ConcurrentHashMap, LRUCache, ConcurrentSkipListMap, etc.- Parameters:
cache
- The custom cache implementation to use for storing results of getAllSuperTypes(). Must be thread-safe and implement Map interface.
-
getRawClass
Extracts the raw Class from a given Type. For example, for Listit returns List.class. - Parameters:
type
- the type to inspect. If type is null, the return is null.- Returns:
- the raw class behind the type
-
extractArrayComponentType
Extracts the component type of an array type.- Parameters:
type
- the array type (can be a Class or GenericArrayType)- Returns:
- the component type, or null if not an array
-
hasUnresolvedType
Determines whether the provided type (including its nested types) contains an unresolved type variable, like T, V, etc. that needs to be bound (resolved).- Parameters:
type
- the type to inspect- Returns:
- true if an unresolved type variable is found; false otherwise
-
resolveTypeUsingInstance
Resolves a generic field type using the actual class of the target instance. It handles type variables, parameterized types, generic array types, and wildcards.- Parameters:
target
- the target instance that holds the fieldtypeToResolve
- the declared generic type of the field- Returns:
- the resolved type
-
resolveType
Public API: Resolves type variables in typeToResolve using the rootContext, which should be the most concrete type (for example, Child.class). -
inferElementType
Infers the element type contained within a generic container type.This method examines the container’s generic signature and returns the type argument that represents the element or value type. For example, in a Map the value type is used, in a Collection the sole generic parameter is used, and in an array the component type is used.
- Parameters:
container
- the full container type, e.g. - Map<String, List<Point>> (infers List<Point>) - List<Person> (infers Person) - Point[] (infers Point)fieldGenericType
- the declared generic type of the field- Returns:
- the resolved element type based on the container’s type, e.g. List<Point>, Person, or Point
-