Skip to end of metadata
Go to start of metadata

Here are some givens:

  • Java static method references (like String.format or Math.sin) work in Groovy.
  • A class name is not a special kind of name (see classes are always objects).
  • The syntax x.y() is always a method call.

From this we are required to give an account of Math.sin as a method named 'sin', and called on an object 'Math.class', which is the Class reflective object for the type Math.

This is complicated by the fact that, in Java, class Class has a fixed set of methods (such as 'newInstance') which does not include names like 'sin' or 'format'. Thus, the following code needs to perform a special kind of method dispatch, which occurs only for the unique class 'java/lang/Class':

This clearly requires some runtime work, although in the common case the compiler can constant-fold the class, look up the method, and emit an 'invokestatic' bytecode.

Advantages:

  • Consistency, lack of special pleading for static references.
  • Removes a syntax case from the compiler (changes it to an optimization).
  • Factors the language specification; makes static method refs. into a negotiable method on Class.
  • (Add more here.)

Disadvantages:

  • Need a runtime lookup to parallel the compile-time looukp.
  • Non-intuitive in some cases? (Only with mutable Class variables.)
  • (Add more here.)
  • No labels