Friday, February 27, 2009

Learning Groovy

I have started learning the Groovy programming language. Looks like dynamic languages are getting really popular these days, and keeping up with my resolution to learn a couple of programming languages this year, I have started learning Groovy.

In short Groovy is a dynamic language which fully interacts with Java. Groovy can invoke Java objects, in fact it itself compiles to bytecode, so Java objects can also invoke Groovy.

In the next few posts I will share what I learn and my notes comparing Groovy with Java.

Let's start with a HelloWorld in Groovy


println 'Hello World'


That's it. You do not need to enclose your HelloWorld in a class or the main method. This is really cool if you just want to create a simple script file to do something quickly for you.

But how does this get compiled to Java bytecode. Java always needs a class and methods to hold code. Well in my case the Groovy plugin in Eclipse automatically compiled the above code and put it in a class with the same name as the name of the file containing this code.

In this example you will also notice that:
  • Groovy Strings can be enclosed in single quotes
  • If a method takes only one parameter, we can give it to the method without enclosing the parameter in ()
  • Groovy statements do not need to be terminated with a ;

Friday, February 20, 2009

What is type erasure in Java?

I had read about type erasure in Java a long time back. However, today when a friend asked me a question related to type erasure, I found myself not quite certain of the answer.

I read up on it again and here is what I learned.

Angelika Langer has an excellent FAQ, where she explains generics, and type erasure. According to the FAQ:

A process that maps a parameterized type (or method) to its unique byte code representation by eliding type parameters and arguments.

OK, let's understand what that means. Below is a simple class which uses the generified version of Java Lists.









package net.adaptivelearningonline.examples.generics;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GenericsErasure {
  public static void main(String args[]) {
    List<String> list = new ArrayList<String>();
    list.add("Hello");
    Iterator<String> iter = list.iterator();
    while(iter.hasNext()) {
      String s = iter.next();
      System.out.println(s);
    }
  }
}




I am going to take the bytecode generated by the above class and decompile it using JAD.

Here is the decompiled Java class:









// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GenericsErasure.java

package net.adaptivelearningonline.examples.generics;

import java.io.PrintStream;
import java.util.*;

public class GenericsErasure
{

    public GenericsErasure()
    {
    }

    public static void main(String args[])
    {
        List list = new ArrayList();
        list.add("Hello");
        String s;
        for(Iterator iter = list.iterator(); iter.hasNext(); System.out.println(s))
            s = (String)iter.next();

    }
}





As you can see, the decompiled code has absolutely no trace of any generics information. This is because of erasure. When the Java compiler compiles client code which uses a library that has been generified, the client code is compiled in such a way that all generics information is erased from the bytecode.

This also explains the fact that generics information in client code is meant purely for the compiler to do type checking. Once the compiler has done it's job, this information is discarded.

The reason why Java uses type erasure is to maintain backwards compatibility. Let's assume you wrote some code a few years back which used Java Lists, before generics was introduced in the language. Obviously your code had no mention of generics. Now, when engineers at Sun decided to introduce generics, they did not want to break code which people had already written. One possible way was to ensure that client code which uses generics in the classes they invoke, never carries any information about generics in the compiled code. So the above class when compiled should not carry any information about generics. Because compiled client code never carries information about generics anyways, library implementers are free to add generics to their code without the worry of breaking anyone's old code.

Bruce Eckel has written a rather lenghty but good article on generics and erasure.

Thursday, February 19, 2009

Bit manipulation in Java

I have always had a hard time remembering rules of bit manipulation in Java.

So, when someone asked this question on Stackoverflow.com, I knew he had to do masking, but I forgot why.

I decided to look up bit wise operations on Wikipedia to refresh my memory. However, this time I am also blogging the answer so I don't forget (yet another use of blogging :-) ).

There are two types of bit shift operations: arithmetic shift, and logical shift. An arithmetic right shift preserves the sign bit, while logical shifts always insert a zero.

Representing -1 as a signed byte we get: 11111111

-1 >> 1 gives us: 11111111
-1 >>> 1 gives us: 01111111


so by this logic (-1 >>> 8) should give us 00000000 which is 0

Well not so:

byte b = -1;
System.out.println("-1 >>> 8 = " + (b >>> 8));

The output I get is:

-1 >> 8 = 16777215

Hmmm. what just happened? Java converted the signed byte into a signed int, and then did an arithmetic right shift of 8 bits.

So Java converted -1 to a signed 32 bit integer:
1111111111111111111111111111111

Then did a logical right shift of 8 bits getting
00000000111111111111111111111111

which is: 16777215 in base 10

ok, let's try casting the output back into a byte

byte b = -1;
System.out.println("-1 >>> 8 = " + (byte)(b >>> 8));

The output is:

-1 >> 8 = -1

That did not help because the lower 8 bits of the answer are still all 1's, so casting to a byte gives us -1.

ok, so how do I get the 0 I was after? We can get it by bit masking the byte.

byte x = -1;
System.out.println("-1 >>> 8 = " + ((x&0xFF) >>> 8));


Running the above code gives us an output of:

-1 >>> 8 = 0

