Monday, December 11, 2006

Another NetBeans+Ruby Preview

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.


There have been several great releases lately, and I'm remiss in covering them. However, they've been covered in great detail elsewhere. First, NetBeans Visual Web Pack shipped on schedule, bringing Creator functionality to NetBeans, along with new support for Java EE 5. Second, Mustan... ^H^H^H^H Java SE 6 shipped today. It's a great release. The number one reason I install it whenever I get to a new system is that I need the improved font aliasing built into Java2D - and of course, the gray-rect-fix. However, there's another reason I crave the release even when I'm on OSX: @Override. Yes, @Override has been there since Java SE 5, but in Mus... Java SE 6, you can use it to indicate that a method should implement an interface method, not only a method inherited from a super class. (Which arguably @Override should have meant all along.) Now if only Apple could ship an update to its dated September pre-release...



Anyway, I have an excuse for being late to blog. In addition to some work on JSR 273 which is coming along nicely, I've been coding away to get more Ruby tools support for NetBeans.
Charles and Tom will be demoing JRuby 0.9.2 (which just shipped, congratulations!) at JavaPolis this week, and showing the NetBeans tooling in the process.



I thought I would share another screenshot with you at this point. This shows the new language embedding. Notice how there's Ruby inside string literals, rdoc tags inside comments, string escape codes highlighted specially, and so on.






This is all made possible by the new Lexer API in NetBeans. It's really nice to build on top of it; writing incremental
lexers is a snap. This is part of NetBeans 6. If you've been using recent milestones, you might have noticed that all our javadoc is highlighted in your Java files. This is not done through hacks in the Java lexer - it's done through embedded sections where the Java comment tokens are then sub-tokenized by a Javadoc lexer. It's not just done for javadoc - if you look at your String literals you'll notice the same String-sequence highlighting that I'm showing for Ruby above. It only changes the font to bold, so it's a bit subtle. Perhaps something we should tweak before the release.



In Charles' pre-review of the Ruby support,
he wrote some very nice things about me ("Tor Norbye is a programming machine", etc.) Thanks Charles! While it's nice to get all the credit, I really want to point out that this is all made possible
because of the tremendous work on the editing infrastructure for NetBeans 6, by a number of
great NetBeans engineers. Not just the Lexer, which I've described above, but all the other things I've relied on, such as the
infrastructure for semantic highlighting, asynchronous parser task scheduling, and so on. I would love
to give individual credit here, but I'm not sure exactly who to attribute all the work to, so I'd rather credit
all of you rather than snub anybody. Not to mention the fact that I have an excellent Ruby AST to work with, thanks to JRuby!



Several of you asked after my last screenshot where or when you can download the code for this. It will show up in NetBeans soon, but since the work started in closed source, I have to follow the established open-sourcing process. It's in progress, but I can't make specific predictions about when it will be done.

Tuesday, December 5, 2006

Posse Night

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.


We had a fun in-person recording session for the Java Posse last night over at Joe's house. This was hopefully the first of many to come, since Dick started working at Google yesterday, and is in the process of moving from Georgia to California. Since we're all swamped with work and life it was definitely an unprepared podcast, but we eventually got into some technical discussions. And we accumulated more clips for the outtakes reel.



Here's the crew, from right to left: Dick, Carl, Joe, me.






