Search

Categories

Links

Referers

OpenJDK desktop background

May 13 2007, 10:05:55 PM PDT »Java Comments [2]
I created an OpenJDK desktop background. My graphic talents are limited, so I hope this can serve as an inspiration for more talented people.

Ask The Experts

May 08 2007, 07:32:50 AM PDT »Java
Jon, Lubo, and I will be at Pavilion booth #1538 Thursday 3.30-4.30pm at JavaOne in the Moscone Center, San Francisco. This is a good chance to come and chat with us about javac.

Suave JavaOne Schedule

Apr 30 2007, 01:23:20 PM PDT »Java Comments [6]

The JavaOne schedule builder has been improved since last year, and it is now easier to resolve conflicts. This is much appreciated. However, the builder should have been written in Java or AJAX to limit the time required to reload the page after adding a session.

Anyways, I just finished building my schedule, and I found the print option to be somewhat lacking. However, I also mailed the schedule to myself and that version did not include all the headers. After printing the mail in two-pages-per-sheet mode, I noticed that I could fit it in my trusted moleskine® notebook:

JavaOne schedule in notebook.

I apologize about the quality; my real camera was stolen on my way to FOSDEM 2007. This picture is from my Razr V3.

JavaOne 2007

Apr 29 2007, 12:40:48 AM PDT »Java Comments [3]

Come to our BOF sessions at JavaOne and hear more about the Java programming language and the Java technology tools (including javac).

BOF-2899: Java Programming Language Features in JDK Release 7
Track: Java SE
Room: Gateway 102/103
Date: 08-MAY-07
Start Time: 22:00
BOF-2898: Java Programming Language, Java Compiler, and Related Tools and API
Track: Java SE; Open Source
Room: Room 105
Date: 09-MAY-07
Start Time: 19:55

One more interview

Apr 06 2007, 09:58:52 AM PDT »Java
Since you are reading my blog you may also find Meet Peter von der Ahé, Tech Lead for javac at Sun Microsystems interesting. For now, the interview is on the front page.

EclipseCon 2007

Mar 06 2007, 09:42:57 AM PST »Java Comments [6]

Alex and I are on stage at EclipseCon 2007 waiting to give our presentation, unfortunately this means that we are missing Scott Adams who we can hear from the conference room next to ours.

So if you're interested in hearing our talk on Java SE 7 language features, now would be a good time to head down to Santa Clara Convention Center. We start at 10:10am.

Java SE 6 Released

Dec 11 2006, 03:00:00 AM PST »Java Comments [1]
Java SE 6 is released today. Read on for my opinion on why now is the time to upgrade. [Read More]

Browsing source code online

Sep 19 2006, 05:22:47 PM PDT »Java
Kohsuke has been playing with all the new compiler API (JSR 199, JSR 269, and the Tree API). He created a cool webbased source code browser. For more details on the project, see the project page.

HotSpot and other compilers

Aug 03 2006, 02:36:07 PM PDT »Java Comments [10]
In a comment on a previous blog entry, Damon Hart-Davis asked if HotSpot is better with classes generated by javac and if it is true that some static transformations (optimization or obfuscation) can impact the performance of your application?

The short answer is that HotSpot is not targeted to javac code but some static transformation can adversely affect the performance of your application. I'll give some more details below.

Damon also mentioned that there had been some fuss about a VM crash caused by some code generated by jikes. I know that all the HotSpot engineers treat VM crashes with utmost urgency and I checked with Ken Russell. Ken remembers one particular crash where jikes used the invokeinterface bytecode to call methods declared in java.lang.Object. This crash was treated with high priority and has since been fixed.

HotSpot is just a piece of software and may contain bugs but that does not mean javac is getting a better deal. However, there are certain factors which can play a role in how good a job HotSpot can do on your application.

Some of the industry benchmarks are based on the combination of compiler and JVM so if HotSpot seems biased towards javac, that may be the reason but not intentional.

On the other hand, many static optimizers will automatically inline method calls. The fundamental problem with inlining method calls is that if the inlined method was never called by method into which it was inlined it will just make the resulting method bigger (and perhaps prevent it from being inlined in other methods). So although a static optimizer think it is optimizing it has no data about how the program behaves at runtime and may make mistakes.

