java.lang.Object
de.craftsblock.craftsnet.utils.reflection.TypeUtils

public class TypeUtils extends Object
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 Type
    Method
    Description
    static boolean
    equals(Class<?> a, Class<?> b)
    Determines if two types a and b are equivalent, considering primitive-wrapper equivalence.
    static boolean
    isAssignable(Class<?> targetType, Class<?> sourceType)
    Determines if the sourceType can be assigned to the targetType, considering both primitive types and their corresponding wrapper classes.
    static boolean
    isPrimitive(Class<?> type)
    Checks if the given type represents a primitive type.
    static boolean
    isWrapper(Class<?> type)
    Checks if the given type represents a wrapper class of a primitive type.
    static Class<?>
    toPrimitive(Class<?> type)
    Converts the given type to its corresponding primitive class if it is a wrapper type.
    static Class<?>
    toWrapper(Class<?> type)
    Converts the given type to its corresponding wrapper class if it is a primitive type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isAssignable

      @Contract(value="null, null -> false", pure=true) public static boolean isAssignable(Class<?> targetType, Class<?> sourceType)
      Determines if the sourceType can be assigned to the targetType, considering both primitive types and their corresponding wrapper classes.

      This method returns true if:

      • targetType is assignable from sourceType
      • targetType is a primitive and sourceType is its wrapper type
      • sourceType is a primitive and targetType is its wrapper type
      Otherwise, it returns 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:
      true if the sourceType is assignable to targetType, false otherwise.
    • toWrapper

      @Contract(value="null -> null; !null -> !null", pure=true) public static Class<?> toWrapper(Class<?> type)
      Converts the given type to its corresponding wrapper class if it is a primitive type. If the given type is not primitive or is null, it is returned unchanged.
      Parameters:
      type - The type to convert; may be primitive or wrapper.
      Returns:
      The wrapper class if type is primitive; otherwise, returns type unchanged.
    • toPrimitive

      @Contract(value="null -> null; !null -> !null", pure=true) public static Class<?> toPrimitive(Class<?> type)
      Converts the given type to its corresponding primitive class if it is a wrapper type. If the given type is not a wrapper or is null, it is returned unchanged.
      Parameters:
      type - The type to convert; may be wrapper or primitive.
      Returns:
      The primitive class if type is a wrapper; otherwise, returns type unchanged.
    • isPrimitive

      @Contract(value="null -> false", pure=true) public static boolean isPrimitive(Class<?> type)
      Checks if the given type represents a primitive type.
      Parameters:
      type - The type to check.
      Returns:
      true if type is a primitive type, false otherwise.
    • isWrapper

      @Contract(value="null -> false", pure=true) public static boolean isWrapper(Class<?> type)
      Checks if the given type represents a wrapper class of a primitive type.
      Parameters:
      type - The type to check.
      Returns:
      true if type is a wrapper class for a primitive, false otherwise.
    • equals

      public static boolean equals(Class<?> a, Class<?> b)
      Determines if two types a and b are equivalent, considering primitive-wrapper equivalence.

      For example, int.class and Integer.class are considered equivalent.

      Parameters:
      a - The first type.
      b - The second type.
      Returns:
      true if the two types are equivalent or both null, false otherwise.