Robert Muths Better Bash Scripting

Sunday, April 20th, 2014

After using bash for years, its still good to learn new things 🙂

Most of my scripts still use back ticks too.  I should really get around to fixing them soon along with everything else on my long todo list.

Netbeans log code template

Tuesday, May 3rd, 2011

For some years I have been adding this code template to Netbeans.

private static final Logger log = Logger.getLogger(${classVar editable="false" currClassName default="getClass()"}.class.getName());

You can add this by going to Netbeans->Tools Menu->Options->Editor->Code Templates dialog.

Netbeans Code Templates

Edit @ 29/08/2011
In newer Netbeans you must ensure that the “On Template Expansion:” is set to “Reformat text” else it gets extra new lines added in the code template.

${no-format}private static final Logger log = Logger.getLogger(${classVar editable="false" currClassName default="getClass()"}.class.getName());

Indent/Format the whole buffer in Emacs

Monday, February 8th, 2010

I have had an indent-buffer command like this for some time, which formats the whole current buffer. I used it for java, c++, css, html, xml and lisp.

(defun indent-buffer ()
    "Indent the current buffer"
    (save-excursion (indent-region (point-min) (point-max) nil))

I found the above function at

Recently I noticed that it was leaving tabs in (which I hate) and trailing spaces.  It was simply just indenting the buffer.

After some more searching today I found the untabify command and I created the following commands.  Mainly because I expected the command to be called something like tab-???

(defun untabify-buffer ()
    "Untabify current buffer"
    (save-excursion (untabify (point-min) (point-max)))

(defun tab-to-spaces ()
    "Convert tab to spaces"
    (save-excursion (untabify (point-min) (point-max)))

I then found this page, which has the best indent/format buffer function.

(defun indent-buffer-2 ()
    "Indent the buffer 2"
        (indent-region (point-min) (point-max) nil)
        (untabify (point-min) (point-max))

JVMTI IterateOverReachableObjects and very poor documentation.

Wednesday, March 25th, 2009

As part of my daily work I’ve been working with Steve to write a heap parser so that our tests can find any memory leaks without the need to attach a profiler and hunt though the results.

We created a jvmti agent which could tag an object on the heap then find out if the object was reachable via any GC roots.

This worked well and we had no problems for over a year but recently we started to get some strange array index out of bounds.

The cause was the very poor documentation of the jvmtiObjectReferenceCallback call where the field information was being calculated.

The documentation was wrong in 1.5 but even in 1.6 it simply does not provide enough information to write a heap walker which can print the field names.

I.e for the JVMTI_REFERENCE_FIELD reference kind it says the following:

Reference from an object to the value of one of its instance fields. For references of this kind the referrer_index parameter to the jvmtiObjectReferenceCallback is the index of the the instance field. The index is based on the order of all the object’s fields. This includes all fields of the directly declared static and instance fields in the class, and includes all fields (both public and private) fields declared in superclasses and superinterfaces. The index is thus calculated by summing the index of field in the directly declared class (see GetClassFields), with the total number of fields (both public and private) declared in all superclasses and superinterfaces. The index starts at zero.

This may seem ok but how do you parse the super classes and super interfaces?  The documentation states nothing about the order of the fields so do you scan all interfaces first or last, do you walk the super classes once for classes and once for interfaces?

Being very vague (even google didnt know much) it was difficult to get the output, when a leak was detected, to show the correct field.

It would randomly output fields which were the wrong type and many other issues.

After days of trial and error I was about to give up, then I found hprof_reference.c via google, and then found the rest of the hprof source as part of the JDK directory.

After looking though this code it was possible to figure out exactly how the class and interface heirachy should be processed.

How are we expected to use the JVMTI when its so poorly documented?

My emacs startup script for cygwin

Tuesday, March 24th, 2009

This is my emacs script for cygwin. It runs the Windows Emacs version and runs just 1 instance of emacs in most cases.

Typing emacs on its own simply starts a new version of emacs.

Type emacs and a file will try to open the document in a running version if its started else it will start emacs.


if [ -z $1 ]
    d:/apps/emacs-22.2/bin/emacsclient.exe -n \
    -a d:/apps/emacs-22.2/bin/emacs.exe $@ \
    >/dev/null 2>&1