Package Torello.JSON

Class InvalidClassException

  • All Implemented Interfaces:
    java.io.Serializable

    public class InvalidClassException
    extends java.lang.RuntimeException
    Used for Read Json method that attempt to build a Java POJO, but fail due to an inability to extract a valid constructor from the object. When using a class' constructor as a builder, the required constructor must be one which accepts only one parameter. That parameter must be of type JsonObject.
    See Also:
    Serialized Form


    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method
      static <T> Constructor<T> check​(Class<T> c)
      • Methods inherited from class java.lang.Throwable

        addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
      • Methods inherited from class java.lang.Object

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

      • serialVersionUID

        🡇     🗕  🗗  🗖
        public static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.

        Note that Java's java.lang.Exception and java.lang.Error classes implement the Serializable interface, and a warning-free build expects this field be defined here.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         public static final long serialVersionUID = 1;
        
    • Constructor Detail

      • InvalidClassException

        🡅  🡇     🗕  🗗  🗖
        public InvalidClassException​(java.lang.String message)
        Constructs a InvalidClassException with the specified detail message.
        Parameters:
        message - the detail message.
      • InvalidClassException

        🡅  🡇     🗕  🗗  🗖
        public InvalidClassException​(java.lang.String message,
                                     java.lang.Throwable cause)
        Constructs a new InvalidClassException with the specified detail message and cause.

        Note: The detail message associated with cause is not automatically incorporated into this exception's detail message.
        Parameters:
        message - The detail message (which is saved for later retrieval by the Throwable.getMessage() method).
        cause - the cause (which is saved for later retrieval by the Throwable.getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
      • InvalidClassException

        🡅  🡇     🗕  🗗  🗖
        public InvalidClassException​(java.lang.Throwable cause)
        Constructs a new InvalidClassException with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for exceptions that are little more than wrappers for other throwables.
        Parameters:
        cause - The cause (which is saved for later retrieval by the Throwable.getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)
    • Method Detail

      • check

        🡅     🗕  🗗  🗖
        public static <T> java.lang.reflect.Constructor<T> check​
                    (java.lang.Class<T> c)
        
        Extracts a valid java.lang.reflect.Constructor instance for a constructor that accepts one arguement of type JsonObject.

        Throws if such an instance of Constructor cannot be found.
        Throws:
        java.lang.NullPointerException - throws if parameter 'c' is null
        InvalidClassException - throws if any errors occur when extracting the instance of java.lang.reflect.Constructor. This method excepts & attempts to extract a one argument constructor whose sole arguement is of type JsonObject.
        Code:
        Exact Method Body:
         final Constructor<T> ctor;
        
         Objects.requireNonNull(c, "The Class<T> parameter 'c' has been passed null");
        
         try
         {
             // This just gets a "Constructor" using Reflection.  The main point is that the
             // Constructor must have exactly one parameter - and that parameter must have a
             // type "JsonObject"  (basic java.lang.reflect stuff)
             //
             //System.out.println("c.getName:():" + c.getName());
        
             ctor = c.getDeclaredConstructor(JsonObject.class);
         }
        
         catch (Exception e)
         {
             if (c.getEnclosingClass() != null)
             {
                 int modifiers = c.getModifiers();
        
                 if ((! Modifier.isStatic(modifiers)) ||  (! Modifier.isPublic(modifiers)))
        
                     throw new InvalidClassException(
                         "Unable to retrieve POJO Constructor for class: " +
                         "[" + c.getName() + "]\n" +
                         "Your class appears to be a Nested-Class, however it has not been " +
                         "declared public and static.  There is no way to retrieve a " +
                         "1-Argument JsonObject Constructor for Nested-Type's unless the " +
                         "type has been declared BOTH static AND public.\n" +
                         "See Exception.getCause() for details.",
                         e
                     );
        
                 else throw new InvalidClassException(
                     "There was a problem retrieving a one-argument, public, constructor for the " +
                     "class you wish to instantiate.  See Exception.getCause() for details.",
                     e
                 );
             }
        
             else throw new InvalidClassException(
                 "Unable to retrieve POJO Constructor for class: [" + c.getName() + "]\n" +
                 "Do you have a one-argument, public, constructor for this class?\n" +
                 "Does it accept a JsonObject in its parameter list?\n" +
                 "See Exception.getCause() for details.",
                 e
             );
         }
        
        
         // If the user has requested a class that doesn't have that kind of constructor, then
         // there is no way to build the object.  Throw an exception.
        
         if (ctor == null) throw new InvalidClassException(
             "The class which was passed to parameter 'c' [" + c.getName() + "] does not " +
             "appear to have a constructor with precisely one parameter of type JsonObject.  " +
             "c.getDeclaredConstructor(JsonObject.class) has returned null."
         );
        
         return ctor;