Tuesday, September 26, 2006

NetBeans Plugins I Use, Part 5: Build Monitor

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


(Parts
1,
2,
3, and
4.)



The Build Monitor plugin for NetBeans allows you to see the status of continuous builds, right from within the IDE. The Build Monitor works with several continuous build systems - CruiseControl,
Hudson,
and of course the NetBeans continous build.
You can monitor multiple build sources - all you do is name your build and point to an RSS feed where the plugin can fetch the build status. Each build is then displayed right in the IDE status bar. You can set the polling frequency yourself; I've set mine to check every minute! Here's what this looks like:





This is obviously what you don't want to see - while the NetBeans continuous build is fine, the second continuous build is currently failing, as indicated by the red ball. In this case, just click on it to open the browser for the corresponding build status page - where you can for example click on the Console Log to see the build script output.






You can grab the Build Monitor plugin from the Beta Update Center, or from nbextras.org.



Yesterday, I set up a continuous build for the project I'm working on. It was really simple. All I had to do was download
glassfish, run its setup script, download Hudson, and drop the hudson.war file into the autodeploy directory in Glassfish. Then I just went to localhost:8080/hudson and from there I could set up a build using a simple web interface. I just needed to point to my CVS server (Subversion is supported too) and my ant scripts, and voila - continuous builds. If you want to take a look at what Hudson looks like in action, visit this page where you can see Glassfish itself being built in a Hudson setup.



Tips:


  1. It's a bit tricky to configure the build monitor. The trick is to open the options, then go to the Advanced Options, locate the Build Monitor item, right click on it, choose new build monitor. Then set the RSS feed to the build failure (or all builds) feed you want to use - Hudson displays it right in the status page.

  2. I had to restart the IDE before the build monitors showed up in the Status Line. We should set the needs-restart property of the auto update bundle.

  3. I had problems using the most recent snapshot of Hudson so I grabbed a slightly older version and things were fine.




Thanks to Tom Ball and Jesse Glick for this plugin.


Wednesday, September 20, 2006

Working with classes that (unfortunately) call overridden methods from their constructors

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


I was struggling with a problem today, and I found a pretty decent workaround. Since initial googling hadn't turned anything useful up, I figured this writeup might help others who search for it in the future.



In essence, the problem boils down to "How can you initialize your own object before the superclass constructor has run?"



Let's express this in code - I've changed the example to a simple domain to make the intent obvious:



/** Represents a building, such as a house, a garage, etc. */
public class Building {
public Building() {
// Do something with the result of getColor(), such as
reservePaint(getColor());
}

protected abstract Color getColor();
}


The above class is the "framework" class I'm trying to extend. I do not have the option of changing it or extending something else; this is the integration point.



Note the error above: the constructor is calling an overridden method. This is a
well
documented
problem. Previous uses of the above class had probably not run into it, because most usages would be something like the following:


public class Skyscraper extends Building {
protected Color getColor() {
return Color.LIGHT_GRAY;
}
}


Since the overridden method typically returns a constant that is independent of the subclass object being properly constructed, calls from the superclass constructor did not cause problems.
The problem was that in my case, I'm dynamically constructing many different types of Buildings, so I want to parameterize the getColor() method:



public class PaintedGarage extends Building {
private Color color;

public PaintedGarage(Color color) {
this.color = color;
}

public Color getColor() {
return color;
}


Unsurprisingly, this fails at runtime.
(When you construct the PaintedGarage object, the superclass constructor, Building(), will be called before the PaintedGarage constructor is called, and when it
calls the getColor() method, it will read the color field which has not yet
been initialized.)



With that long motivation: What can we do about this? Following the Effective Java practice of not calling overridden methods from the constructor is not an option. Remember, that is done in the superclass, and that is a class we have no control over - it's part of the framework we are plugging into.



In an earlier version of this, I was already computing classes on the fly (using a byte code generator library), so it was simple for me to simply inline the literal return value in an overridden version of getColor.



However, I no longer need to do that for other purposes, so is there a way for me to ensure that getColor gets initialized by the time the super class constructor calls it?



Yes!



The trick is to use an inner class. The process is as follows.
First, file a bug against the framework you are using to get the constructor behavior changed.
Second, try something like this:



public class PaintedGarageFactory {
private Color color; // Moved out of PaintedGarage and into enclosing (not super!) class

private PaintedGarageFactory(Color color) { // Use factory method below
this.color = color;
}

class PaintedGarage extends Building {
public PaintedGarage() {
}

public Color getColor() {
return color;
}
}

public static PaintedGarage create(Color color) {
PaintedGarageFactory factory = new PaintedGarageFactory(color);
// Notice how the outer class is fully constructed before the
// inner class (and its superclass constructor) is built.
// Each factory instance constructs PaintedGarages of a particular
// color (and could be reused etc.)
return factory.create();
}

private PaintedGarage create() {
return PaintedGarage();
}

}


What this has done is move the parameters that need to be initialized before the superclass
constructor is run, out to the outer class. These are obviously initialized before the innerclass
is constructed.



This is not a good way to write code, but it solves the immediate need: You've
provided an implementation of the super class that is properly constructed as far as the
superclass constructor is concerned.



Don't forget to get the framework fixed.


Monday, September 18, 2006

Tabs Are Evil, Part 3

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


For part 2, see here.




We've got a new

forum
set up for the Java Posse. There's quite a bit of activity there already. One entry in this thread argues once again that there is nothing wrong with tabs and that they are a good way to express indentation.
(We keep mentioning that Tabs Are Bad on the show). I've gotten similar feedback from my previous anti-tab blog entries.



A couple of weeks ago I was taking a look at the findbugs source code, and here's what I saw:






Findbugs follows a coding style where it uses tabs for indentation. In the above you can see that the tabs are really standing out since I have them highlighted with the NetBeans fixtabs module.



The thing to notice is that there are lines where spaces are (probably accidentally) used for indentation. For example, line 2, the beginning of the method signature. And also on line 3, where there is first a tab, then eight spaces, to indent the throws clause.



If this source file is interpreted with tabs expanded to anything other than 8, the code will not be aligned properly. And this is precisely what is problematic about Tabs. Tabs, in most editors, are visually indistinguishable from spaces. Thus, when you're editing, you can't tell that you've accidentally just indented a line with spaces. Obviously, developers might do this by accident, since they already hit the spacebar to insert whitespace between symbols. You can't have the IDE automatically insert tabs instead of spaces. But you can certainly do the opposite. And if you do, you'll avoid problems like these.


Thursday, September 7, 2006

Welcome JRuby

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


Sun has hired the two primary JRuby developers,
Charles Nutter and
Thomas Enebo.
This is great news for JRuby, because it will now be their full time job to work on it.
(JRuby is an implementation of the Ruby language that runs on top of the Java platform.)



This obviously fits well with our strategy to support multiple languages on the JVM, and in particular,
dynamic and scripting languages. In fact,
Charles and Thomas is joining the group I'm in, so hopefully a lot of the tool support we have built to support BASIC will also benefit JRuby. Going forward there will probably be a lot of cross pollination.



Welcome to Sun, Charles and Thomas! Charles has already blogged about joining Sun.