This is a fundamental problem that will make your programs run slower on HotSpot. The name HotSpot comes from the VM's ability to recognize hotspots in the program based on statistics collected while the program is running. By using this data, HotSpot can generally make better decisions than a static optimizer.

Besides hurting HotSpots ability to inline based on live data, big methods resulting from premature inlining can have other negative effects on performance. HotSpot will compile hot methods to native machine code. This code is sensitive to CPU level optimizations such as code caches and branch prediction. I don't know if this is a real life problem.

I checked with Ken and Ross Knippel to see if there could be other problems and they mentioned that obfuscators sometimes makes some very radical transformations of the control flow of your application. This can cause problems for HotSpots ability to detect loops and optimize them. A similar problem could occur if the compiler generated code that looked very different from what javac and similar compilers generate. However, in Mustang, HotSpot will apply a technique called loop rotation to properly place a loop exit that is testing an induction variable. Ross provided this example:

int i = 0;
while (true) {
    stmt1;
    i++;
    if (i >= n) break;
    stmt2;
}

is transformed into

int i = 0;
stmt1;
i++;
if (i < n) {
    do {
        stmt2;
        stmt1;
        i++;
    } while (i < n)
}

JSR 269 @ JavaOne

May 30 2006, 03:50:55 AM PDT »Java
photo: JSR 269 expert group
A few members and hangarounds of the JSR 269 expert group (annotation processing) got together at JavaOne in San Francisco. From the left: Bruce, Joe, Peter, Jess, Scott, and Jon. Bruce had brought delicious Kiwi chocolate all the way from New Zealand.

JavaOne 2006 Slides

May 22 2006, 03:00:00 PM PDT »Java Comments [19]
I have uploaded the slides from the BOF Joe and I did on Best Practices With Generics and Other Java™ Platform 5.0 Language Features (BOF-0160)

JavaOne: questions

May 15 2006, 02:18:47 PM PDT »Java Comments [11]
If there are any topics you would like me to cover in my BOFs on Wednesday, feel free to add a comment below. If I am not able to cover it on Wednesday, I'll follow up on the blog.

JavaOne 2006 BOF

May 13 2006, 07:30:56 PM PDT »Java Comments [2]
I co-host two BOFs at JavaOne this year and be available at the Ask The Experts pod:
Best Practices With Generics and Other Java™ Platform 5.0 Language Features BOF-0160 (Wed 8:30 - 9:20 pm)
This session is a follow-up to BOF-9225 of the 2005 JavaOne conference.

Generic types (generics) were added to the Java™ programming language in the JDK™ 5.0 release (code-named Tiger).

Generic types in the language were designed to allow an unsurpassed level of compatibility, but it is still possible to break compatibility. This presentation examines the underlying implementation technique (erasure) and discusses best practices and how to avoid common pitfalls when "generifying" existing code.
Java™ Programming Language and Compiler Issues for the Java Platform BOF-0159 (Wed 9:30 - 10:20 pm)
A recurring event at the JavaOne conference:

This session discusses Java™ programming language issues and issues involving javac, the compiler for the language.

Any language and compiler issue can be discussed:
  • Java Platform, Standard Edition (Java SE) 5 features
  • Old, well-known features
  • Proposed features for the upcoming Java platform releases (code-named Mustang and Dolphin)
  • Wouldn't it be nice...?
Ask The Experts: Java™ Compiler & Language at the Java SE Community booth (Thu 1 - 2 pm)
Come by if you have any questions on the compiler, the language, JSR 199, or JSR 269.

Joe has posted about JSR 269 events on his blog. I may join him at his booth slot on Tuesday.

Real Java Script

Apr 30 2006, 10:31:25 PM PDT »Java Comments [6]
A. Sundararajan have used JSR 199 to create a scripting engine for Java. It works by compiling the script (a Java program) in memory using the facilities of javax.tools. The scripting engine can be used in any Java program via JSR 223.

Imagine writing s StarOffice macro in Java :-)

