Search in sources :

Example 21 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project jna by java-native-access.

the class Convert method toVariant.

/**
         * Convert a java value into a VARIANT suitable for passing in a COM
         * invocation.
         * 
         * <p><i>Implementation notes</i></p>
         * 
         * <ul>
         * <li>VARIANTs are not rewrapped, but passed through unmodified</li>
         * <li>A string is wrapped into a BSTR, that is wrapped into the VARIANT.
         *  The string is allocated as native memory by the VARIANT constructor.
         *  The BSTR needs to be freed by {@link com.sun.jna.platform.win32.OleAuto#SysFreeString}.</li>
         * </ul>
         * 
         * @param value to be wrapped
         * @return wrapped VARIANT
         */
public static VARIANT toVariant(Object value) {
    if (value instanceof VARIANT) {
        return (VARIANT) value;
    } else if (value instanceof BSTR) {
        return new VARIANT((BSTR) value);
    } else if (value instanceof VARIANT_BOOL) {
        return new VARIANT((VARIANT_BOOL) value);
    } else if (value instanceof BOOL) {
        return new VARIANT((BOOL) value);
    } else if (value instanceof LONG) {
        return new VARIANT((LONG) value);
    } else if (value instanceof SHORT) {
        return new VARIANT((SHORT) value);
    } else if (value instanceof DATE) {
        return new VARIANT((DATE) value);
    } else if (value instanceof BYTE) {
        return new VARIANT((BYTE) value);
    } else if (value instanceof Byte) {
        return new VARIANT((Byte) value);
    } else if (value instanceof Character) {
        return new VARIANT((Character) value);
    } else if (value instanceof CHAR) {
        return new VARIANT((CHAR) value);
    } else if (value instanceof Short) {
        return new VARIANT((Short) value);
    } else if (value instanceof Integer) {
        return new VARIANT((Integer) value);
    } else if (value instanceof Long) {
        return new VARIANT((Long) value);
    } else if (value instanceof Float) {
        return new VARIANT((Float) value);
    } else if (value instanceof Double) {
        return new VARIANT((Double) value);
    } else if (value instanceof String) {
        return new VARIANT((String) value);
    } else if (value instanceof Boolean) {
        return new VARIANT((Boolean) value);
    } else if (value instanceof com.sun.jna.platform.win32.COM.IDispatch) {
        return new VARIANT((com.sun.jna.platform.win32.COM.IDispatch) value);
    } else if (value instanceof Date) {
        return new VARIANT((Date) value);
    } else if (value instanceof Proxy) {
        InvocationHandler ih = Proxy.getInvocationHandler(value);
        ProxyObject pobj = (ProxyObject) ih;
        return new VARIANT(pobj.getRawDispatch());
    } else if (value instanceof IComEnum) {
        IComEnum enm = (IComEnum) value;
        return new VARIANT(new WinDef.LONG(enm.getValue()));
    } else if (value instanceof SAFEARRAY) {
        return new VARIANT((SAFEARRAY) value);
    } else {
        return null;
    }
}
Also used : SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) CHAR(com.sun.jna.platform.win32.WinDef.CHAR) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) VT_VARIANT(com.sun.jna.platform.win32.Variant.VT_VARIANT) VT_BSTR(com.sun.jna.platform.win32.Variant.VT_BSTR) BSTR(com.sun.jna.platform.win32.WTypes.BSTR) Proxy(java.lang.reflect.Proxy) BYTE(com.sun.jna.platform.win32.WinDef.BYTE) LONG(com.sun.jna.platform.win32.WinDef.LONG) InvocationHandler(java.lang.reflect.InvocationHandler) Date(java.util.Date) SHORT(com.sun.jna.platform.win32.WinDef.SHORT) DATE(com.sun.jna.platform.win32.OaIdl.DATE) VT_DATE(com.sun.jna.platform.win32.Variant.VT_DATE) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) VT_BOOL(com.sun.jna.platform.win32.Variant.VT_BOOL)

Example 22 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project javaslang by javaslang.

the class Lazy method val.

/**
     * Creates a real _lazy value_ of type {@code T}, backed by a {@linkplain java.lang.reflect.Proxy} which delegates
     * to a {@code Lazy} instance.
     *
     * @param supplier A supplier
     * @param type     An interface
     * @param <T>      type of the lazy value
     * @return A new instance of T
     */
