HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Dynamic Java Debug Code"
Volume: 3 Issue: 12, p. 48

	

Listing 1: CondDebug class in debug package. 

/* 
 * @ CondDebug.java 
 * 
 */ 

package com.company.debug; 

/** 
 * The <code>CondDebug</code> class contains 
 * methods that help debugging applications. 
 * It cannot be instantiated. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/9/98 
 */ 

public final class CondDebug { 

  /** Disallow instantiation of this class */ 
  private CondDebug() {} 

  /** 
  * Stops the program by throwing a runtime 
  * exception, CondAssertException, if the 
  * <code>cond</code> is NOT true. 
  * <p> 
  * 
  * @param    cond    the condition to be verified 
  * 
  */ 
  public static void assert(boolean cond) { 
    if (!cond) 
      throw new CondAssertException(); 
  } 

  /** 
  * Stops the program by throwing a runtime 
  * exception, CondAssertException, and prints 
  * <code>msg</code>, if the <code>cond</code> is 
  * NOT true. 
  * <p> 
  * 
  * @param    cond    the condition to be verified 
  * @param    msg     message to be printed 
  * 
  */ 
  public static void assert(boolean cond, String msg) { 
    if (!cond) 
      throw new CondAssertException(msg); 
  } 

  /** 
  * Tests CondDebug class 
  */ 
  public static void main(String[] args) { 
    try 
    { 
      CondDebug.assert((3 + 2) == 6); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean)"); 
      e.printStackTrace(); 
    } 
    try 
    { 
      CondDebug.assert((3 + 2) == 6, "(3 + 2) == 6"); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean, String)"); 
      e.printStackTrace(); 
    } 
  } 
} 

Listing 2: CondAssertException class. 

/* 
 * @ CondAssertException.java 
 * 
 */ 

package com.company.debug; 

/** 
 * Thrown when an assertion is failed in 
 * <code>CondDebug</code> class. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/2/98 
 */ 

public class CondAssertException 
  extends RuntimeException { 

  /** Constructs an <code>CondAssertException</code> 
  * with no detail message. 
  */ 
  public CondAssertException() { 
    super(); 
  } 

  /** Constructs an <code>CondAssertException</code> 
  * with the specified detail message. 
  */ 
  public CondAssertException (String msg) { 
    super(msg); 
  } 
}; 

Listing 3: CondDebug class in tools package. 

/* 
 * @ CondDebug.java 
 * 
 */ 

package com.company.tools; 

/** 
 * The <code>CondDebug</code> class in this package 
 * contains methods that return to the caller 
 * immediately. It cannot be instantiated. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/8/98 
 */ 

public final class CondDebug { 
  /** Disallow instantiation of this class */ 
  private CondDebug() {} 
  /** 
  * Returns immediately to the caller. 
  * A debug stub. 
  * <p> 
  * 
  * @param      cond    the condition to be verified 
  * 
  */ 
  public static void assert(boolean cond) {} 
  /** 
  * Returns immediately to the caller. 
  * A debug stub. 
  * <p> 
  * 
  * @param      cond    the condition to be verified 
  * @param      msg     message to be printed 
  * 
  */ 
  public static void assert(boolean cond, 
    String msg) {} 
  /** 
  * Tests CondDebug class 
  */ 
  public static void main(String[] args) { 
    try 
    { 
      CondDebug.assert((3 + 2) == 6); 
      System.out.println( 
        "CondDebug.assert(boolean) is empty"); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean)"); 
      e.printStackTrace(); 
    } 
    try 
    { 
      CondDebug.assert((3 + 2) == 6, 
        "(3 + 2) == 6"); 
      System.out.println( 
        "CondDebug.assert(boolean, String) is empty" 
        ); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean, String)"); 
      e.printStackTrace(); 
    } 
  } 
} 

Listing 4: TestCondDebug class. 

/* 
 * @ TestCondDebug.java 
 * 
 */ 

package com.company.product; 
// By commenting either of the following two lines, 
// the conditional compilation of C++ can be 
// manually achieved. 
import com.company.debug.*; 
//import com.company.tools.*; 

/** 
 * The <code>TestCondDebug</code> class 
 * demonstrates the technique of importing 
 * conditional debug package. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/11/98 
 */ 

