How to filter results of a virtual method?
Jim Laskey
jlaskey at me.com
Wed Jan 26 13:01:55 PST 2011
I think it's a simple issue. Drop should use 1 instead of 0.
public static
MethodHandle filterReturnValue(MethodHandle target, MethodHandle filter) {
MethodType targetType = target.type();
MethodType filterType = filter.type();
if (filterType.parameterCount() != 1
|| filterType.parameterType(0) != targetType.returnType())
System.err.println("target and filter types do not match");
// FIXME: Too many nodes here.
MethodHandle returner = dropArguments(filter, 0, targetType.parameterList()); <<< 0 should be 1
return foldArguments(returner, exactInvoker(target.type()).bindTo(target));
}
Cheers,
-- Jim
On 2011-01-26, at 1:17 PM, Jim Laskey wrote:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TestFilter.java
Type: application/octet-stream
Size: 1459 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20110126/a10cee4d/attachment.obj
-------------- next part --------------
>
>
> Is there a way to filter the result of a virtual method? Seems okay for statics, but receiver runs interference on virtual calls.
>
> Running the enclosed I get;
>
> 10.0
> java.lang.IllegalArgumentException: target and combiner types must match: (TestFilter,int)double != (TestFilter)int
> at sun.dyn.MemberName.newIllegalArgumentException(MemberName.java:513)
> at java.dyn.MethodHandles.misMatchedTypes(MethodHandles.java:1706)
> at java.dyn.MethodHandles.foldArguments(MethodHandles.java:1646)
> at java.dyn.MethodHandles.filterReturnValue(MethodHandles.java:1588)
> at TestFilter.main(TestFilter.java:29)
>
>
> Source:;
>
> import java.dyn.*;
>
> public class TestFilter {
>
> public static int a = 10;
> public int b = 10;
>
> public static double filter(int x) {
> return (double)x;
> }
>
> public static void main(String[] args) {
> try {
> MethodHandles.Lookup lookup = MethodHandles.lookup();
> MethodHandle getter = lookup.findStaticGetter(TestFilter.class, "a", int.class);
> MethodHandle filter = lookup.findStatic(TestFilter.class, "filter", MethodType.methodType(double.class, int.class));
> MethodHandle filteredGetter = MethodHandles.filterReturnValue(getter, filter);
>
> Object result = filteredGetter.invokeWithArguments();
> System.out.println(result);
> } catch (Throwable throwable) {
> throwable.printStackTrace();
> }
>
> try {
> MethodHandles.Lookup lookup = MethodHandles.lookup();
> MethodHandle getter = lookup.findGetter(TestFilter.class, "b", int.class);
> MethodHandle filter = lookup.findStatic(TestFilter.class, "filter", MethodType.methodType(double.class, int.class));
> MethodHandle filteredGetter = MethodHandles.filterReturnValue(getter, filter);
>
> TestFilter m = new TestFilter();
> Object result = filteredGetter.invokeWithArguments();
> System.out.println(result);
> } catch (Throwable throwable) {
> throwable.printStackTrace();
> }
> }
> }
>
More information about the mlvm-dev
mailing list