Bootstrap method question
Howard Lovatt
howard.lovatt at gmail.com
Fri Jun 18 23:40:40 PDT 2010
@Rémi,
Thanks for the advise about using a "slowPath" method to discover the
runtime types. I have coded up something along the lines you
suggested:
private static double slowPath( final CallSite callSite, final
CostCalculator cC, final Item i ) throws Throwable {
System.out.println("callSite = " + callSite + ", CostCalculator =
" + cC + ", i = " + i );
final MethodHandle nearest = Dispatchers.nearest( mhs.values(),
double.class, cC, i );
System.out.println( "nearest = " + Dispatchers.toString( nearest ) );
final MethodHandle converted = MethodHandles.collectArguments(
nearest, callSite.getTarget().type() );
System.out.println( "converted = " + Dispatchers.toString( converted ) );
callSite.setTarget( converted );
return converted.<double>invokeExact( cC, i );
}
When run I get the following:
callSite = CallSite#29596205[from invokedynamicmultipledispatch.Cost :
(invokedynamicmultipledispatch.CostCalculator,invokedynamicmultipledispatch.Item)double
=> slowPath], CostCalculator =
invokedynamicmultipledispatch.Price at bf2d5e, i =
[Ljava.lang.Object;@13bad12
Exception in thread "main"
invokedynamicmultipledispatch.MultipleDispatchException
at invokedynamicmultipledispatch.Cost.cost(Cost.java:62)
at invokedynamicmultipledispatch.Cost.main(Cost.java:51)
Caused by: invokedynamicmultipledispatch.MultipleDispatchException:
Cannot find type,
(invokedynamicmultipledispatch.Price,java.lang.Object[])double,
compatible method in,
[cost(invokedynamicmultipledispatch.Postage,invokedynamicmultipledispatch.CD)double,
cost(invokedynamicmultipledispatch.CostCalculator,invokedynamicmultipledispatch.Item)double,
cost(invokedynamicmultipledispatch.Price,invokedynamicmultipledispatch.Book)double,
cost(invokedynamicmultipledispatch.Price,invokedynamicmultipledispatch.CD)double,
cost(invokedynamicmultipledispatch.Postage,invokedynamicmultipledispatch.Book)double]
at invokedynamicmultipledispatch.Dispatchers.nearest(Dispatchers.java:182)
at invokedynamicmultipledispatch.Dispatchers.nearest(Dispatchers.java:145)
at invokedynamicmultipledispatch.CostCalculatorDispatcher.slowPath(CostCalculatorDispatcher.java:35)
at sun.dyn.FromGeneric$A2.invoke_D2(FromGeneric.java:534)
at sun.dyn.FilterGeneric$F2.invoke_C1(FilterGeneric.java:550)
at sun.dyn.ToGeneric$A2.target(ToGeneric.java:661)
at sun.dyn.ToGeneric$A2.targetA2(ToGeneric.java:662)
at sun.dyn.ToGeneric$A2.invoke_D(ToGeneric.java:671)
at invokedynamicmultipledispatch.Cost.cost(Cost.java:60)
... 1 more
The interesting bit is "i = [Ljava.lang.Object;@13bad12" on the first
line of the printout (ignoring wrapping). This Object[] contains 1
element, the expected Item. What is not expected is wrapping this in
an array. Also this is a type unsafe at runtime since an array is
passed into a method in a position where an Item is coded.
Any idea what is going on?
-- Howard.
Rémi Forax forax at univ-mlv.fr Sat Jun 12 05:09:48 PDT 2010 wrote:
[snip]
> public class RT {
> public static CallSite bootstrap(Class<?> declaringClass, String name, MethodType methodType) {
> CallSite callSite = new CallSite();
> MethodHandle target = MethodHandles.insertArguments(slowPath, 0, callSite);
> target = MethodHandles.collectArguments(target, methodType);
> callSite.setTarget(target);
> return callSite;
> }
>
> private static final MethodHandle slowPath = RT#slowPath(ClassSite,Object[]);
>
> public Object slowPath(CallSite callsite, Object[] args) {
> // find the target method handle depending of the classes of arguments
> MethodHandle specific = ...
> // create a test for those classes
> MethodHandle test = ...
>
> MethodHandle guard = MethodHandles.guardWithTest(test, specific, callSite.getTarget());
> callSite.setTarget(guard);
>
> return specific.invokeVarargs(args);
> }
> }
[snip]
More information about the mlvm-dev
mailing list