So what just happened? b is still converted into a signed 32 bit integer, giving us:
1111111111111111111111111111111


but then we mask it with 0xFF, which gives us
0000000000000000000000011111111


Now when we do a logical right shift of 8 bits, we get
0000000000000000000000000000000


I hope this post helps me remember bit wise operations in Java, and I hope it helps you too. If you have a problem remembering things like I do, you may want to create a little example code and blog about it yourself.

Thursday, February 12, 2009

Automated release tagging with ANT

Sometime back I was researching how to automatically tag a CVS snapshop from an ANT script. Yes, I know this should be a basic thing :-) But for some reason, IT has always taken care of tagging and creating builds. Whenever I needed to release some code, I would tag the CVS snapshot from Eclipse, I would manually creat a MANIFEST.MF file, and then create the distributable.

However, this time I tried to automate the task with an ANT script.

What I needed was an ANT script which would automatically create a MANIFEST.MF file with a release number, and tag the CVS project with that release number.

The first thing I came across was the Ant cvs task. This task allows us to run any CVS command on a repository. However, there is a catch. You need CVS installed on your local machine for this to work. Under the hoods the CVS task invokes cvs.exe (on Windows), or cvs on *nix machines. This was a clear show stopper. I did not want to make all developers install CVSNT on their development machines.

So, I started looking for CVS libraries implemented in Java. There are a couple of them. I decided to settle with javacvs which is created by the Netbeans folks. javacvs is a CVS client implementation written purely in Java. If you are interested, here is a javacvs quick start tutorial. It outlines some non trivial steps for setting up and using javacvs.

I downloaded javacvs and wrote some code to tag my repository. However, when I ran it, the library threw a NullPointerException. The javacvs folks have not created Javadocs for their projects, so the only option I had to figure out what was wrong, was to look at their source code. I followed a link from their page to download the sources. However, the page suggested that the sources were moved to a Mercurial repository. They were also merged with the Netbeans source, so I would now have to download all of Netbeans to get the javacvs sources.

Ok, so I installed Mercurial and ran this to get the Netbeans sources.

hg clone http://hg.netbeans.org/main/

It seemed to be doing something, but it was also taking forever. After downloading some 350 MB of stuff, it still seemed to be getting more. The command line did not give me any status message, so I had no clue how much was downloaded and how long it would take. To make matters worse, we had some network problems on that day and the Internet was really dragging. After a couple of hours, I gave up and aborted the checkout. After coming home I tried to resume the download by running the above command, but it failed. Looks like partially cloned Mercurial repositories cannot be resumed. I would have to delete all the downloaded files and restart the clone operation.

Meanwhile, I came across a class called CvsCommand in javacvs, which was like a command line for javacvs. It could take any cvs command with arguments and run it on the CVS repository. I ran the following from my project directory. This class was also set to be the main-class of the jar file. So executing the jar as shown below results in the main method of CvsCommand being invoked.

java -jar org-netbeans-lib-cvsclient.jar tag b20090211_01

It almost worked. It read CVSROOT details from the ./CVS/Root file, but it complained that it could not find the password for my CVS account. Some research suggested that I needed a .cvspass file in my home directory. But how was I to create a .cvspass file? It had to be created automatically. I copied .cvspass from my Linux machine into the home directory of my Windows machine. But javacvs continued complaining about the password. So, I manually modified the ./CVS/Root file in the project directory and added a password to the cvsroot string. The format is

:pserver:user_name:password@cvs.hostname/home/cvs/project

Finally everything worked. Running the program automatically tagged the CVS repository with a releaseNumber.

I could run the above command using ANT's java task. However, I still had to manually specify the build number in the Ant script. Ant also has a buildNumber task, which can create incremental numbers. This task relies on a file to store a number. Every time it is run it takes the number from the file and saves it in memory in an Ant property called build.number. At the same time it also rewrites the file with an incremented number. By default it uses a file called build.number in the same directory as the Ant script. However, we can specify another file if we want. Ok, so this gave me incremental numbers, but I still wanted to use a timestamp in the build number. Ant has a task called tstamp for just that. When run, it's default behavior is to store the current time (yyyymmdd) in an Ant property called TODAY. I could concatenate the output from tstamp and buildNumber to create a release tag.

I still wanted to automatically create a MANIFEST.MF file with the release number. Ant also has a manifest task, which creates a MANIFEST.MF file from supplied parameters.

Here's a simple build file that uses Ant's buildNumber, tstamp, manifest, and java tasks to automatically tag CVS with a release number and create a manifest file. To make a release, run this file first followed by your regular ANT script which creates the distributable file.

<project name="MyProject" basedir="." default="tag">
  <target name="build_number">
    <tstamp />
    <buildnumber/>
    <property name="project.build.number" value="release_${DSTAMP}_${build.number}" />
  </target>

  <target name="generate_manifest" depends="build_number">
    <manifest file="WebContent/META-INF/MANIFEST.MF">
      <attribute name="Built-By" value="${user.name}"/>
      <attribute name="Product-Name" value="MyProject" />
      <attribute name="Date" value="${TODAY}" />
      <attribute name="Tag" value="${project.build.number}" />
      <attribute name="Class-Path" value="" />
    </manifest>
  </target>

  <target name="tag" depends="generate_manifest">
    <echo message="tagging with build number ${project.build.number}" />
    <java jar="lib/org-netbeans-lib-cvsclient.jar" fork="true">
      <arg value="tag" />
      <arg value="${project.build.number}" />
    </java>
  </target>
