use of java.lang.reflect.Proxy in project buck by facebook.
the class Connection method setRemoteInterface.
@SuppressWarnings("unchecked")
public void setRemoteInterface(Class<REMOTE> remoteInterface, ClassLoader classLoader) {
checkNotClose();
InvocationHandler invocationHandler = (proxy, method, args) -> {
InvocationMessage invocation = new InvocationMessage(method.getName(), Arrays.asList(args));
ReturnResultMessage response = messageTransport.sendMessageAndWaitForResponse(invocation);
return response.getValue();
};
this.remoteObjectProxy = (REMOTE) Proxy.newProxyInstance(classLoader, new Class[] { remoteInterface }, invocationHandler);
}
use of java.lang.reflect.Proxy in project buck by facebook.
the class BuildTargetTypeCoercerTest method shouldCoerceAWindowsStylePathCorrectly.
@Test
public void shouldCoerceAWindowsStylePathCorrectly() throws CoerceFailedException {
// EasyMock doesn't stub out toString, equals, hashCode or finalize. An attempt to hack round
// this using the MockBuilder failed with an InvocationTargetException. Turns out that easymock
// just can't mock toString. So we're going to do this Old Skool using a dynamic proxy. *sigh*
// And we can't build a partial mock from an interface. *sigh*
final Path concreteType = Paths.get("notused");
Path stubPath = (Path) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Path.class }, (proxy, method, args) -> {
if ("toString".equals(method.getName())) {
return "foo\\bar";
}
return method.invoke(concreteType, args);
});
BuildTarget seen = new BuildTargetTypeCoercer().coerce(createCellRoots(filesystem), filesystem, stubPath, ":baz");
BuildTarget expected = BuildTargetFactory.newInstance("//foo/bar:baz");
assertEquals(expected, seen);
}
use of java.lang.reflect.Proxy 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.Proxy 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.Proxy in project j2objc by google.
the class ProxyTest method test_isProxyClassLjava_lang_Class.
/**
* java.lang.reflect.Proxy#isProxyClass(java.lang.Class)
*/
public void test_isProxyClassLjava_lang_Class() {
Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class.getClassLoader(), new Class[] { Support_Proxy_I1.class });
class Fake extends Proxy {
Fake() {
super(null);
}
}
Proxy fake = new Proxy(new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
}) {
};
assertTrue("Does not believe its a Proxy class ", Proxy.isProxyClass(proxy));
assertTrue("Proxy subclasses do not count ", !Proxy.isProxyClass(Fake.class));
assertTrue("Is not a runtime generated Proxy class ", !Proxy.isProxyClass(fake.getClass()));
boolean thrown = false;
try {
Proxy.isProxyClass(null);
} catch (NullPointerException ex) {
thrown = true;
}
assertTrue("NPE not thrown.", thrown);
}
Aggregations