Wednesday, 24 December 2008

Java: automating the equals method

I spend quite a lot of time writing Java code. I got to thinking about the time I spent implementing, testing, maintaining and just paging over equals /hashCode implementations. These common building blocks tend to work much the same way in most classes and I wondered if there were a way to make them go away.

Thursday, 2 October 2008

Java: how not to make a mess of stream handling

This article was written with Java 6 in mind.
Updated 2008/12/16.

   * Writes "Hello, World!" to a file.
  public static void main(String[] args) {
    try {
      byte[] data = "Hello, World!".getBytes("UTF-8");
      OutputStream out = new FileOutputStream("output.txt");
      //if write throws an error
      //then close will never be called! BUG!
    catch(IOException e) {

If you aren't careful with streams in Java, you end up with resource leaks. This article addresses some of the pitfalls of stream handling.

The examples that follow use implementations of OutputStream. Which output stream is not important - implementation details are encapsulated by the object and may change between Java versions. It is the developer's responsibility to honour the contract as per the documentation.

OutputStream.close(): Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

New Java developers pick up pretty quickly that they have to close streams. Problems arise when they start thinking about how to do it.

Monday, 18 August 2008

Ant: automated deployment to WebSphere Application Server

It can be useful to automate deployment of enterprise applications to servers during development, either to automatically set up test builds or perform build verification during the kitting process. WebSphere Application Server (WAS) comes with a number of Ant tasks that can be used for this.

Tuesday, 5 August 2008

Java: int versus Integer

Changes in the Java language have made the differences between int and java.lang.Integer less obvious but every Java developer should understand them. Unless otherwise stated, Java 7 syntax and types are used.

Many of these issues apply to all the primitive types and their wrapper types.

This post has been rewritten in 2013. The original post was still generating comments (not the good kind) five years after it was written. This post is more detailed and provides better examples. Old comments have been deleted to avoid confusion - new criticism is welcome.

Friday, 1 August 2008

Java: using XMLBeans to edit web.xml

XMLBeans is a Java API and set of utilities for working with XML. The code that follows demonstrates how to use it to manipulate a J2EE web.xml (version 2.5) document.

Thursday, 31 July 2008

Java: determining the version of an XML document

When working with XML in Java, it is not uncommon to want to work with the data using strongly typed objects that enforce the document structure. That is, you want to use Java Beans rather than a Document Object Model (DOM).

A number of technologies can be used to automate this transformation, such as the Apache Commons Digester (a rules-based entity mapper) or XMLBeans (which provides schema-based bean generation).

Monday, 30 June 2008

Java: class names

In Java, it is possible to get a String representation of a class by calling the Class.getName() method. Since Java 1.5 (AKA Java 5), it has been possible to get the canonical name using Class.getCanonicalName().

Monday, 26 May 2008

C: hex dump application

A C version of PrintHex to avoid a dependency on Mono.

Sunday, 25 May 2008

C: Hello, World!

gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;

Thursday, 1 May 2008

Java: generating a DOS executable

By incrementally changing instructions in an assembly language, it is possible to build up a picture of how the bytes relate to the assembly instructions (without going to the trouble of actually reading the documentation). Note that, for these trivial examples at least, there is no difference between the binaries generated by NASM and the equivalent MS Debug instructions.

Assembler: Hello, World! [2]

NASM version 0.98.39 compiled on Feb 25 2005
DOS, 16bit, x86

;syntax expected by NASM
;DOS, x86, 16bit
org 100h        ;start at address 100
mov ax,0200h    ;AH=2
mov dx,0048h    ;'H'
int 21h         ;int 21,2 (print char)
mov dx,0065h    ;'e'
int 21h
mov dx,006Ch    ;'l'
int 21h
mov dx,006Ch    ;'l'
int 21h
mov dx,006Fh    ;'o'
int 21h
mov dx,002Ch    ;','
int 21h
mov dx,0020h    ;' '
int 21h
mov dx,0057h    ;'W'
int 21h
mov dx,006Fh    ;'o'
int 21h
mov dx,0072h    ;'r'
int 21h
mov dx,006Ch    ;'l'
int 21h
mov dx,0064h    ;'d'
int 21h
mov dx,0021h    ;'!'
int 21h
int 20h         ;int 20 (terminate)

Compare and contrast with the debug.exe version.

Assembler: using debug.exe to write DOS programs


Windows (XP) still comes with Debug.

0C8A:0100 mov ax,0200
0C8A:0103 mov dx,0041
0C8A:0106 int 21
0C8A:0108 int 20
-h 010A 0100
020A  000A
CX 0000
Writing 0000A bytes


Assembler: Hello, World!

DOS, x86, 16bit, debug.exe

mov ax,0200
mov dx,0048
int 21
mov dx,0065
int 21
mov dx,006C
int 21
mov dx,006C
int 21
mov dx,006F
int 21
mov dx,002C
int 21
mov dx,0020
int 21
mov dx,0057
int 21
mov dx,006F
int 21
mov dx,0072
int 21
mov dx,006C
int 21
mov dx,0064
int 21
mov dx,0021
int 21
int 20

Wednesday, 30 April 2008

C#: file hex dump application

A simple application for printing file contents as hexadecimal.

Tuesday, 29 April 2008

C#: Hello, World!

using System;

public class HelloWorld {
        public static void Main(String[] args) {
                Console.WriteLine("Hello, World!");

Tuesday, 22 April 2008

Java: using EL outside J2EE


If you have done much JSP programming, you will be familiar with the Expression Language (EL), also now known as the Unified Expression Language. EL is used in JSPs to help remove business logic from the view while keeping the data content dynamic. Uses for EL can go beyond J2EE platforms and it is relatively easy to incorporate it into your own applications.

Monday, 14 April 2008

Java: finding binary class dependencies with BCEL

Sometimes you need to find all the dependencies for a binary class. You might have a project that depends on a large product and want to figure out the minimum set of libraries to copy to create a build environment. You might want to check for missing dependencies during the kitting process.

Wednesday, 9 April 2008

Java: finding the application directory

EDIT 2009/05/28: It has been pointed out to me that a far easier way to do all this is using this method:

    URL url = Location.class.getProtectionDomain()

...which makes everything below here pointless. You live and learn!

Tuesday, 8 April 2008

Java: synchronizing on an ID

If you are a Java programmer, you will be familiar with synchronized blocks.
  Object myObject = //some instance
  synchronized(myObject) {
   //do some thread-sensitive
   //work on myObject
Sometimes, you want to synchronize on a transient object - a resource that isn't going to stay in memory.

For example, there is nothing in the Servlet 2.5 MR6 specification that says a HttpServletSession instance can't be recreated as a facade object every time it is requested. That makes the session instance a poor candidate for a synchronized lock. There is nothing in the specification that prevents the container implementer from always serializing session attributes as soon as they are set either. That makes session attributes poor candidates for synchronization locks. Note: existing implementations may support either of these approaches in practice, but lets say our imaginary servlet container doesn't. However, the session ID will be consistent across requests.

Java: Hello, World!

public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello, World!");