Saturday, August 13, 2011

The MethodHandle with Java 7

Suppose we wanted to invoke a non-public methods of a class, prior to Java 7 the only way to do this is to use the Reflection API. Now Java 7 introduces new API for dynamic run-time inspection of a Java types, the method handle.
A method handle is a typed, directly executable reference to an underlying method, constructor, field, or similar low-level operation, with optional transformations of arguments or return values. These transformations are quite general, and include such patterns as conversion, insertion, deletion, and substitution ...
[See java.lang.invoke.MethodHandle]

Note: Package java.dyn was renamed to java.lang.invoke package.
Let's have a quick short program hello-world to use this method handle. The ff. code also uses the new Java 7's multi-catch statement:

MethodAccess.java

Coin.java

I use NetBeans IDE 7.0.1 - Output:

See also:
Underscores in Binary Literals with Java 7
Read File Into String with Java 7

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.