Return to "When libraries aren't enough"



  • Ladies and gentlemen, I give you Reflection:

    // ***************************************************************************
    // File: Reflection.java
    //
    // Description: Reflection.java defines the reflection utility class.
    //
    // ***************************************************************************
    //
    // Declare the package
    //
    package app.util; // application utility package
    //
    // Import packages
    //
    import java.lang.reflect.; // Java reflection package
    //
    // Define classes
    //
    // ===========================================================================
    // Class: Reflection
    //
    // Reflection provides utility methods for handling object reflection.
    //
    // @since JDK 1.3
    //
    // ===========================================================================
    public abstract
    class Reflection
    { // begin Reflection class
    //
    // Define methods
    //
    // -------------------------------------------------------------------------
    // Method: Reflection.getClassObject
    /
    *

    • Loads a specified class. This method really only calls
    • Class.forClass, but it catches the exceptions from
    • forClass() and throws an exception with a more descriptive
    • error message.
    • @param className (String) the name of the class to load
    • @return (Class) the specified class
    • @throws ReflectionException Thrown if unable to find the specified
    •                           class file
      

    /
    // -------------------------------------------------------------------------
    public static
    Class
    getClassObject(String className) throws ReflectionException
    { // begin method Reflection.getClassObject
    //
    // Declare variables
    //
    Class returnValue;
    //
    // Trap exceptions
    //
    try
    { // begin trapping exceptions
    //
    // Load the class file
    //
    returnValue = Class.forName(className);
    } // end trapping exceptions
    catch (ClassNotFoundException e)
    { // begin class not found exception handler
    //
    // Throw an exception
    //
    throw new ReflectionException("unable to find class " +
    className);
    } // end class not found exception handler
    //
    // Return
    //
    return (returnValue);
    } // end method Reflection.getClassObject
    // -------------------------------------------------------------------------
    // Method: Reflection.getClassInstance
    /
    *

    • Returns an instance of a specified class. This method is only used for
    • classes with a no-argument constructor
    • @param classObj (Class) the class to instantiate
    • @return (Object) the class instance
    • @throws ReflectionException thrown if unable to create a class instance
      /
      // -------------------------------------------------------------------------
      public static
      Object
      getClassInstance(Class classObj) throws ReflectionException
      { // begin method Reflection.getClassInstance
      //
      // Declare variables
      //
      Object instance;
      //
      // Trap exceptions
      //
      try
      { // begin trapping exceptions
      //
      // Instantiate the class
      //
      instance = classObj.newInstance();
      } // end trapping exceptions
      //
      // Tried to instantiate an abstract class
      //
      catch (IllegalAccessException e)
      { // begin illegal access exception handler
      //
      // Throw an exception
      //
      throw new ReflectionException("unable to instantiate abstract class " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end illegal access exception handler
      //
      // Unable to instantiate the class
      //
      catch (InstantiationException e)
      { // begin instantiation exception handler
      //
      // throw an exception
      //
      throw new ReflectionException("unable to insantiate " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end instantiation exception handler
      //
      // Error in permissions
      //
      catch (SecurityException e)
      { // begin security exception handler
      //
      // throw an exception
      //
      throw new ReflectionException("security exception instantiating " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end security exception handler
      //
      // Return
      //
      return instance;
      } // end method Reflection.getClassInstance
      // -------------------------------------------------------------------------
      // Method: Reflection.getClassInstance
      /
      *
    • Returns an instance of a specified class. This method is only used for
    • classes with a no-argument constructor
    • @param classObj (Class) the class to instantiate
    • @param argTypes (Class[]) the argument types for the constructor
    • @param args (Object[]) the arguments for the constructor
    • @return (Object) the class instance
    • @throws ReflectionException thrown if unable to create a class instance
      /
      // -------------------------------------------------------------------------
      public static
      Object
      getClassInstance(Class classObj,
      Class[] argTypes,
      Object[] args) throws ReflectionException
      { // begin method Reflection.getClassInstance
      //
      // Declare variables
      //
      Constructor constructor;
      Object instance;
      //
      // Trap exceptions
      //
      try
      { // begin trapping exceptions
      //
      // Get the constructor
      //
      constructor = classObj.getConstructor(argTypes);
      //
      // Instantiate the class
      //
      instance = constructor.newInstance(args);
      } // end trapping exceptions
      //
      // Tried to insantiate an abstract class
      //
      catch (IllegalAccessException e)
      { // begin illegal argument exception
      //
      // Throw an exception
      //
      throw new ReflectionException("Unable to instantiate abstract class " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end illegal argument exception
      //
      // Error in arguments
      //
      catch (IllegalArgumentException e)
      { // begin illegal argument exception
      //
      // Throw an exception
      //
      throw new ReflectionException("unable to instantiate " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end illegal arugment exception
      //
      // Unable to instantiate
      //
      catch (InstantiationException e)
      { // begin instantiation exception handler
      //
      // Throw an exception
      //
      throw new ReflectionException("unable to instantiate " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end instantiation exception handler
      //
      // Error in the constructor
      //
      catch (InvocationTargetException e)
      { // begin invocation target exception handler
      //
      // Throw an exception
      //
      throw new ReflectionException("unable to construct " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end invocation target exception handler
      //
      // Unable to find the constructor
      //
      catch (NoSuchMethodException e)
      { // begin class not found exception handler
      //
      // Throw an exception
      //
      throw new ReflectionException("unable to find constructor for " +
      classObj.getName() +
      ": " +
      e.getMessage());
      } // end class not found exception handler
      //
      // Return
      //
      return instance;
      } // end method Reflection.getClassInstance
      // -------------------------------------------------------------------------
      // Method: Reflection.getClassInstance
      /
      *
    • Returns an instance of a specified class. This method is only used for
    • classes with a no-argument constructor
    • @param classObj (String) the name of the class to instantiate
    • @return (Object) the class instance
    • @throws ReflectionException thrown if unable to create a class instance
      /
      // -------------------------------------------------------------------------
      public static
      Object
      getclassInstance(String className) throws ReflectionException
      { // begin method Reflection.getclassInstance
      //
      // get an instance of the specified class
      //
      return
      (Reflection.getClassInstance(Reflection.getClassObject(className));
      } // end method Reflection.getClassInstance
      // -------------------------------------------------------------------------
      // Method: Reflection.getClassInstance
      /
      *
    • Returns an instance of a specified class. This method is only used for
    • classes with a no-argument constructor
    • @param classObj (String) the name of the class to instantiate
    • @param argTypes (Class[]) the argument types for the constructor
    • @param args (Object[]) the arguments for the constructor
    • @return (Object) the class instance
    • @throws ReflectionException thrown if unable to create a class instance
      */
      // -------------------------------------------------------------------------
      public static
      Object
      getclassInstance(String className,
      Class[] argTypes,
      Object[] arguments) throws ReflectionException
      { // begin method Reflection.getClassInstance
      //
      // Get an instance of the specified class
      //
      return (Reflection.getClassInstance(Reflection.getClassObject(className),
      argTypes,
      args));
      } // end method Reflection.getClassInstance
      } // end Reflection class

    and its companion, ReflectionException

    // ***************************************************************************
    //  File:  ReflectionException.java
    // 
    //  Description:  ReflectionException.java defines the ReflectionException
    //  class.
    //
    // ***************************************************************************
    //
    //  Define the package
    //
    package app.util;  //  application utility package
    //
    //  Define classes
    //
    // ===========================================================================
    //  Class:  ReflectionException
    /**
     *  ReflectionException is an exception class that identifies errors in the
     *  application reflection utilities.
     *  
     *  @since JDK 1.3
     *
     *  @see app.util#Reflection
     */
    // ===========================================================================
    public
    class ReflectionException extends Exception
    { // begin class ReflectionException
      // 
      //  Define constructors
      //
      // -------------------------------------------------------------------------
      //   Constructor:  ReflectionExcetion.<INIT>
      /**
       *   Constructs a default reflection exception object
       */
      // -------------------------------------------------------------------------
      public
      ReflectionException()
      { // begin ReflectionException constructor
        // 
        //  Call the parent constructor
        //
        super();
      } // end ReflectionException constructor
      // -------------------------------------------------------------------------
      //  Constructor:  ReflectionException.<INIT>
      /** 
       *  Constructs a reflection exception with a message
       *
       *  @param message (String) the message
       */
      // -------------------------------------------------------------------------
      public
      ReflectionException (String message)
      { // begin ReflectionException constructor
        //
        //  Call the parent constructor
        //
        super(message);
      } // end ReflectionException constructor
    } // end class ReflectionException
    


  • my favorite bit is the message saying that its going to return a more descriptive exception ... when they hide all the descriptive exceptions by 'overriding' them all ...



  • My favorite part is that every single line contains a fucking comment. "How can we make this even less readable? I know, replace every empty line with an empty comment!!"



  • Um, yeah. tl;dr



  • @morbiuswilters said:

    My favorite part is that every single line contains a fucking comment. "How can we make this even less readable? I know, replace every empty line with an empty comment!!"

    And there are even comments to explain the functionality of LANGUAGE KEYWORDS, such as try... because, yeah, nobody really knows what try does, right?



  • @ekolis said:

    And there are even comments to explain the functionality of LANGUAGE KEYWORDS, such as try... because, yeah, nobody really knows what try does, right?
     

    What does it do?

    Do, or do not.  There is no try.



  • A reflection class so 'complex' and doesn't provide a way of calling methods or setting variables. Lovely.



  •  //begin comment

    I //noun

    really //adverb

    wanted //verb

    to read //verb

    the code //noun

    but //conjuction

    it //pronoun

    is //verb

    so hard //adjective

    to find //verb

    it //pronoun

    in //preposition

    there //pronoun? not sure

    . //end of sentence

    //end of comment



  • I especially like that the comments that don't explain the obvious are mostly wrong because of mindless copy-pasting.

    Oh, and also:

    throw new ReflectionException("unable to instantiate abstract class " +
                                        classObj.getName()                      +
                                        ": "                                    +
                                        e.getMessage());
    

    Because no one will be interested in the stack trace of the original exception, they will just be happy that your code gave them a nicer exception instead...



    But maybe this isn't what it looks like. The original author may have written some code that parses Java code and adds comments to every construct, explaining what it does, then tested it on this class. I can dream...



  • I initially thought of some IDE that begins with a template containing stubs and comments, leaving the dev to add their own code - perhaps they felt that lines of their own code without comments looked too bare (deviated too much from the IDE-provided template that's clearly the "correct" way to do it) and liberally applied their own shart.

    Or, alternatively, the dev was too fuckwitted to see water when obscured by the ocean.

    (I knew a programmer like this back in the old BASIC schooldays - Pareto comments:code ratio. We called him "REM".)



  • Everyone knows comments only make code more readable.



  •  This has got to be some sort of passive-agressive reaction to management calling for more documentation. I just can't see anyone documenting try...catch because they think it's a good idea.



  • My favorite part is the no-argument constructor function that calls a constructor with arguments.



  •  @DOA said:

     This has got to be some sort of passive-agressive reaction to management calling for more documentation. I just can't see anyone documenting try...catch because they think it's a good idea.

    ++

    "Oh, they want more comments, do they? Well that's exactly what they'll get!" maniacal laughter

     



  • @DOA said:

     This has got to be some sort of passive-agressive reaction to management calling for more documentation. I just can't see anyone documenting try...catch because they think it's a good idea.

    You must be new here. Some people hear "commenting is good" and proceed to comment every single line, no matter how inane. The best advice I ever heard for commenting was "Every comment should contain the word 'because'; you should be commenting the why and not the what or the how*."

    *Obviously there are exceptions to this rule.


  • Discourse touched me in a no-no place

    Everyone else is complaining about the comments, but does this code even do anything useful?



  • @PedanticCurmudgeon said:

    Everyone else is complaining about the comments, but does this code even do anything useful?

    It hides all the extraneous Exceptions which can be thrown when instantiating objects via reflection in Java. Everything could be done with builtins, but it does keep you from inserting all that boilerplate exception handling.

    If I were to design such beast, tho, I think I would pass the original exception to the new ReflectionException as a reason so it could be traced better.

    As far as the comment style goes, it is my understanding that the person responsible for that code back in the 1999 timeframe still comments code that way.



  • @lettucemode said:

    My favorite part is the no-argument constructor function that calls a constructor with arguments.

    That may be a transcription typo on my part, but I really don't care enough to go back and find out.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.