当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandle.invokeExact方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandle.invokeExact方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandle.invokeExact方法的具体用法?Java MethodHandle.invokeExact怎么用?Java MethodHandle.invokeExact使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandle的用法示例。


在下文中一共展示了MethodHandle.invokeExact方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCast

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public void testCast() throws Throwable {
    Class<?>[] types = { Object.class, Serializable.class, String.class, Number.class, Integer.class };
    Object[] objects = { new Object(), Boolean.FALSE,      "hello",      (Long)12L,    (Integer)6    };
    for (Class<?> dst : types) {
        MethodHandle caster = ValueConversions.cast().bindTo(dst);
        assertEquals(caster.type(), MethodHandles.identity(Object.class).type());
        for (Object obj : objects) {
            Class<?> src = obj.getClass();
            boolean canCast = dst.isAssignableFrom(src);
            try {
                Object result = caster.invokeExact(obj);
                if (canCast)
                    assertEquals(obj, result);
                else
                    assertEquals("cast should not have succeeded", dst, obj);
            } catch (ClassCastException ex) {
                if (canCast)
                    throw ex;
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:ValueConversionsTest.java

示例2: knownFunctionPropertyGuardProto

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private static boolean knownFunctionPropertyGuardProto(final Object self, final PropertyMap map, final MethodHandle getter, final int depth, final ScriptFunction func) {
    if (self instanceof ScriptObject && ((ScriptObject)self).getMap() == map) {
        final ScriptObject proto = getProto((ScriptObject)self, depth);
        if (proto == null) {
            return false;
        }
        try {
            return getter.invokeExact((Object)proto) == func;
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    return false;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:ScriptObject.java

示例3: test

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
void test(MethodHandle mh_SiO,
          MethodHandle mh_vS,
          MethodHandle mh_vi,
          MethodHandle mh_vv) throws Throwable {
    Object o; String s; int i;  // for return type testing

    // next five must have sig = (String,int)Object
    mh_SiO.invokeExact("world", 123);
    mh_SiO.invokeExact("mundus", 456);
    Object k = "kosmos";
    mh_SiO.invokeExact((String)k, 789);
    o = mh_SiO.invokeExact((String)null, 000);
    o = (Object) mh_SiO.invokeExact("arda", -123);

    // sig = ()String
    s = (String) mh_vS.invokeExact();

    // sig = ()int
    i = (int) mh_vi.invokeExact();
    o = (int) mh_vi.invokeExact();

    // sig = ()void
    mh_vv.invokeExact();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:InvokeMH.java

示例4: forEach

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * ECMA6 23.2.3.6 Set.prototype.forEach ( callbackfn [ , thisArg ] )
 *
 * @param self the self reference
 * @param callbackFn the callback function
 * @param thisArg optional this object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static void forEach(final Object self, final Object callbackFn, final Object thisArg) {
    final NativeSet set = getNativeSet(self);
    if (!Bootstrap.isCallable(callbackFn)) {
        throw typeError("not.a.function", ScriptRuntime.safeToString(callbackFn));
    }
    final MethodHandle invoker = Global.instance().getDynamicInvoker(FOREACH_INVOKER_KEY,
            () -> Bootstrap.createDynamicCallInvoker(Object.class, Object.class, Object.class, Object.class, Object.class, Object.class));

    final LinkedMap.LinkedMapIterator iterator = set.getJavaMap().getIterator();
    for (;;) {
        final LinkedMap.Node node = iterator.next();
        if (node == null) {
            break;
        }

        try {
            final Object result = invoker.invokeExact(callbackFn, thisArg, node.getKey(), node.getKey(), self);
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:NativeSet.java

示例5: testBox

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public void testBox() throws Throwable {
    for (Wrapper w : Wrapper.values()) {
        if (w == Wrapper.VOID)    continue;  // skip this; no unboxed form
        if (w == Wrapper.OBJECT)  continue;  // skip this; already unboxed
        for (int n = -5; n < 10; n++) {
            Object box = w.wrap(n);
            MethodHandle boxer = ValueConversions.boxExact(w);
            Object expResult = box;
            Object result = null;
            switch (w) {
                case INT:     result = (Integer) boxer.invokeExact(/*int*/n); break;
                case LONG:    result = (Long)    boxer.invokeExact((long)n); break;
                case FLOAT:   result = (Float)   boxer.invokeExact((float)n); break;
                case DOUBLE:  result = (Double)  boxer.invokeExact((double)n); break;
                case CHAR:    result = (Character) boxer.invokeExact((char)n); break;
                case BYTE:    result = (Byte)    boxer.invokeExact((byte)n); break;
                case SHORT:   result = (Short)   boxer.invokeExact((short)n); break;
                case BOOLEAN: result = (Boolean) boxer.invokeExact((n & 1) != 0); break;
            }
            assertEquals("(dst,src,n,box)="+Arrays.asList(w,w,n,box),
                         expResult, result);
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:26,代码来源:ValueConversionsTest.java

示例6: invokeLongGetter

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private static long invokeLongGetter(final Accessors gs, final MethodHandle invoker, final int programPoint, final Object self) throws Throwable {
    final Object func = gs.getter;
    if (func instanceof ScriptFunction) {
        return (long) invoker.invokeExact(func, self);
    }

    throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:UserAccessorProperty.java

示例7: filterVarArgs

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private static Object[] filterVarArgs(final MethodHandle parameterFilter, final Object[] args) throws Throwable {
    Object[] newArgs = null;
    for(int i = 0; i < args.length; ++i) {
        final Object arg = args[i];
        final Object newArg = parameterFilter.invokeExact(arg);
        if (arg != newArg) {
            if (newArgs == null) {
                newArgs = args.clone();
            }
            newArgs[i] = newArg;
        }
    }
    return newArgs == null ? args : newArgs;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:DefaultInternalObjectFilter.java

示例8: invokeNumberGetter

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@SuppressWarnings("unused")
private static double invokeNumberGetter(final Accessors gs, final MethodHandle invoker, final int programPoint, final Object self) throws Throwable {
    final Object func = gs.getter;
    if (func instanceof ScriptFunction) {
        return (double) invoker.invokeExact(func, self);
    }

    throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:UserAccessorProperty.java

示例9: testStaticSetter

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public static synchronized void testStaticSetter() throws Throwable {		
	staticSettableInt = 0;
	MethodHandle mh = MethodHandles.lookup().findStaticSetter(BasicStaticTest.class, "staticSettableInt", int.class);
	mh.invoke(1);
	check(1, staticSettableInt);
	mh.invokeExact(2);
	check(2, staticSettableInt);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:9,代码来源:BasicStaticTest.java

示例10: main

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
  /*
  MethodHandle iterator, hasNext, next, test, apply, accept;
  Lookup lookup = MethodHandles.publicLookup();
  try {
    iterator = lookup.findVirtual(Collection.class, "iterator", methodType(Iterator.class));
    hasNext = lookup.findVirtual(Iterator.class, "hasNext", methodType(boolean.class));
    next = lookup.findVirtual(Iterator.class, "next", methodType(Object.class));
    test = lookup.findVirtual(Predicate.class, "test", methodType(boolean.class, Object.class));
    apply = lookup.findVirtual(Function.class, "apply", methodType(Object.class, Object.class));
    accept = lookup.findVirtual(Consumer.class, "accept", methodType(void.class, Object.class));
  } catch (NoSuchMethodException | IllegalAccessException e) {
    throw new AssertionError();
  }
  
  MethodHandle[] clause0 = new MethodHandle[] { iterator, null, hasNext };
  MethodHandle[] clause1 = new MethodHandle[] { null, next };
  MethodHandle[] clause2 = new MethodHandle[] { null, MethodHandles.dropArguments(
      apply.bindTo((Function<String, String>)x -> x + " !"),
      0, Iterator.class)};
  MethodHandle[] clause3 = new MethodHandle[] { null, null, MethodHandles.dropArguments(
      test.bindTo(((Predicate<String>)x -> true)),
      0, Iterator.class, Object.class),
      MethodHandles.zero(void.class) };
  MethodHandle[] clause4 = new MethodHandle[] { null, MethodHandles.dropArguments(
      accept.bindTo((Consumer<Object>)System.out::println),
      0, Iterator.class, Object.class)};
  
  MethodHandle loop = MethodHandles.loop(clause0, clause1, clause2, clause3, clause4);
  loop.invokeExact((Collection<?>)List.of("a", "b"));
  */
  
  LoopBuilder builder = LoopBuilder.create();
  builder.map(x -> x +  " !");
  builder.filter(x -> true);
  MethodHandle loop = builder.forEach(System.out::println);
  loop.invokeExact((Collection<?>)List.of("a", "b"));
}
 
开发者ID:forax,项目名称:mjolnir,代码行数:39,代码来源:LoopBuilder.java

示例11: deconstructor

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Deconstruct({ int.class, int.class })
public Object deconstructor(MethodHandle carrier) throws Throwable {
  if (x == y) {
    return carrier.invokeExact(false, 0, 0);  // reject
  }
  return carrier.invokeExact(true, x, y);
}
 
开发者ID:forax,项目名称:mjolnir,代码行数:8,代码来源:PatternMatchingTests.java

示例12: getIterator

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
/**
 * ES6 7.4.1 GetIterator abstract operation
 *
 * @param iterable an object
 * @param global the global object
 * @return the iterator
 */
public static Object getIterator(final Object iterable, final Global global) {
    final Object object = Global.toObject(iterable);

    if (object instanceof ScriptObject) {
        // TODO we need to implement fast property access for Symbol keys in order to use InvokeByName here.
        final Object getter = ((ScriptObject) object).get(NativeSymbol.iterator);

        if (Bootstrap.isCallable(getter)) {
            try {
                final MethodHandle invoker = getIteratorInvoker(global);

                final Object value = invoker.invokeExact(getter, iterable);
                if (JSType.isPrimitive(value)) {
                    throw typeError("not.an.object", ScriptRuntime.safeToString(value));
                }
                return value;

            } catch (final Throwable t) {
                throw new RuntimeException(t);
            }
        }
        throw typeError("not.a.function", ScriptRuntime.safeToString(getter));
    }

    throw typeError("cannot.get.iterator", ScriptRuntime.safeToString(iterable));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:AbstractIterator.java

示例13: main

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    Test8015436 testObj = new Test8015436();
    testObj.someMethod();
    testObj.defaultMethod(DEFAULT_MTD_INVOKED_DIRECTLY);

    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType   mt = MethodType.methodType(void.class, String.class);
    MethodHandle mh = lookup.findVirtual(Test8015436.class, "defaultMethod", mt);
    mh.invokeExact(testObj, DEFAULT_MTD_INVOKED_MH);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:Test8015436.java

示例14: testShortZeroConversion

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
@Test
public void testShortZeroConversion() throws Throwable {
    MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
    MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
    MethodHandle h3 = h2.asType(MethodType.methodType(short.class));  // add 0
    MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box

    Object x = h4.invokeExact();
    assertEquals(x, (short)0);
    assertTrue(x == Short.valueOf((short)0));
    assertTrue(x == Wrapper.SHORT.zero());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:WrapperTest.java

示例15: testAllAccessCallerSameModule

import java.lang.invoke.MethodHandle; //导入方法依赖的package包/类
public void testAllAccessCallerSameModule() throws Throwable {
    Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, MethodHandles.lookup());
    assertTrue(lookup.lookupClass() == nonPublicType);
    assertTrue(lookup.hasPrivateAccess());

    // get obj field
    MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
    Object obj = mh.invokeExact();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:PrivateLookupInTests.java


注:本文中的java.lang.invoke.MethodHandle.invokeExact方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。