cute demo uses import-static

Rémi Forax forax at univ-mlv.fr
Tue May 5 06:23:06 PDT 2009


John Rose a écrit :
> I have been updating the InvokeDynamicDemo toy project from my blog 
> (and the meth/indy software in general).
>
> Here's the driver routine for that demo:
>
> import java.dyn.*;
> import static java.dyn.MethodHandles.*;
> import static java.dyn.MethodType.*;
>
> public class Main {
>     static final Class[] CLASSES = { Hello.class, GetNameDemo.class, 
> FidgetDemo.class };
>     public static void main(String[] args) {
>         for (Class c : CLASSES) {
>             MethodHandle main = lookup().findStatic(c, "main",
>                     make(void.class, String[].class));
>             System.out.println("******** "+c.getName()+"."+main);
>             main.<void>invoke(args);
>         }
>     }
> }
>
> The import-statics reduce the verbosity, but (to me at least) it's 
> still readable.

I'm not sure it will be readable for rookies.
Two difficulties is always one too many.
(rough translation of a favorite sentence of one of my teachers)

>  I'm thinking maybe we should rename "make" to "type", as in 
> MethodType.type(...) or for short just type(...).

The other way is to add overloaded methods for findStatic, findVirtual 
and findSpecial.

public static MethodHandle findStatic(Class<?> defc, String name, 
Class<?>... parameterTypes) throws NoAccessException {
  return findStatic(defc, name, MethodType.make(parameterTypes)));
}
and add a method make(Class<?>[]) in MethodType.

>
> I will be pushing the code that actually runs this stuff in a day or 
> two.  (I know folks have been waiting for it.)
>
> Here's a bit of good news:  I just pushed the experimental javac 
> changes into the TL/langtools integration area, so that we can 
> actually write the code above.
>
> (N.B. The javac support is subject to change, if we decide there's a 
> better design for the language support.  Anybody who followed Neal's 
> closures design, or the earlier debates about internal vs. external 
> iterator APIs, knows that delicate problems arise when checked 
> exceptions mix with function pointers.)
>
> Best wishes,
> -- John

cheers,
Rémi



More information about the mlvm-dev mailing list