(Other sizes at Joe's flickr page).


Wednesday, November 29, 2006

Semplice Is Dead. Long Live Semplice!

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.


My co-conspirators on Project Semplice, John Kline and
Herbert Czymontek, have both recently left Sun. Rest assured, I'm not going anywhere. But where does that leave Project Semplice?



Well, the spirit of the project was to support scripting on the VM. We started our research with BASIC, but the vision all along was to design things in such a way that we could support multiple scripting languages, without starting from scratch each time. The tooling aspect was my responsibility, and I'm happy to say it has not been in vain. With the hiring of the JRuby developers, I've been focusing on how to adapt everything to support Ruby, and it's going really well.

I'll post a second blog entry on that shortly.



Back to BASIC. Like most software at Sun, open source is the obvious strategy and we have started the process. On the other hand, just "dumping code on the community" is not likely to be successful, so we're looking for interest and support to decide how to proceed.



In any case, as the blog title suggests, the project is not dead. It was a research project, we've learned a lot, and the technology is now forming the basis for some great new scripting tools coming your way very soon. Rising out of the ashes, so to speak! We'll have more to show at JavaPolis in a few weeks, so keep your eyes peeled!


NetBeans + Ruby = True

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.







Click on image for full size. Some things to notice: semantic highlighting (for example, parameters are shown in different colors than local variables), code completion, mark occurrences (other uses of the length method under the caret is highlighted), ...



You'll see more at JavaPolis... and on this blog!


Sunday, November 12, 2006

Java Open Sourced - Podcast Special

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.


It seems I'm late to join the blogging party; just about everybody has covered the news already. In the unlikely
case that you haven't read about GPL'ed Java, read just about any blog on blogs.sun.com (and many on java.net). The main resource page is http://www.sun.com/software/opensource/java/.



We've got a special episode dedicated
to this on the Java Posse. It's an hour interview with
Mark Reinhold,
Rich Sands and
Eric Chu
discussing just about everything related to this. Please check it out
(and digg it).


Thursday, November 2, 2006

Using JDK 5 language features in Creator

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.


As you're probably well aware of, there were several important releases this week. First, NetBeans 5.5 was
released. This has been covered extensively by other Sun bloggers
so I won't repeat the highlights here - just check it out if you haven't already.



Second, a number of add-on packs for NetBeans 5.5 were released. A pack is a cluster of plugins.
One of these is the

Visual Web Pack
, which basically bundles all the visual web page design functionality from Creator into NetBeans. With this, you can create pages Creator style alongside other NetBeans features, such as the profiler, or the UML tool, or the enterprise XML support.



There's another big reason why even Creator users should try the visual web pack (which is in technology preview state, not a final release like NetBeans 5.5.). It offers new support for Java 5 language features, as well as Java EE 5 support. I took it for a quick testspin and thought I would show it in action here.



If you download NetBeans 5.5, and then the Visual Web Pack installer, you can create Visual Web Projects. Unfortunately, out of the box, it will only offer to create J2EE 3 and J2EE 4 projects. The reason for this is that NetBeans is only bundled with the Tomcat web container, and Tomcat is not adequate for Java EE 5 projects. Thus, the trick is to also download a Java EE 5 capable app server such as Glassfish or its cousin, the Sun Application Server. Once you've done that, and you select it as the target in the new project dialog, you are able to create a Java EE 5 project. Hopefully we'll have everything ready as a single bundle so that out of the box, you're defaulting to Java EE 5 projects and Java 5 source. Here's the configuration portion of the New Project wizard (click Manage to register additional web servers).






Let's create a simple web app which utilizes Java 5 features. It will be a simple page which lets you provide the name of a Java class, and then the methods (possibly generic) of the class are displayed. Here's what the web app looks like in the designer:






Here's the button click event handler:






Note that we simply use the error method to log messages for the page; these are all displayed in the group message component. This is a very simple way to log messages for ourself during debugging. You should always have a Message Group component, on every single page of your application, during development. This will make it immediately obvious if you have validation errors or other problems that might otherwise silently occur and leave you scratching your head.



When we run and try it with some sample data, here's the result which shows Java 5 language features in action:






The next step is to play with Java EE 5 features mixed with JSF. That will have to wait for another day; I've gotta get back to my day job, which I'll blog about soon.


Sunday, October 15, 2006

Code Advice #14: Don't initialize fields to default values

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.


(See intro for a background and caveats on these coding advice blog entries.)



If you've been coding in C, you've probably picked up the habit of initializing all your fields:


char *foo = NULL;
int bar = 0;




This is necessary, because in C, memory can be left uninitialized, so there's no telling what value foo will have before it is assigned something.



In Java, however, the language specification clearly defines default values, so virtual machines will for example always initialize reference fields to null.



Specifically, this means that code like the following is redundant:


private int foo = 0;
private Bar bar = null;
private boolean baz = false;




The alternative form of initializing the fields explicitly in the constructor, is the same:


private int foo;
private Bar bar;
private boolean baz;

public Foo() {
foo = 0;
Bar bar = null;
baz = false;
}



You can leave out the above initializations, and the program will behave the same way. Carl Quinn once convinced me that this was more readable, so I picked up the habit, and I now swear by it. On the one hand, you can argue that leaving the explicit initializations in is more readable because you're making it really clear what it is you are intending. On the other hand, Java programmers quickly learn what the default values are and understand that an uninitialized field is the same as a nulled out field.



It turns out that the two forms are not exactly identical! If you disassemble the above code, you'll find the following bytecode is generated:


4: aload_0
5: iconst_0
6: putfield #2; //Field foo:I
9: aload_0
10: aconst_null
11: putfield #3; //Field bar:LBar;
14: aload_0
15: iconst_0
16: putfield #4; //Field baz:Z



If you leave out the initializations, none of the above bytecode is generated. In a simple microbenchmark there was a measurable time difference (about 10%) for the case where a handful of fields were initialized versus leaving them uninitialized, so it's clear that Hotspot doesn't completely eradicate the differences here. (<speculation>Perhaps it just zeroes out the whole object memory block when allocating a new object, relying on the default values to all be represented as zeroes natively, and this is done even when all fields are later initialized?</speculation>)



Obviously, speed considerations is not what should be the deciding factor here. This was a microbenchmark doing nothing other than construct objects with and without initialization - it's unlikely that you'll find a measurable difference on a real program. What really matters is readability. I should also point out that Findbugs will treat these two scenarios differently. If you print out a field value in the constructor, it will warn about an uninitialized field if the field was not explicitly initialized in the field declaration.



I can see arguments both for and against explicit field initialization, but I think this is one of those cases where convention wins. I personally find code cleaner and more readable when you leave your fields with implicit rather than explicit initialization.



P.S. Remember that null fields are often a bad idea and should be initialized to null-objects!