Throwing unchecked exceptions

Mar 02 2006, 07:13:48 PM PST »Java Comments [11]
I'm doing spring cleaning of the compiler API (JSR 199, javax.tools). Yury suggested that while documenting which unchecked exceptions are thrown, perhaps they should not be listed in throws clauses.

So while this is good style:

/** * ... * @throws java.lang.RuntimeException if an unrecoverable error * occurred in a user supplied component. The cause will be the * error in user code. */

This is not:

void run() throws RuntimeException;

So how can I reliably find all unchecked exceptions? Very easily if I use an annotation processor. I used this annotation processor:

@SupportedAnnotationTypes("*") @SupportedOptions("summary") public class FindRuntimeException extends AbstractProcessor { // For summary of unchecked exceptions used in throws clauses final Set<TypeMirror> uncheckedExceptions = new LinkedHashSet<TypeMirror>(); public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) { // Get various utility classes final Elements elements = processingEnv.getElementUtils(); final Types types = processingEnv.getTypeUtils(); final Messager log = processingEnv.getMessager(); // Get the types (type mirrors) of RuntimeException and Error final TypeMirror runtimeException = elements.getTypeElement("java.lang.RuntimeException").asType(); final TypeMirror error = elements.getTypeElement("java.lang.Error").asType(); // Scanner for traversing the structure of a class, interface, etc. ElementScanner6<Void,Void> scan = new ElementScanner6<Void,Void>() { @Override public Void visitExecutable(ExecutableElement e, Void p) { // Executable means method or constructor, this method is called for // every method or constructor. So we just have to look at the throws // declaration... for (TypeMirror t :e.getThrownTypes()) // ...and see if any of the thrown types are subtypes of // RuntimeException or Error. if (types.isSubtype(t, runtimeException) || types.isSubtype(t, error)) { uncheckedExceptions.add(t); // print a warning so you can use your favorite editor to jump // to the problematic method log.printMessage(WARNING, "Throwing unchecked exception " + t, e); } return null; } }; // examine all the classes provided on the command line for (TypeElement e : roundEnvironment.getSpecifiedTypeElements()) scan.scan(e); // print a summary of found unchecked exceptions if (roundEnvironment.processingOver() && processingEnv.getOptions().containsKey("summary")) { System.out.println("Summary of unchecked exceptions found in throws clauses:"); for (TypeMirror t : uncheckedExceptions) System.out.println("\t" + t); } return true; } }

UPDATE: I have updated the code with comments and colors (using htmlize.el by Hrvoje Niksic)

Then I compiled the annotation processor:

javac -classpath /usr/java/jdk1.6.0/lib/tools.jar FindRuntimeException.java

You need a fairly recent Mustang snapshot to compile this. Also, from b74 (Thanks Kelly!), you won't have to put tools.jar on the classpath anymore. Then run the annotation processor:

javac -Asummary -proc:only -processor FindRuntimeException src/share/classes/javax/tools/*.java

Enjoy...

Mustang Beta

Feb 15 2006, 08:18:51 AM PST »Java Comments [3]
was just released. If you're not already hooked on the weekly snapshots, now would be a good time to try Mustang. It comes complete with the Tree API, JSR 269, and JSR 199. Javac now supports (JSR 269 style) annotation processing directly. This basically gives you the ability to write compiler plugins and use them as a standard part of your development processes. You already know how to use libraries (jar files) when compiling, now as annotation processing is a part of any Java compiler, libraries can use annotation processing and automatically generate boiler-plate code for you. A possible example of this is RFE 4820062. I plan to elaborate more on this in a future entry.

Strange filenames on Windows

Feb 02 2006, 09:45:46 PM PST »Java Comments [2]
Perhaps you're aware that files like CON, NUL, etc are reserved filenames on Windows. Sherman (Xueming) has just fixed java.io.File so that it should be possible to fix this old issue in the compiler: 4190157.

How to strip debug information

Jan 24 2006, 05:39:56 PM PST »Java
Earlier today a former colleague called and asked me how to strip debug information from classes compiled with javac's option -g. It turns out that it is very easy with Sun's JDK. In the same directory as the javac executable is another program, pack200. If you have a jar file with debugging information and want to remove, simply run pack200 like this:
pack200 -r -G debug.jar

This will strip all debug information from the jar file. Now this is a round-about way of doing it and probably not the fastest way. However, it works in a plain Sun JDK.

Getting from 1.4.2 to Mustang

Jan 20 2006, 04:29:35 AM PST »Java
I have been giving a talk in various places on how to take advantage of the new features in JDK 5.0 with a small cache example written for 1.4.2. As I have submitted this as a talk for JavaOne, I don't want to release the slides yet. However, here are the resources linked in the presentation:

Evolving the Language - updated

Jan 20 2006, 04:15:27 AM PST »Java
I promised to upload my slides from JavaOne in Tokyo. This is an updated version of evolving the Java™ programming language.

Live from Japan

Nov 06 2005, 05:25:17 AM PST »Java
This is my first and perhaps last post from Tokyo (I brought the wrong power connector). I'll be giving Graham, Mark, and Gilad's talk on evolving the Java™ programming language. The original talk was posted on SDN a week ago. However, progress have been made since June and I have updated the talk accordingly.

Tomorrow I'll try to sneak into the NetBeans day if I can find it.

Tree API updates in Mustang beta (b59)

Nov 03 2005, 12:32:15 PM PST »Java
I have updated the Tree API project with the interfaces and javadoc that will be part of Mustang beta (b59).

JSR 202 public review

Oct 28 2005, 02:49:21 PM PDT »Java
The new class file specification went public earlier today. This includes the new StackMapTable attribute which is required reading for all compiler engineers and anyone else who generates bytecodes (or modify them) ;-) I expect that Gilad will post something on this very soon.

Next Big Thing

Oct 21 2005, 08:23:26 PM PDT »Java Comments [3]
Joe just announced the next big thing in compilers. We have been working hard the last months to the get this ready. There's still a lot to be done, but you can actually run an annotation processor directly in javac. This is without hurting performance of plain old javac. In the next months we will work to integrate JSR 199, JSR 269, and the Tree API seamlessly.

Considered Harmful

Jul 27 2005, 10:44:51 PM PDT »Java Comments [16]
Dijkstra wrote a classical paper Go To Statement Considered Harmful. A paper that was considered classical in 1995 is truly a classic in our business ;-)

Dijkstra did understand what he was talking about.

Dijkstra was urging the community to move forward and work with higher abstractions.

Dijkstra points out that our intellectual powers are limited in a fashion that should make us strive to shorten the conceptual gap between the static program and the dynamic process.

A modern example of this conceptual gap is found when adding an element to a collection and extracting it in the Java™ Programming Language before the advent of generic types:

List strings = new ArrayList(); // list of strings
strings.add("Hello");
...
String s = (String)strings.get(0);

Notice how the programmer is required to bridge a conceptual gap by adding comments and casts that aren't enforced by the compiler as it is with generic types:

List<String> strings = new ArrayList<String>();
strings.add("Hello");
...
String s = strings.get(0);

Also notice that the compiler will notify the developer if he makes mistakes:

Integer i = (Integer)strings.get(0); // compile-time error
strings.add(1); // compile-time error

The benefits should be clear and I like to think if Dijkstra had been consulted on generics he would have said: "not having generic types should be considered harmful".

Ken Arnold doesn't understand generics and thus he suggest that generics should be considered harmful. Let's examine Ken's arguments:

My colleague told me something I misunderstood, but when I follow my wrong conclusions to the end, it doesn't make sense. Thus generics is considered harmful. And so Ken declares that you shouldn't do this:

    interface Holder<T> {
        T[] toArray();
    }

Allow me to correct your misunderstanding. You can't do this:

class Box<T> {
    T[] array = new T[10]; // compile-time error
}

You should not try to work around it:

class Box<T> {
    T[] array = (T[])new Object[10]; // BAD and causes a warning
}

This is an unfortunate side-effect of erasure, the VM doesn't know what T is at runtime, so the compiler has no way to generate code that can construct an array of the right type. In the VM, all arrays must record their element type. This is because arrays should be considered harmful:

public class Test {
    public static void main(String... args) {
        Object[] array = new String[10];
        array[0] = new Object(); // causes a run-time error, ArrayStoreException
    }
}
The "intuitively correct" type rule that allows the first assignment demonstrates that arrays aren't statically type safe. However, they are type safe: the VM eventually rejects the problematic assignment.

If you replace arrays with the generic ArrayList, you get this program:

import java.util.*;
public class Test {
    public static void main(String... args) {
        List<Object> array = new ArrayList<String>(10); // compile-time error
        array.add(0, new Object());
    }
}

So Ken and others should consider arrays harmful, not generics.

Ken also says that because he and David can't understand the declaration of Enum then it must be evil.

Let's look at Enum, it's not that complicated:

class Enum<E extends Enum<E>> { ... }

This is called an F-bound, I'm not exactly sure what the F means but think of it as function bounds. The bound on the type variable is a function (F) of the type variable. Yes, I know that probably didn't help your understanding. Hold on, here goes...

Since Enum is a special implementation class used for the new enum types, let's look at Comparable instead. The definition of Collections.sort looks like this:

public static <T extends Comparable<? super T>> void sort(List<T> list)

T should be a type which implements Comparable of a supertype of T. In other words: T must be comparable to it self (or a supertype of itself). This is a simple recursive definition.

So how do you validate that a given type meets the criteria (is within the bounds of T). It's simple: substitute T for the type and check if the statement is true. For example, Integer implements Comparable<Integer>, so the question is does

Integer extend Comparable<? super Integer>

The answer is yes. Integer implements Comparable<Integer> which is a subtype of Comparable<? super Integer>. Piece of cake.

JSR 199 Java™ Compiler API — Early Draft Release

Jul 13 2005, 01:31:35 PM PDT »Java
The EDR of JSR 199 is available for download.

There are still some minor issues that must be ironed out before it goes in to the weekly Mustang drops:

  • Locations are required to implement the Filer api of apt and son-of-apt JSR 269.
  • Compilation results. In order to provide better integration with IDEs, the result of javax.tools.JavaCompilerTool.run(java.io.Writer, javax.tools.JavaFileObject...) should be an object with more detail than a mere int.
  • While preparing my JavaOne demo, I noticed that it would be useful to provide some abstract implementations of JavaFileObject and JavaFileManager.

Any comments regarding these issues or other issues are welcome and should be addressed to the expert group.

Impressions from JavaOne

Jul 13 2005, 12:43:00 PM PDT »Java
JavaOne seemed to have more attendents than last year. I don't know how this measure up to the dot-com days. Last year was my first JavaOne so I was just pleased to see more developers.

I really enjoy meeting our customers at JavaOne and was pleased to see how popular apt has become. There was a lot of sessions on apt and a lot of interest in what we're doing in the tools area (javac and apt).

In the following days I'll try to post some more info on the new features we're planning in Mustang. In the meantime, here are the slides from Joe Darcy's and my BOF on "Generic Do's and Don'ts". Hopefully, we can elaborate more on these ideas in the future.

J2SE

Jun 27 2005, 12:00:00 PM PDT »Java
Earlier today, we announced the new naming for the Java™ platform. Mustang will be named Java™ Platform, Standard Edition 6 (shorthand: Java™ SE 6).

See also: Building and Strengthening the Java Brand

DIY Java

May 27 2005, 05:07:34 PM PDT »Java
Great news! Tiger update 3 is now available under JIUL. Read the FAQ and download Tiger.

Java Puzzlers - The Book

Feb 03 2005, 12:55:13 PM PST »Java Comments [1]
Neal and Josh are finally publishing a book based on their great JavaOne puzzlers talks. If you like the puzzlers, you're sure to like this book. If you don't like their talks, consider buying the book anyways as it is sure to raise your alertness to common (and uncommon) pitfalls. Pre-order it today from Amazon.
Java is a trademark of Sun Microsystems, Inc.
Copyright © 2006,2007 Peter von der Ahé