Archive for January, 2004

Commons-Collections 3.0

In case you didn't notice, Jakarta Commons Collection 3.0
has just been released. I think it is probably fair to say the the reception
it got
wasn't 100% favourable.

While I don't really want to comment on the release (although I can't resist pointing to this), what I do find
puzzling is the lack of discussion on the
commons-dev
mailing list
about this. I would have thought that one developer would perhaps consider the possibility that they should
listen to feedback from their users.

I can understand that they must have built up some kind of immunity to bile by now, but some of the ideas and comments
on TheServerside were… well.. kind of valid..

Comments

JDK1.5: java.lang.instrument

The java.lang.instrument package allows a programmer to modify classfiles
as they are loaded. No API for doing the modifications is supplied, but you
can use ASM or BCEL etc to do that. This example prints out class names as
they are loaded.


package inst;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;

public class Instrument {

    public static void premain(String options, Instrumentation ins) {
    	ins.addTransformer(new Logger() );
    }

    public static void main(String args[] ) {
    	System.out.println("Hello World" );
    }

    public static class Logger implements ClassFileTransformer {
        public byte[] transform(java.lang.ClassLoader loader,
                java.lang.String className,
                java.lang.Class classBeingRedefined,
                java.security.ProtectionDomain protectionDomain,
                byte[] classfileBuffer) throws IllegalClassFormatException
        {
            System.err.println(className );
            return null;
        }
    }

}

It is run using the new javaagent parameter:

   java -javaagent:inst.Instrument inst.Instrument

Output:


    java/lang/StringCoding
    java/lang/ThreadLocal$ThreadLocalMap
    java/lang/ThreadLocal$ThreadLocalMap$Entry
    java/lang/StringCoding$CharsetSD
    java/lang/StringCoding$StringDecoder
    Hello World

I presume that classes like java.lang.String are loaded prior to the agent
class being hooked up. Also, beware that
it is possible to deadlock the classloading mechanism if you aren't careful
- only modifying classes that don't match a regexp like
“java.*|sun.*|javax.*” is a good idea!

Comments

JDK 1.5: Monitoring the JVM

JDK 1.5 contains a number of useful interfaces to monitor the performance of the JVM. This includes
support for both Memory and CPU usage. These metrics are exposed via JMX as well as via plain interfaces
in the JDK.

The code below iterates over the different elements in the memory pool and prints out their
current statistics.


    List mpMBList = ManagementFactory.getMemoryPoolMBeans();
    for (Iterator iter = mpMBList.iterator(); iter.hasNext(); ) {
        MemoryPoolMBean element = (MemoryPoolMBean) iter.next();
        System.out.println(element.getName() + " " + element.getPeakUsage());
    }

Output:


Code Cache initSize = 196608, used = 450112, committed = 458752, maxSize = 33554432
Eden Space initSize = 524288, used = 518712, committed = 524288, maxSize = -1
Survivor Space 1 initSize = 65536, used = 0, committed = 65536, maxSize = -1
Survivor Space 2 initSize = 65536, used = 65528, committed = 65536, maxSize = -1
Tenured Gen initSize = 1441792, used = 42944, committed = 1441792, maxSize = 61997056
Perm Gen initSize = 8388608, used = 1718928, committed = 8388608, maxSize = 67108864

The information you get is similar to what you can get using the
JVM Stat tool in Sun's
JDK 1.4.2 VM.

Comments

Talking about Java 1.5

As you may have noticed, Sun has released an early version of JDK1.5
(which I appreciate very much). Initially Javalobby href="http://www.javalobby.org/members/j2se15.jsp">publicised this
mostly, and insisted that downloaders are bound by a non-disclosure
agreement (which I ranted about href="http://www.mackmo.com/nick/blog/java/?permalink=JDK1.5Alpha.txt">previously).

However, now Sun
have published a link to the download
, with no mention of a NDA. There
is some quite dense language in the click-thru licence about confidential
information, but I don't think that is supposed to be a NDA (INAL,
though). In any case, href="http://java.sun.com/j2se/1.4.1/j2sdk-1_4_1-ia64-license.html">exactly
the same language is used for the JDK1.4.1 download.

It is worth pointing out that the href="http://onesearch.sun.com/search/developers/index.jsp?qt=%2B+%2BTiger&c
ol=javabugs&category=&state=&query=Tiger&submit=+Search+">bug database for
1.5 is public, too.

Anyway, unless someone (preferably someone from Sun) can let me know I
shouldn't, I intend to start talking about 1.5

Comments

Eclipse Modeling Framework

If you are interested in code generation, model driven architecture or in metaprogramming generally, have a look at
Eclipse EMF.

To put it simply, you can create an entire Java GUI application (okay – an Eclipse Plugin) by defining your model in
Java classes
(or it can import a Rational Rose model
or use the
Omondo UML plugin for Eclipse).

I found the existing functionality amazing: Following the tutorial
it took me 30 minutes to have a fully functioning Eclipse plugin that allowed data editing and persistance to XML. The only
code that I needed to write was some interfaces that defined how my data interacted using the JavaBeans model, and everything else
was generated.

The crazy thing is that this application generation I'm raving about is really just a sample for the tutorial – developers
are supposed to use EMF as a tool to generate code however they like. Anyway, if I was considering writing a XDoclet plugin
or something similar right now I would look very hard at using EMF instead.

Comments