Search in sources :

Example 1 with Proxy

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);
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) Preconditions(com.google.common.base.Preconditions) InvocationHandler(java.lang.reflect.InvocationHandler) Nullable(javax.annotation.Nullable) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 2 with Proxy

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);
}
Also used : Path(java.nio.file.Path) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TestCellBuilder.createCellRoots(com.facebook.buck.rules.TestCellBuilder.createCellRoots) Proxy(java.lang.reflect.Proxy) Paths(java.nio.file.Paths) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) Test(org.junit.Test) BuildTarget(com.facebook.buck.model.BuildTarget) Path(java.nio.file.Path) Assert.assertEquals(org.junit.Assert.assertEquals) BuildTarget(com.facebook.buck.model.BuildTarget) Test(org.junit.Test)

Example 3 with Proxy

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;
    }
}
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 4 with Proxy

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);
}
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 5 with Proxy

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);
}
Also used : Proxy(java.lang.reflect.Proxy) Support_Proxy_I1(tests.support.Support_Proxy_I1) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

Proxy (java.lang.reflect.Proxy)19 InvocationHandler (java.lang.reflect.InvocationHandler)10 Method (java.lang.reflect.Method)3 IOException (java.io.IOException)2 Constructor (java.lang.reflect.Constructor)2 URLClassLoader (java.net.URLClassLoader)2 RMIClassLoader (java.rmi.server.RMIClassLoader)2 MBeanServerInvocationHandler (javax.management.MBeanServerInvocationHandler)2 ObjectName (javax.management.ObjectName)2 IInterface (android.os.IInterface)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargetFactory (com.facebook.buck.model.BuildTargetFactory)1 TestCellBuilder.createCellRoots (com.facebook.buck.rules.TestCellBuilder.createCellRoots)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 Preconditions (com.google.common.base.Preconditions)1 MXBeanProxy (com.sun.jmx.mbeanserver.MXBeanProxy)1 DATE (com.sun.jna.platform.win32.OaIdl.DATE)1 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)1 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)1