Java Q&A 
by Karl Moss

Listing One
public class hello
{
  public static void main(String[] args)
  {
    System.out.println("Hello, world!");
  }
}


Listing Two
/** Constructs class elements from the raw byte stream
  * @param name The class file name
  * @param bytes The class file format
  */
public void read(String name, byte[] b) throws IOException
{
    // Create a new input stream for reading the bytes
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    DataInputStream in = new DataInputStream(bais);
    // Read the magic number
    magic = in.readInt();
    if (magic != 0xcafebabe) {
        throw new IOException("Bad Magic Number");
    }
    // Read the minor version and major version. Do not worry about validation
    minor_version = in.readUnsignedShort();
    major_version = in.readUnsignedShort();
    // Read the constant pool count
    constant_pool_count = in.readUnsignedShort();
    // Read the constant pool. If a problem exists the results will be null, 
    // in which case we'll just exit and let the class verifier catch the 
    // problem. The input stream will be positioned just after the constant
    // pool when complete (on the access_flags attribute)
    constantPool = readConstantPool(name, in, constant_pool_count);
    if (constantPool == null) {
        throw new IOException("Invalid Constant Pool");
    }
    // Get the access flags
    access_flags = in.readUnsignedShort();
    // This class
    this_class = in.readUnsignedShort();
    // Super class
    super_class = in.readUnsignedShort();
    // The number of interfaces
    interface_count = in.readUnsignedShort();
    // Read the interfaces
    interfaces = new Vector();
    for (int i = 0; i < interface_count; i++) {
        ClassInterface iface = new ClassInterface(constantPool);
        iface.read(in);
        interfaces.addElement(iface);
    }
    // The number of fields
    field_count = in.readUnsignedShort();

    // Read the fields
    fields = new Vector();
    for (int i = 0; i < field_count; i++) {
        ClassField field = new ClassField();
        field.read(in);
        fields.addElement(field);
    }
    // The number of methods
    method_count = in.readUnsignedShort();
    // Get the methods
    methods = new Vector();
    for (int i = 0; i < method_count; i++) {
        ClassMethod method = new ClassMethod(constantPool);
        method.read(in);
        methods.addElement(method);
    }
    // Get the number of attributes
    attribute_count = in.readUnsignedShort();
    // Get the attributes
    attributes = new Vector();
    for (int i = 0; i < attribute_count; i++) {
        ClassAttribute attr = new ClassAttribute(constantPool);
        attr.read(in);
        attributes.addElement(attr);
    }
}


Listing Three
/** Reads the constant pool into a Vector. If some error occurs a null will 
  * be returned. The input stream should be positioned at the first byte in 
  * the constant pool
  * @param name The class name
  * @param in The input stream containing the class file
  * @param count The constant pool count
  * @return A vector containing each entry in the constant pool
  */
protected Vector readConstantPool(String name, DataInputStream in,
                           int constantPoolCount) throws IOException
{
    // Loop through the constant pool and build a vector containing
    // all of the entries
    Vector entries = new Vector();
    for (int entry = 1; entry < constantPoolCount; entry++) {
        // Read the next constant pool tag
        byte tag = in.readByte();
        ConstantInterface constant;
        // Create the proper type of constant object
        switch(tag) {
        case Tags.CONSTANT_Utf8:
            constant = new ConstantUtf8();
            break;
        case Tags.CONSTANT_Integer:
            constant = new ConstantInteger();
            break;

        case Tags.CONSTANT_Float:
            constant = new ConstantFloat();
            break;
        case Tags.CONSTANT_Long:
            constant = new ConstantLong();
            break;
        case Tags.CONSTANT_Double:
            constant = new ConstantDouble();
            break;
        case Tags.CONSTANT_Class:
            constant = new ConstantClass();
            break;
        case Tags.CONSTANT_String:
            constant = new ConstantString();
            break;
        case Tags.CONSTANT_Fieldref:
            constant = new ConstantFieldref();
            break;
        case Tags.CONSTANT_Methodref:
            constant = new ConstantMethodref();
            break;
        case Tags.CONSTANT_InterfaceMethodref:
            constant = new ConstantInterfaceMethodref();
            break;
        case Tags.CONSTANT_NameAndType:
            constant = new ConstantNameAndType();
            break;
        default:
            System.out.println("Unknown Tag " + tag);
            return null;
        }
        // Read the constant entry
        constant.read(in);
        // Add the constant to the pool
        entries.addElement(constant);
        // Now adjust for long and double. For obscure reasons, when an entry 
        // is tagged CONSTANT_Long or CONSTANT_Double the JVM
        // considers this a taking up two entries.
        if ((tag == Tags.CONSTANT_Long) ||
            (tag == Tags.CONSTANT_Double)) {
            entry++;
            entries.addElement(new BaseConstant());
        }
    }
    return entries;
}


Listing Four
/** Reads the entry from the input stream
  * @param in The input stream
  */
public void read(DataInputStream in) throws IOException
{
    value = in.readUTF();

}
/** Writes the entry to the output stream
  * @param out The output stream
  */
public void write(DataOutputStream out) throws IOException
{
    // Write the tag byte
    out.writeByte(getTag());
    // Write the string
    out.writeUTF(getString());
}


Listing Five
public Connection getConnection(String url, String user, String password)
    throws SQLException
{
    Connection connection = DriverManager.getConnection(url, user, password);
    return connection;
}

Listing Six
/** Contains static logging methods */
public class StaticLogger
{
    /** Logs the elapsed time for a method
     * @param className The class name
     * @param methodName The method name
     * @param elapsed The elapsed time for the method
     */
    public static void logTime(String className, String methodName, long elapsed)
    {
      System.out.println("Method "+className+"."+methodName+" took "+elapsed+"ms");
    }
}

Listing Seven
public Connection getConnection(String url, String user, String password)
    throws SQLException
{
    long starting_timer = System.currentTimeMillis();
    Connection connection = DriverManager.getConnection(url, user, password);
    long elapsed = System.currentTimeMillis() - starting_timer;
    StaticLogger.logTime("myclass", "getConnection", elapsed);
    return connection;
}

1


