compiler accepts incompatible method reference for Sam conversion
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Wed Jul 13 02:36:06 PDT 2011
Hi Remi,
On 7/13/11, Rémi Forax <forax at univ-mlv.fr> wrote:
> On 07/13/2011 06:01 AM, Ali Ebrahimi wrote:
>
> Hi Ali,
>
> ...
>> MethodHandle mh =
>> MethodHandles.lookup().findStatic(MethodHandleTest.class,
>> "sTest", MethodType.methodType(void.class,int.class,int.class));
>> MethodHandle smh = mh.asSpreader(Object[].class,
>> mh.type().parameterCount());
>>
>> smh.invoke(new Object[]{i,i});<===============crash
>>
>> main cause: mh.asSpreader
>>
>> I don't known this crash appears in jdkb147.
>
> What you want here is to use asCollector, not asSpreader.
> asSpreader requires that the method as already an object array
> and extract the value from it.
May be i'm wrong. but this code picked from MethodHandleProxies class
public static
<T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) {
final Method sm = getSingleMethod(intfc);
if (sm == null)
throw new IllegalArgumentException("not a single-method
interface: "+intfc.getName());
MethodType smMT = MethodType.methodType(sm.getReturnType(),
sm.getParameterTypes());
MethodHandle checkTarget = target.asType(smMT); // make throw WMT
checkTarget =
checkTarget.asType(checkTarget.type().changeReturnType(Object.class));
final MethodHandle vaTarget =
checkTarget.asSpreader(Object[].class, smMT.parameterCount());
return intfc.cast(Proxy.newProxyInstance(
intfc.getClassLoader(),
new Class[]{ intfc, WrapperInstance.class },
new InvocationHandler() {
private Object getArg(String name) {
if ((Object)name ==
"getWrapperInstanceTarget") return target;
if ((Object)name == "getWrapperInstanceType")
return intfc;
throw new AssertionError();
}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if (method.getDeclaringClass() == WrapperInstance.class)
return getArg(method.getName());
if (method.equals(sm))
return vaTarget.invokeExact(args);
if (isObjectMethod(method))
return callObjectMethod(this, method, args);
throw new InternalError();
}
}));
}
and this results in this crash:
class MethodHandleTest{
...
public static void sTest(int x,int y){
// System.out.println("stest");
}
static interface Sam{
void m(int x,int y);
}
..
Sam sam = MethodHandleTest#sTest;
sam.m(1,1);<==========
>
> In your example, the call to mh.asSpreader should return a runtime
> exception,
> I will investigate why it's not the case when I will be at my office :)
>
>> Best Regards,
>> Ali Ebrahimi
>
> regards,
> Rémi
>
>
> _______________________________________________
> mlvm-dev mailing list
> mlvm-dev at openjdk.java.net
> http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
>
Best Regards,
Ali Ebrahimi
More information about the lambda-dev
mailing list