Class TypeUtils
java.lang.Object
de.craftsblock.craftsnet.utils.reflection.TypeUtils
Utility class for handling Java primitive types and their wrapper classes.
Provides methods to check type assignability, convert between primitive and wrapper types,
and compare types for equivalence considering primitive-wrapper relationships.
This class is designed to assist reflection operations where understanding the relationship between primitive types and their wrappers is essential.
- Since:
- 3.5.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanDetermines if two typesaandbare equivalent, considering primitive-wrapper equivalence.static booleanisAssignable(Class<?> targetType, Class<?> sourceType) Determines if thesourceTypecan be assigned to thetargetType, considering both primitive types and their corresponding wrapper classes.static booleanisPrimitive(Class<?> type) Checks if the giventyperepresents a primitive type.static booleanChecks if the giventyperepresents a wrapper class of a primitive type.static Class<?>toPrimitive(Class<?> type) Converts the giventypeto its corresponding primitive class if it is a wrapper type.static Class<?>Converts the giventypeto its corresponding wrapper class if it is a primitive type.
-
Method Details
-
isAssignable
@Contract(value="null, null -> false", pure=true) public static boolean isAssignable(Class<?> targetType, Class<?> sourceType) Determines if thesourceTypecan be assigned to thetargetType, considering both primitive types and their corresponding wrapper classes.This method returns
trueif:targetTypeis assignable fromsourceTypetargetTypeis a primitive andsourceTypeis its wrapper typesourceTypeis a primitive andtargetTypeis its wrapper type
false.- Parameters:
targetType- The target type to assign to; may be primitive or wrapper.sourceType- The source type to assign from; may be primitive or wrapper.- Returns:
trueif thesourceTypeis assignable totargetType,falseotherwise.
-
toWrapper
@Contract(value="null -> null; !null -> !null", pure=true) public static Class<?> toWrapper(Class<?> type) Converts the giventypeto its corresponding wrapper class if it is a primitive type. If the given type is not primitive or isnull, it is returned unchanged.- Parameters:
type- The type to convert; may be primitive or wrapper.- Returns:
- The wrapper class if
typeis primitive; otherwise, returnstypeunchanged.
-
toPrimitive
@Contract(value="null -> null; !null -> !null", pure=true) public static Class<?> toPrimitive(Class<?> type) Converts the giventypeto its corresponding primitive class if it is a wrapper type. If the given type is not a wrapper or isnull, it is returned unchanged.- Parameters:
type- The type to convert; may be wrapper or primitive.- Returns:
- The primitive class if
typeis a wrapper; otherwise, returnstypeunchanged.
-
isPrimitive
Checks if the giventyperepresents a primitive type.- Parameters:
type- The type to check.- Returns:
trueiftypeis a primitive type,falseotherwise.
-
isWrapper
Checks if the giventyperepresents a wrapper class of a primitive type.- Parameters:
type- The type to check.- Returns:
trueiftypeis a wrapper class for a primitive,falseotherwise.
-
equals
Determines if two typesaandbare equivalent, considering primitive-wrapper equivalence.For example,
int.classandInteger.classare considered equivalent.- Parameters:
a- The first type.b- The second type.- Returns:
trueif the two types are equivalent or bothnull,falseotherwise.
-