Java Reference
In-Depth Information
Object
rcvr
=
"a"
;
try
{
MethodType
mt
=
MethodType
.
methodType
(
int
.
class
);
MethodHandles
.
Lookup
l
=
MethodHandles
.
lookup
();
MethodHandle
mh
=
l
.
findVirtual
(
rcvr
.
getClass
(),
"hashCode"
,
mt
);
int
ret
;
try
{
ret
=
(
int
)
mh
.
invoke
(
rcvr
);
System
.
out
.
println
(
ret
);
}
catch
(
Throwable
t
)
{
t
.
printStackTrace
();
}
}
catch
(
IllegalArgumentException
|
NoSuchMethodException
|
SecurityException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
x
)
{
x
.
printStackTrace
();
}
Method handles provide a clearer and more coherent way to access the same
dynamic programming capabilities as Reflection. In addition, they are designed to
work well with the low-level execution model of the JVM and thus hold out the
promise of much better performance than Reflection can provide.
g
R
e
l
e
c
t
i
o
n
d