You know, we do have wheels now...



  • And yet another gem from this wonderful app I'm rewriting. This is one of the many useless utility classes the previous developer left me.

    public class Print extends Object
    {
        private static Logger logger;

        private static String NAME;

        static
        {
            try
            {
                NAME = new Print().getClass().getName();
                logger = Logger.getLogger(Print.class);
            } catch (Exception e){ logger = null; }
        }

        public static void println(Object obj)
        {
            String str = obj == null ? null : obj.toString();
            if (str == null || str.trim().length() <= 0)
            {
                if (logger != null) { logger.debug(""); }
                else { System.out.println(); }
            }
            else
            {
                if (logger != null) { logger.debug(str); }
                else { System.out.println(getTimeStamp() + " " + str); }
            }
        }//end println()

    }

     

    Lovely, and I love the fact that they extended the Object class, you know, just to make sure.  The other great thing is that by having the logger here in the Print class, and by setting the logger to the name of the Print class you no longer get your debug messages saying something like [MyClass:103]  Where 103 is the line number the debug message comes from.  No, now all you get is Print.  Very helpful.



  • this reminds me of a function we have called printlnCommon which takes an XML DOM and populates some Hashtables with data from the XML so that a different function can then use the data from the hashtables.  It doesn't actually print anything.



  •  here it is...  I've left it's aweful formatting too.

     

     

        private void printlnCommon(Node n,String XmlNodeType,String Info) {
           
           
           //Log4j.info( "\n" + " GOT ELEMENT " + XmlNodeType + "\n" ) ;
           
            String val = n.getNamespaceURI();
            if (val != null) {
               // Log4j.info(" uri=\"" + val + "\"");
            }

            val = n.getPrefix();
            if (val != null) {
               //Log4j.info(" pre=\"" + val + "\"");
            }

            val = n.getLocalName();
            if (val != null) {
               //Log4j.info(" local=\"" + val + "\"");
            }

            val = n.getNodeValue();
            if (val != null) {           
                     if (val.trim().equals("")) {
                     }
                     else {
                    
                    // Log4j.info("\n" + " PUT IN HASHMAP " + CurrentNode + "==" + n.getNodeValue() +"\n" ) ;
                                  
           
                            int newGensym = matchString(Info)  ;
                            //Log4j.info("TocXmlHandler CURRENT1 " + newGensym );
                           
                            if (newGensym >= 0 ) {
                   
                    if ( newGensym > prevGensym )
                            currentGensym++   ;
       
                prevGensym = newGensym ;
                       
                }
                            //Log4j.info( "TocXmlHandler CURRENT2 "  + currentGensym) ;

                            if ( currentGensym == 1 ) {
                           // Log4j.info( "TocXmlHandler Func "  + CurrentNode + "=" +n.getNodeValue() ) ;
                            Func.put(CurrentNode,n.getNodeValue()); 
                    }
                        else if ( currentGensym == 2) {
                           // Log4j.info( "TocXmlHandler Host "  + CurrentNode + "=" +n.getNodeValue() ) ;
                            HostInfo.put(CurrentNode,n.getNodeValue()); 
                    }
                else if ( currentGensym == 3) {
                            // Log4j.info( "TocXmlHandler Test "  + CurrentNode + "=" +n.getNodeValue() ) ;
                            TestInfo.put(CurrentNode,n.getNodeValue()); 
                }
                     }
               }

           
           
        }



  • @tster said:

                }
                     }
               }
     

     The birds fly in formation!



  • @op said:

    if (str == null || str.trim().length() <font size=+1><</font>= 0)

    I like how it handles negative-length strings...



  • That sure is some horrible Java syntax. But I don't see any wierd side effects so you should just be able to refactor it out... right?



  • @Dudehole said:

    That sure is some horrible Java syntax. But I don't see any wierd side effects so you should just be able to refactor it out... right?

     

    Oh yeah.  I just got done going through and removing the 400+ references to it and replacing them with either LOGGER.error(), LOGGER.debug() or something else (where appropriate).  It wasn't just a simple "Search and replace" though :(

    The above was just one of the many useless methods contained within this utility function.  Here is another gem that could be replaced by either:

    error.printStackTrace();

    Or

    LOGGER.error(error);

    But nooooo, previous dev's way is:

    public static void print(Exception error)
        {
            if (error != null)
            {
                try
                {
                    StackTraceElement[] eArray = error.getStackTrace();
                    println(error.toString());
                    int length = eArray != null ? eArray.length : 0;
                    for (int i = 0; i < length; ++i)
                    {
                        if (LOGGER != null) { LOGGER.debug("     at " + eArray[i].toString()); }
                        else { System.out.println("     at " + eArray[i].toString()); }
                    }
                }catch (Exception e){}
            }
        }//end print()


Log in to reply