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;
}
}
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);
}
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);
}
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);
}
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());
}
});
}
Aggregations