public class TestCondDebug { 
  public static void main(String[] args) { 
    try 
    { 
      CondDebug.assert((3 + 2) == 6); 
      System.out.println( 
        "CondDebug.assert(boolean) is empty"); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean)"); 
      e.printStackTrace(); 
    } 
    try 
    { 
      CondDebug.assert((3 + 2) == 6, 
        "(3 + 2) == 6"); 
      System.out.println( 
        "CondDebug.assert(boolean, String) is empty" 
        ); 
    } catch (CondAssertException e) { 
      System.err.println( 
        "CondDebug.assert(boolean, String)"); 
      e.printStackTrace(); 
    } 
  } 
} 

Listing 5: JDebug class. 

/* 
 * @ JDebug.java 
 * 
 */ 

package com.company.tools; 

import com.company.debug.*; 
import java.util.Date; 

/** 
 * The <code>JDebug</code> class contains methods 
 * that help debugging applications. It cannot be 
 * instantiated. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/2/98 
 */ 

public final class JDebug { 
  /** Disallow instantiation of this class */ 
  private JDebug() {} 
  static Debug _debug=null; 
  static boolean loaded = false; 
  /** Loads Debug class if found */ 
  private static boolean debugLoaded() { 
    if (!loaded) { 
      try { 
        Class jDebugClass = Debug.class; 
        _debug = (Debug)jDebugClass.newInstance(); 
        System.err.println("*** JDebug On ***"); 
      } catch (NoClassDefFoundError e) { 
      } catch (InstantiationException e) { 
      } catch(IllegalAccessException e) { 
      } 
      loaded = true; 
    } 
    return (_debug != null); 
  } 
  /** 
  * Stops the program by throwing a runtime 
  * exception, AssertException, if the 
  * <code>cond</code> is NOT true. 
  * <p> 
  * 
  * @param cond    the condition to be verified 
  * 
  */ 
  public static void assert(boolean cond) { 
    if (debugLoaded()) 
   _debug.assert(cond); 
  } 
  /** 
  * Stops the program by throwing a runtime 
  * exception, AssertException, and prints 
  * <code>msg</code>, if the <code>cond</code> 
  * is NOT true. 
  * <p> 
  * 
  * @param  cond    the condition to be verified 
  * @param  msg     message to be printed 
  * 
  */ 
  public static void assert(boolean cond, 
    String msg) { 
    if (debugLoaded()) 
     _debug.assert(cond, msg); 
  } 
  /** 
  * Prints Throwable's backtrace to the standard 
  * error stream. 
  * <p> 
  */ 
  public static void printStackTrace() { 
    if (debugLoaded()) 
      _debug.printStackTrace(); 
  } 
  /** 
  * Prints the difference of <code>time1</code> 
  * and <code>time2</code>. 
  * <p> 
  * 
  * @param    msg     message to be printed 
  * @param    time1   base time 
  * @param    time2   new time 
  */ 
  public static void printTimeDiff(String msg, 
    long time1, long time2) { 
    if (debugLoaded()) 
      _debug.printTimeDiff(msg, time1, time2); 
  } 
  /** 
  * Returns the total amount of memory and the amount of free memory 
  * in the JVM using Runtime class' totalMemory() and freeMemory() 
  * <p> 
  * 
  * @param    msg     message to be printed 
  */ 
  public static void memoryInfo(String msg) { 
    if (debugLoaded()) 
      _debug.memoryInfo(msg); 
  } 
  /** 
  * Tests JDebug class 
  */ 
  public static void main(String[] args) { 
    long t1 = System.currentTimeMillis(); 
    try 
    { 
        JDebug.assert((3 + 2) == 6); 
    } catch (AssertException e) { 
      System.err.print("JDebug.assert(boolean) - "); 
      e.printStackTrace(); 
    } 
    try 
    { 
        JDebug.assert((3 + 2) == 6, "(3 + 2) == 6"); 
    } catch (AssertException e) { 
      System.err.print("JDebug.assert(boolean, String) - "); 
      e.printStackTrace(); 
    } 
    JDebug.printStackTrace(); 
    JDebug.memoryInfo("In JDebug main()"); 
    long t2 = System.currentTimeMillis(); 
    JDebug.printTimeDiff("Time to run JDebug.main()", t1, t2); 
  } 
} 

Listing 6: Debug.java.
 
/* 
* @ Debug.java 
* 
*/ 

