Xxx.class = a hack

September 30th, 2003 by Hen

I remember a friend back in the UK who is big into looking at class files complaining that .class files were a big hack. Now I understand why:

list.add( Object.class );

is also known as:

list.add(class$java$lang$Object == null
? class$java$lang$Object = class$(”java.lang.Object”)
: class$java$lang$Object);

plus a hidden method and variable:

/*synthetic*/ static Class class$java$lang$Object;

/*synthetic*/ static Class class$(String x0) {
Class var_class;
try {
var_class = Class.forName(x0);
} catch (ClassNotFoundException x1) {
throw new NoClassDefFoundError(x1.getMessage());
}
return var_class;
}

3 Responses to “Xxx.class = a hack”

  1. Kumar Mettu Says:

    I am not sure this is a hack considering “synthetic” usage has decent documentation in VM Spec.

  2. Danno Ferrin Says:

    There is a fine line between syntactic sugar and a hack. Xxx.class is syntactic sugar, along with all the new “language featuers” in 1.5. Under the covers it may look ugly but when it’s all sugared up who cares if you are eating flakes made of sub-standard corn not fit for the produce aisle of the super market?

    It’s not you are writing code using reflection to invoke a jdk 1.4 method so the class will compile in a 1.1 java compiler. Now _that’s_ a hack!

  3. freddo Says:

    class A {
    class B { }
    static void foo() {
    A _this = new A();
    B b = _this.new B();
    }
    }

    Now thats a hack