Class Caster


  • public class Caster
    extends java.lang.Object
    Methods to choose and perform casts from database type to Java types.
    Author:
    Jim McBeath
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int OBJECT_COST
      The cost to cast to the closes Java object type.
      static int PRIMITIVE_COST
      The cost to cast to the closest Java primitive type.
    • Constructor Summary

      Constructors 
      Constructor Description
      Caster()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object[] castArgsToConstructor​(TObject[] args, java.lang.reflect.Constructor constructor)
      Cast the given arguments to the specified constructors parameter types.
      static void deserializeJavaObjects​(TObject[] args)
      Find any JAVA_OBJECTs in the args and deserialize them into real Java objects.
      static java.lang.reflect.Constructor findBestConstructor​(java.lang.reflect.Constructor[] constructs, TObject[] args)
      Search for the best constructor that we can use with the given argument types.
      static java.lang.String getArgTypesString​(TObject[] args)
      Get a string giving the database types of all of the arguments.
      static int getSqlType​(TObject arg)
      Get the SQL type for a database argument.
      static int[] getSqlTypes​(TObject[] args)
      Get the SQL types for the given database arguments.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PRIMITIVE_COST

        public static final int PRIMITIVE_COST
        The cost to cast to the closest Java primitive type.
        See Also:
        Constant Field Values
      • OBJECT_COST

        public static final int OBJECT_COST
        The cost to cast to the closes Java object type.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Caster

        public Caster()
    • Method Detail

      • deserializeJavaObjects

        public static void deserializeJavaObjects​(TObject[] args)
        Find any JAVA_OBJECTs in the args and deserialize them into real Java objects.
        Parameters:
        args - The args to deserialize. Any JAVA_OBJECT args are converted in-place to a new TObject with a value which is the deserialized object.
      • findBestConstructor

        public static java.lang.reflect.Constructor findBestConstructor​(java.lang.reflect.Constructor[] constructs,
                                                                        TObject[] args)
        Search for the best constructor that we can use with the given argument types.
        Parameters:
        constructs - The set of constructors from which to select.
        argSqlTypes - The SQL types of the database arguments to be passed to the constructor.
        Returns:
        The constructor with the lowest cost, or null if there are no constructors that match the args.
      • getSqlTypes

        public static int[] getSqlTypes​(TObject[] args)
        Get the SQL types for the given database arguments.
        Parameters:
        args - The database args.
        Returns:
        The SQL types of the args.
      • getSqlType

        public static int getSqlType​(TObject arg)
        Get the SQL type for a database argument. If the actual value does not fit into the declared type, the returned type is widened as required for the value to fit.
        Parameters:
        arg - The database argument.
        Returns:
        The SQL type of the arg.
      • getArgTypesString

        public static java.lang.String getArgTypesString​(TObject[] args)
        Get a string giving the database types of all of the arguments. Useful for error messages.
        Parameters:
        args - The arguments.
        Returns:
        A string with the types of all of the arguments, using comma as a separator.
      • castArgsToConstructor

        public static java.lang.Object[] castArgsToConstructor​(TObject[] args,
                                                               java.lang.reflect.Constructor constructor)
        Cast the given arguments to the specified constructors parameter types. The caller must already have checked to make sure the argument count and types match the constructor.
        Parameters:
        args - The database arguments from which to cast.
        constructor - The constructor to which to cast.
        Returns:
        The cast arguments.