@GwtIncompatible("reflection is not supported")
@SuppressWarnings("unchecked")
public static <T> T val(Supplier<? extends T> supplier, Class<T> type) {
    Objects.requireNonNull(supplier, "supplier is null");
    Objects.requireNonNull(type, "type is null");
    if (!type.isInterface()) {
        throw new IllegalArgumentException("type has to be an interface");
    }
    final Lazy<T> lazy = Lazy.of(supplier);
    final InvocationHandler handler = (proxy, method, args) -> method.invoke(lazy.get(), args);
    return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, handler);
}
Also used : Option(javaslang.control.Option) Proxy(java.lang.reflect.Proxy) Predicate(java.util.function.Predicate) Seq(javaslang.collection.Seq) IOException(java.io.IOException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Serializable(java.io.Serializable) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Iterator(javaslang.collection.Iterator) ObjectOutputStream(java.io.ObjectOutputStream) InvocationHandler(java.lang.reflect.InvocationHandler) NoSuchElementException(java.util.NoSuchElementException) Vector(javaslang.collection.Vector) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 23 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project mybatis-3 by mybatis.

the class StatementLogger method newInstance.

/*
   * Creates a logging version of a Statement
   *
   * @param stmt - the statement
   * @return - the proxy
   */
public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
    InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
    ClassLoader cl = Statement.class.getClassLoader();
    return (Statement) Proxy.newProxyInstance(cl, new Class[] { Statement.class }, handler);
}
Also used : Statement(java.sql.Statement) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 24 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project neo4j by neo4j.

the class AdversarialFileSystemAbstraction method adversarialProxy.

private <K extends ThirdPartyFileSystem> ThirdPartyFileSystem adversarialProxy(final ThirdPartyFileSystem fileSystem, Class<K> clazz) {
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            adversary.injectFailure((Class<? extends Throwable>[]) method.getExceptionTypes());
            return method.invoke(fileSystem, args);
        }
    };
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    return (ThirdPartyFileSystem) Proxy.newProxyInstance(loader, new Class[] { clazz }, handler);
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 25 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project nutz by nutzam.

the class JsonRPC method mapper.

/**
     * 客户端. 用于生成一个代理接口的实例,透明访问json-rpc服务
     * @param klass 需要代理的接口
     * @param endpoint jsonrpc URL入口
     * @param namespace 命名空间,非json-rpc标准,扩展用,不需要就传null
     * @param timeout 超时设置,若永不超时,设置为-1
     * @return 代理实例
     */
public static <T> T mapper(Class<T> klass, final String endpoint, final String namespace, final int timeout) {
    return (T) Proxy.newProxyInstance(klass.getClassLoader(), new Class<?>[] { klass }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            NutMap jreq = new NutMap();
            jreq.setv("jsonrpc", "2.0").setv("id", R.UU32()).setv("method", method.getName());
            if (!Strings.isBlank(namespace)) {
                jreq.put("namespace", namespace);
            }
            jreq.setv("params", args);
            Request req = Request.create(endpoint, METHOD.POST);
            req.setData(Json.toJson(jreq));
            Response resp = Sender.create(req).setTimeout(timeout).send();
            if (resp.isOK()) {
                if (method.getReturnType() == Void.class)
                    return null;
                return Json.fromJson(method.getGenericReturnType(), resp.getReader());
            }
            throw new RuntimeException("resp code=" + resp.getStatus());
        }
    });
}
Also used : Response(org.nutz.http.Response) Request(org.nutz.http.Request) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) NutMap(org.nutz.lang.util.NutMap)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)179 Method (java.lang.reflect.Method)97 InvocationTargetException (java.lang.reflect.InvocationTargetException)24 Test (org.junit.Test)19 IOException (java.io.IOException)11 Proxy (java.lang.reflect.Proxy)10 Field (java.lang.reflect.Field)9 AccessibleObject (java.lang.reflect.AccessibleObject)7 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 Support_Proxy_I1 (tests.support.Support_Proxy_I1)6 Callable (java.util.concurrent.Callable)5 MBeanServerInvocationHandler (javax.management.MBeanServerInvocationHandler)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 RemoteObjectInvocationHandler (java.rmi.server.RemoteObjectInvocationHandler)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3