11 December 2008

New in Java 6

Java 6 New Features coverIt has been two years since Java 6 was released, so it's really high time to get to know it better.

Book about new features
Earlier this year I read Java 6 New Features by Budi Kurniawan to get an idea of the new features. I liked the book a lot because it is really unique. All the other books you find for Java 6 are general Java books that more or less cover the new features of Mustang. I do not want to read about the methods of java.lang.Object or the Collections Framework again. Budi Kurniawan does an excellent job focusing only on the new stuff. If you want to know what is new in release 6, go for this book!

List of all new classes in Mustang
Then I updated my list of all classes included in the runtime of Java, similar to what I did two years ago. I used Sun's Java 1.6.0_11 and created the updated list of all runtime environment classes in Java 1.6. Let's see what I found: Java 6 added only 74 new types in the java. name-space,
java.awt.Desktop
java.awt.font.LayoutPath
java.awt.geom.Path2D
java.awt.GridBagLayoutInfo
java.awt.LinearGradientPaint
java.awt.MultipleGradientPaint
java.awt.RadialGradientPaint
java.awt.SplashScreen
java.awt.SystemTray
java.awt.TrayIcon
java.beans.ConstructorProperties
java.io.Console
java.io.IOError
java.lang.management.LockInfo
java.lang.management.MonitorInfo
java.net.CookieManager
java.net.CookiePolicy
java.net.CookieStore
java.net.HttpCookie
java.net.IDN
java.net.InterfaceAddress
java.security.PolicySpi
java.security.URIParameter
java.sql.ClientInfoStatus
java.sql.NClob
java.sql.RowId
java.sql.RowIdLifetime
java.sql.SQLClientInfoException
java.sql.SQLDataException
java.sql.SQLFeatureNotSupportedException
java.sql.SQLIntegrityConstraintViolationException
java.sql.SQLInvalidAuthorizationSpecException
java.sql.SQLNonTransientConnectionException
java.sql.SQLNonTransientException
java.sql.SQLRecoverableException
java.sql.SQLSyntaxErrorException
java.sql.SQLTimeoutException
java.sql.SQLTransactionRollbackException
java.sql.SQLTransientConnectionException
java.sql.SQLTransientException
java.sql.SQLXML
java.sql.Wrapper
java.text.Normalizer
java.text.spi.BreakIteratorProvider
java.text.spi.CollatorProvider
java.text.spi.DateFormatProvider
java.text.spi.DateFormatSymbolsProvider
java.text.spi.DecimalFormatSymbolsProvider
java.text.spi.NumberFormatProvider
java.util.ArrayDeque
java.util.concurrent.BlockingDeque
java.util.concurrent.ConcurrentNavigableMap
java.util.concurrent.ConcurrentSkipListMap
java.util.concurrent.ConcurrentSkipListSet
java.util.concurrent.LinkedBlockingDeque
java.util.concurrent.locks.AbstractOwnableSynchronizer
java.util.concurrent.locks.AbstractQueuedLongSynchronizer
java.util.concurrent.RunnableFuture
java.util.concurrent.RunnableScheduledFuture
java.util.Deque
java.util.NavigableMap
java.util.NavigableSet
java.util.ServiceConfigurationError
java.util.ServiceLoader
java.util.spi.CurrencyNameProvider
java.util.spi.LocaleNameProvider
java.util.spi.LocaleServiceProvider
java.util.spi.TimeZoneNameProvider
java.util.zip.DeflaterInputStream
java.util.zip.InflaterOutputStream
java.util.zip.ZipError
and around 400 in javax. packages,
javax.activation
javax.annotation
javax.jws
javax.lang.model
javax.script
javax.smartcardio
javax.tools
javax.xml.bind
javax.xml.crypto
javax.xml.soap
javax.xml.stream
javax.xml.transform.stax
javax.xml.ws
mainly the Java Activation Framework, some extended annotations and the model of the Java language. Further there are the new Scripting (JSR 223) and Compiler (JSR 199) APIs. More than half of the new classes are XML related, including XML Binding/JAXB 2.0 (JSR 222), XML signatures (JSR 105), Streaming API 4 XML/StaX (JSR 175) and XML based Web Services/JAX-WS 2.0 (JSR 224).

The need for a better tool
Till now I used some ugly shell scripts to extract the needed information about the classes in rt.jar. With the growth of the JRE the repeated call of javap -public %class% for each class took more and more time. I needed something else to determine the accessibility of each class. I remembered that there was a field that determined various attributes of a class. I wanted to load the class file's bytes, jump to the offset of this field and read the package/public flag. The Java class file format is simple, but unfortunately not that simple. In order to read the access_flags, one needs to understand the whole constant pool because the access flag is stored afterwards and the constant pool is of variable length. So I started disassembling the class files. I do not know why, but I happened to write the new script in Ruby:
# Unpack HI-LO byte orders contained in the String.
class String
  # Return the _index_ 'th and the next element as unsigned word.
  def u2(index)
    self[index..index+1].unpack('n')[0]
  end
end

class JavaClassHeader

  # Parse the binary _data_ from the class file.
  def initialize(data)

    # u4 magic;
    # u2 minor_version;
    # u2 major_version;

    # u2 constant_pool_count;
    # cp_info constant_pool[constant_pool_count-1];

    constant_pool_count = data.u2(8)
    pos = 10
    cnt = 1
    while cnt <= constant_pool_count - 1
      case data.u1(pos) # cp_info_tag
        when 7
          pos += 3
        when 9
          pos += 5
        when 10
          pos += 5
        when 11
          pos += 5
        when 8
          pos += 3
        when 3
          pos += 5
        when 4
          pos += 5
        when 5
          pos += 9
          cnt += 1
        when 6
          pos += 9
          cnt += 1
        when 12
          pos += 5
        when 1
          length = data.u2(pos+1)
          pos += 3 + length
        else
          raise "const ##{cnt} unknown tag #{data[pos]}"
      end
      cnt += 1
    end

    # u2 access_flags;
    @access_flags = data.u2(pos)
  end
After (sort of) parsing the class file, the check if a class is public or only package accessible was straight forward to implement.
  ACC_PUBLIC = 0x0001

  # Return +true+ if the class is public.
  def accessible?
   (@access_flags & ACC_PUBLIC) != 0
  end
This improved performance a lot and I am looking forward to the release of Java 7 to use my new ClassList script again.

(Download the list of all classes in Mustang.)

Update 14 Mar 2009

javaclass-rb Project Initiated

The ClassList script was fun and made me want to play more with class files. Based on the source code above, I created javaclass-rb, a parser and disassembler for Java class files, similar to the javap command. The initial version reads the class file version and the package/public flag of a class as well as the constant pool. Expect updates soon!