package com.company.debug; 
import com.company.tools.*; 
/** 
 * The <code>Debug</code> class is dynamically loaded by <code>JDebug</code> 
 * class when it's present in the package. 
 * <p> 
 * 
 * @author Joe Chou 
 * @version 1.0, 7/2/98 
 */ 

public final class Debug { 
  /** 
   * Stops the program by throwing a runtime exception, AssertException, 
   * if the <code>cond</code> is NOT true. 
   * <p> 
   * 
   * @param cond the condition to be verified 
   * 
   */ 
  public final static void assert(boolean cond) { 
    if (!cond) 
      throw new AssertException(); 
  } 
  /** 
   * Stops the program by throwing a runtime 
   * exception, AssertException, and prints 
   * <code>msg</code>, if the <code>cond</code> 
   * is NOT true. 
   * <p> 
   * 
   * @param cond the condition to be verified 
   * @param msg message to be printed 
   * 
   */ 
  public final static void assert(boolean cond, 
                                  String msg) { 
    if (!cond) 
      throw new AssertException(msg); 
  } 
  /** 
  * Prints Throwable's backtrace to the standard 
  * error stream. 
  * <p> 
  */ 
  public final static void printStackTrace() { 
    Throwable t = new Throwable(); 
    System.err.print( 
      "Debug.printStackTrace() - "); 
    t.printStackTrace(); 
  } 
  /** 
  * Prints the difference of <code>time1</code> 
  * and <code>time2</code>. 
  * <p> 
  * 
  * @param    msg     message to be printed 
  * @param    time1   base time 
  * @param    time2   new time 
  */ 
  public final static void printTimeDiff(String msg, 
    long time1, long time2) { 
    System.err.print("Debug.printTimeDiff() - "); 
    if (time1 < 0) 
      System.err.println( 
        "Invalid parameter, time1, specified"); 
    if (time2 < 0) 
      System.err.println( 
        "Invalid parameter, time2, specified"); 
    long timeDiff = time2 - time1; 
    System.err.println(msg + ": " + timeDiff + 
        " millisecond"); 
  } 
  /** 
  * Returns the total amount of memory and the 
  * amount of free memory in the JVM using 
  * Runtime class' totalMemory() and freeMemory() 
  * <p> 
  * 
  * @param    msg     message to be printed 
  */ 
  public final static void memoryInfo(String msg) { 
    Runtime r = Runtime.getRuntime(); 
    System.err.println("Debug.memoryInfo() - " + 
      msg + " Total Memory: " + r.totalMemory() + 
      " Free Memory: " + r.freeMemory()); 
  } 
} 

Listing 7: ProfileCondDebug class. 

/* 
 * @ ProfileCondDebug.java 
 * 
 */ 

package com.company.product; 
import com.company.tools.*; 

/** 
 * The <code>ProfileCondDebug</code> class 
 * obtains the time needed to run a 
 * <code>CondDebug.assert()</code> 10 million times. 
 * The result of this class will be compared 
 * with the result of <cond>ProfileDynamicDebug 
 * </cond> class. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/29/98 
 */ 

public class ProfileCondDebug { 
  public static void main(String[] args) { 
    long t1 = System.currentTimeMillis(); 
    for (int i=0; i < 10000000; i++) { 
      CondDebug.assert(false); 
    } 
    long t2 = System.currentTimeMillis(); 
    System.out.println( 
   "Time to run ProfileCondDebug.main(): " 
   + (t2 - t1) + " millisecond"); 
  } 
} 

Listing 8: ProfileDynamicDebug class. 

/* 
 * @ ProfileDynamicDebug.java 
 * 
 */ 

package com.company.product; 
import com.company.tools.*; 

/** 
 * The <code>ProfileDynamicDebug</code> class 
 * obtains the time needed to run a 
 * <cond>Debug.assert()</code> 10 million times. The 
 * result of this class will be compared with the 
 * result of <cond>ProfileCondDebug</cond> class. 
 * <p> 
 * 
 * @author  Joe Chou 
 * @version 1.0, 7/29/98 
 */ 

public class ProfileDynamicDebug { 
  public static void main(String[] args) { 
    long t1 = System.currentTimeMillis(); 
    for (int i=0; i < 10000000; i++) { 
      JDebug.assert(false); 
    } 
    long t2 = System.currentTimeMillis(); 
    System.out.println( 
   "Time to run ProfileDynamicDebug.main(): " 
   + (t2 - t1) + " millisecond"); 
  } 
} 
  
  

  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.