Class TypeMagic

java.lang.Object
blue.endless.jankson.magic.TypeMagic

public class TypeMagic extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Class<?>
    This is a surprisingly intractable problem in Java: "Type" pretty much represents all possible states of reified and unreified type information, and each kind of Type has different, mutually exclusive, and often unintended ways of uncovering its (un-reified) class.
    static <U> U
    createAndCast(Class<U> t, boolean failFast)
    Attempts to create a new instance of the specified class using its no-arg constructor, if it has one.
    static <U> U
    Attempts to create a new instance of type t, and (unsafely) cast it to the target type U.
    static <U> U
     
    static <T> T
    Extremely unsafely casts an object into another type.

    Methods inherited from class java.lang.Object

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

    • TypeMagic

      public TypeMagic()
  • Method Details

    • classForType

      @Nullable public static Class<?> classForType(Type t)
      This is a surprisingly intractable problem in Java: "Type" pretty much represents all possible states of reified and unreified type information, and each kind of Type has different, mutually exclusive, and often unintended ways of uncovering its (un-reified) class.

      Generally it's much safer to use this for the type from a *field* than a blind type from an argument.

    • createAndCast

      @Nullable public static <U> U createAndCast(Type t)
      Attempts to create a new instance of type t, and (unsafely) cast it to the target type U. This might work even if the class is private or has a private constructor.
      Type Parameters:
      U - the target type.
      Parameters:
      t - the source type. The object will be created as this type.
      Returns:
      an object of type t, cast to type U. If any part of this process fails, this method silently returns null instead.
    • createAndCastCarefully

      public static <U> U createAndCastCarefully(Type t) throws DeserializationException
      Throws:
      DeserializationException
    • createAndCast

      @Nullable public static <U> U createAndCast(Class<U> t, boolean failFast) throws DeserializationException
      Attempts to create a new instance of the specified class using its no-arg constructor, if it has one. This might work even if the class is private or the constructor is private/hidden!
      Type Parameters:
      U - the target type.
      Parameters:
      t - the source type. The object will be created as this type.
      Returns:
      a new object of type U. If any part of this process fails, this method silently returns null instead.
      Throws:
      DeserializationException
    • shoehorn

      public static <T> T shoehorn(Object o)
      Extremely unsafely casts an object into another type. It's possible to mangle a List<Integer> into a List<String> this way, and the JVM might not throw an error until the program attempts to insert a String! So use this method with extreme caution as a last resort.
      Type Parameters:
      T - the destination type
      Parameters:
      o - the source object, of any type
      Returns:
      the source object cast to T.