</project>

Thursday, February 05, 2009

Getting interested in polyglot programming

From the last few days I have started learning Groovy by listening to the Groovy series podcasts. While listening to the podcast on numbers (attached_code), I couldn't help smiling when I saw this (line 42 in the attached code):


assert 1/2 == 0.5


Yes 1/2 is actually 0.5 in Groovy. Isn't that awesome :-) We all know what we get when we write this code in Java:

System.out.println("1/2 = " + 1/2);

A few other things I found interesting about Groovy numbers are:

def bigDecimalObj = 5.12345
assert bigDecimalObj.class.name == 'java.math.BigDecimal'

Groovy treats all floating point numbers as BigDecimal (there are exceptions, but I will not get into that here). We all know how difficult it is to implement high precision calculations in Java. We cannot use double, because we may lose precision in calculations involving double. The alternative of using BigDecimal results in unreadable code.

However, Groovy can be compiled to bytecode and can coexist with Java. So now we can use the best tool that fits the job. We can continue using Java for most things, but places where we need to do high precision calculations, we can encapsulate that code into Groovy classes.

Slowly but surely I am beginning to see the benefits of polyglot programming.

Tuesday, February 03, 2009

Coding and learning and sharing and earning :-)

Sometime in 2005 I created a website to share practical information on software programming and design. The idea was to share practical programming tips, best practices, howtos, gotchas, etc. The material would come from what I learn writing actual code, and the medium to share it would be a blog, audio, and screencasts.

I started this venture with the intention of doing something I enjoyed, and generating revenue. The website grew steadily, though very gradually. After about a year and a half I had about 27,000 page hits a month, and a few comments per post. I know it is very modest, but I believe it was at a point where it would have grown much more rapidly form there on. But due to various reasons, I was not able to dedicate enough time to working on the website, and eventually I shut it down.

Even though I shut down the website sometime last year, the desire to be able to work on open source software (that has a social impact), share information, and generate revenue from it, persisted.

I started this blog a few months back as the first step towards this goal. I also changed my work pattern and commitments to support this goal. I do actual work (work that pays) for part of the month, and I spend the remaining time keeping my knowledge current (through books, blogs, and podcasts), experimenting with code doing coding katas (right now I am learning Groovy, so my katas are pretty much at the Hello World level in Groovy), writing (this blog), and well... thinking and researching about how I can make all this work.

To be honest, I am very positive that type of work is feasible, but it will need lots of patience and perseverence. No problems, I have both :-).

However the fact is that even though I am seeing an increase in page hits and recent posts have started getting comments, things are not moving ahead at quite the speed I was hoping (in terms of the number of readers and interaction on individual posts). But then again I do not post as regularly as I should. Sometimes I post twice a week, while sometimes two weeks go by without any post at all. This is not good. I have to be more consistent. I am thinking that a frequency of 2 - 3 posts a week is maneagable for me. I think it is also a frequency people would find useful without getting overwhelmed (with too much quantity). If you have found my blog useful, I need your help. Please tell me what frequency of posts would be most useful to you.

My posts have just started attracting comments, so I assume that people do read them and find them useful. Please remember that if you leave a comment, it helps me realize that there are people reading this blog, and to a large extent it motivates me to write regularly :-)

I have a Google Group for programming, and design related discussions, but their isn't much activity on it. Again I am to blame. I should post more frequently to the group and share useful information from time to time. I promise to share more stuff on the group as well.

Sometime back I started @coding_insights, a Twitter feed to share small and useful bits of information related to programming. Some people have found it useful and follow the feed. I am planning to publish as least 1 - 2 Tweets a day.

So along with doing actuak programming I try and share what I learn through this blog and through my Twitter feed. I think it is time to add one more thing... a long time passion of mine... podcasts. I plan to start publishing a podcast sometime next month, and it's going to be all about coding. I will publish a small program which either explains some API, so a coding or design best practice, and then create a podcast to explain the code. If you think such a podcast will be useful, do give me your feedback about what you would like to listen and how much you would like to listen. By how much I mean the length of the podcast. What should be the optimal length of the podcast so that it can deliver value without distracting people from their work.

I will look forward to your feedback. Also if you have any other thoughts, do share them as well.

Sunday, February 01, 2009

Creating Boolean Objects

Have you ever written this line of code?


Boolean b = new Boolean(false);


According to FindBugs (and rightly so) should not instantiate Boolean objects in Java as we did above. Instead we must use the creational method provided to us.


Boolean b = Boolean.valueOf(false);


This is better for performance. Since we can have only two Boolen values, either true or false, the JVM can cache both these objects, and we can reuse them across our application by using the above creational method, instead creating new Boolean objects every time we need them.

Discuss this post in the learning forum.