Showing posts with label JavaClass. Show all posts
Showing posts with label JavaClass. Show all posts

19 December 2011

JavaClass 0.4 released

Much has happened since I created JavaClass almost three years ago. I started with version 0.0.1 because much was missing in there. Since then I managed a small release once a year. Version two added class names and references. Last year I added classpath abstractions and moved the project from Rubyforge to Google Code (and then in the future to Bitbucket and eventually to GitHub). This year I finished version 0.0.4. Now the Java class file parser supports everything except fields and methods. Adding them would be no big deal, I just had no need to do it till now. This fourth release feels more complete and should rather be named version 0.4 than 0.0.4. So the next version will be 0.5.

Coffee BeansNew in JavaClass 0.4
I added support for Java 5 Enums and Annotations to JavaClass::ClassFile::JavaClassHeader and write code to retrieve all interfaces implemented by a class. Further I created Maven- and Eclipse-aware classpath abstractions. If you point JavaClass to the folder of a whole Eclipse workspace, you get a JavaClass::Classpath::CompositeClasspath containing all projects found in that workspace. Finally I fixed some defects, e.g. a character to byte conversion problem found in Ruby 1.9 and a printf inconsistency between Windows and Linux platforms. See the history of JavaClass 0.4 for the complete list of changes.

RDOC documentation was improved and several examples have been added. In fact these examples are real world scripts that I use to analyse code bases. They are just formatted nicely and commented in very detail.

Find Unused Classes
For example let us look for unused classes. By "unused" I mean classes that have no incoming reference inside a code base. Such classes might still be referenced by Java Reflection, but more likely are just dead code. I have seen legacy code bases where more than 10% of all classes were unused and their deletion was a welcomed reduction of the maintenance burden. A list of classes that can (potentially) be deleted is a valuable asset for the next clean-up cycle. Still, each class on the list needs to be double checked because classes with main methods, Servlets and other framework classes might show up on the list but still be used.
require 'javaclass/dsl/mixin'
require 'javaclass/classpath/tracking_classpath'

location = 'C:\Eclipse\workspace'
package = 'com.biz.app'

# 1) create the (tracking) composite classpath of the given workspace.
cp = workspace(location)

# Create a filter to limit all operations to the classes of our application.
filter = Proc.new { |clazz| clazz.same_or_subpackage_of?(package) }

# 2) load all classes of the application, this can take several minutes.
classes = cp.values(&filter)

# 3) for all classes, mark all referenced types as accessed.
cp.reset_access
classes.map { |clazz| clazz.imported_types }.flatten.
  find_all(&filter).
  each { |classname| cp.mark_accessed(classname) }

# 4) also mark classes referenced from config files.
scan_config_for_3rd_party_class_names(location).
  find_all(&filter).
  each { |classname| cp.mark_accessed(classname) }

# 5) find non accessed classes.
unused_classes = classes.find_all { |clazz| cp.accessed(clazz) == 0 }
The above example uses EclipseClasspath and TrackingClasspath under the hood. TrackingClasspath adds functionality to record the number of usages of each class. The classes that are not used by any imports (step 3) or any hard coded configurations (step 4) are potentially unused.

Java Class Literals in Ruby
Inspired by date literals in Ruby I thought about Java class name literals in Ruby. This feels a bit crazy so it is not enabled by default. After including JavaClass::Dsl::JavaNameFactory you can write
clazz = java.lang.String    # => "java.lang.String"
Note the absence of any quotes around the right hand side of the above assignment. The returned class name is not just a plain String, but also a JavaQualifiedName with some extra methods to work on Java names, like
clazz.to_java_file          # => "java/lang/String.java"
clazz.full_name             # => java.lang.String"
clazz.package               # => "java.lang"
clazz.simple_name           # => "String"
Adding these rich strings caused me a lot of trouble, but that is another story.

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!