Example usage for java.lang.invoke MethodHandle invoke

List of usage examples for java.lang.invoke MethodHandle invoke

Introduction

In this page you can find the example usage for java.lang.invoke MethodHandle invoke.

Prototype

@HotSpotIntrinsicCandidate
public final native @PolymorphicSignature Object invoke(Object... args) throws Throwable;

Source Link

Document

Invokes the method handle, allowing any caller type descriptor, and optionally performing conversions on arguments and return values.

Usage

From source file:hm.binkley.util.Converter.java

@SuppressWarnings("unchecked")
private static <T, E extends Exception> Conversion<T, E> thunk(final MethodHandle handle) {
    return value -> {
        try {//w w w  .  java  2 s. c o  m
            return (T) handle.invoke(value);
        } catch (final Error | RuntimeException e) {
            throw e;
        } catch (final Throwable t) {
            throw (E) t;
        }
    };
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static <T> T propertyValue(Object bean, String propertyName) {

    try {//w  w w.j ava  2 s  .  c om
        ConcurrentMap<String, MethodHandle> methodHandles = CACHE.get(bean.getClass(),
                new Callable<ConcurrentMap<String, MethodHandle>>() {

                    @Override
                    public ConcurrentMap<String, MethodHandle> call() throws Exception {

                        return new MapMaker().concurrencyLevel(2).makeMap();
                    }
                });
        /*
         * We may assume this map only grows and never shrinks. It does not matter if the same
         * getter is added twice by concurrent invocations of this method.
         */
        MethodHandle getter = methodHandles.get(propertyName);
        if (getter == null) {
            getter = getterMethod(bean.getClass(), propertyName);
            methodHandles.put(propertyName, getter);
        }
        assert getter != null;
        return (T) getter.invoke(bean);
    } catch (Throwable t) {
        throw Throwables.propagate(t);
    }
}

From source file:de.ks.flatadocdb.metamodel.EntityDescriptor.java

@SuppressWarnings("unchecked")
private <T> T invokeGetter(MethodHandle handle, Object instance) {
    try {// w w w. j a  v a2 s  .  c  o  m
        return (T) handle.invoke(instance);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:net.orzo.data.Web.java

/**
 *
 *//*ww  w  .  j a v  a 2 s .com*/
public List<Element> queryPage(Element root, String select, ScriptFunction fn) {
    MethodHandle mh;
    Element curr;
    List<Element> ans = null; // returns null in case fn is null

    try {
        if (fn != null) {
            for (Iterator<Element> iter = root.select(select).iterator(); iter.hasNext();) {
                curr = iter.next();
                mh = fn.getBoundInvokeHandle(curr);
                mh.invoke(curr);
            }

        } else {
            ans = new ArrayList<>();
            Iterators.addAll(ans, root.select(select).iterator());
        }
        return ans;

    } catch (Throwable ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.ks.flatadocdb.session.Session.java

private Object load(IndexElement indexElement) {
    SessionEntry sessionEntry = loadSessionEntry(indexElement);
    EntityDescriptor descriptor = sessionEntry.getEntityDescriptor();
    Object object = sessionEntry.getObject();
    dirtyChecker.trackLoad(sessionEntry);

    Set<MethodHandle> lifeCycleMethods = descriptor.getLifeCycleMethods(LifeCycle.POST_LOAD);
    for (MethodHandle handle : lifeCycleMethods) {
        try {/*from w ww. jav  a  2 s.c  om*/
            handle.invoke(object);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
    return object;
}

From source file:org.diorite.config.impl.ConfigTemplateImpl.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void defaultValueFromDefaultMethod(MethodInvoker methodInvoker, ConfigPropertyTemplateImpl template) {
    Method method = methodInvoker.getMethod();
    methodInvoker.ensureAccessible();/*  ww  w.j  a va2s  .  co m*/
    try {
        MethodHandle methodHandle = DioriteReflectionUtils.createLookup(method.getDeclaringClass(), -1)
                .unreflectSpecial(method, method.getDeclaringClass());
        template.setDefaultValueSupplier(cfg -> {
            try {
                return methodHandle.invoke(cfg);
            } catch (Throwable t) {
                throw DioriteUtils.sneakyThrow(t);
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}