Search in sources :

Example 26 with InvocationHandler

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

the class JPAProviderTest method hibernate_for_proxy.

@Test
public void hibernate_for_proxy() {
    factory = Persistence.createEntityManagerFactory("h2");
    em = factory.createEntityManager();
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return method.invoke(em, args);
        }
    };
    EntityManager proxy = (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { EntityManager.class }, handler);
    assertEquals(HQLTemplates.DEFAULT, JPAProvider.getTemplates(proxy));
}
Also used : EntityManager(javax.persistence.EntityManager) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 27 with InvocationHandler

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

the class JPAProviderTest method eclipseLink_for_proxy.

@Test
public void eclipseLink_for_proxy() {
    factory = Persistence.createEntityManagerFactory("h2-eclipselink");
    em = factory.createEntityManager();
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return method.invoke(em, args);
        }
    };
    EntityManager proxy = (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { EntityManager.class }, handler);
    assertEquals(EclipseLinkTemplates.DEFAULT, JPAProvider.getTemplates(proxy));
}
Also used : EntityManager(javax.persistence.EntityManager) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 28 with InvocationHandler

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

the class RatpackServerProxy method capture.

public static RatpackServerProxy capture(ClassLoader classLoader, final String mainClassName, final String[] appArgs) {
    Class<?> mainClass;
    try {
        mainClass = classLoader.loadClass(mainClassName);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Did not find specified main class: " + mainClassName, e);
    }
    Method main;
    try {
        main = mainClass.getMethod("main", String[].class);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Did not find main(String...) method on main class: " + mainClassName, e);
    }
    if (!Modifier.isStatic(main.getModifiers())) {
        throw new RuntimeException("main(String...) is not static on class: " + mainClassName);
    }
    Class<?> capturer;
    try {
        capturer = classLoader.loadClass(CAPTURER_CLASS_NAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("could not load " + CAPTURER_CLASS_NAME, e);
    }
    Class<?> blockType;
    try {
        blockType = classLoader.loadClass(BLOCK_CLASS_NAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("could not load " + BLOCK_CLASS_NAME, e);
    }
    Method captureMethod;
    try {
        captureMethod = capturer.getMethod("capture", blockType);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not find capture() on ServerCapturer");
    }
    final Method finalMain = main;
    Object block = Proxy.newProxyInstance(classLoader, new Class<?>[] { blockType }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                finalMain.invoke(null, new Object[] { appArgs });
            } catch (Exception e) {
                throw new RuntimeException("failed to invoke main(String...) on class: " + mainClassName, e);
            }
            return null;
        }
    });
    Object server;
    try {
        server = captureMethod.invoke(null, block);
    } catch (Exception e) {
        throw new RuntimeException("Failed to invoke get() on ServerCapturer");
    }
    if (server == null) {
        throw new RuntimeException("main(String...) of " + mainClassName + " did not start a Ratpack server");
    }
    if (!server.getClass().getName().equals(SERVER_CLASS_NAME)) {
        throw new RuntimeException("Captured " + server.getClass().getName() + ", not " + SERVER_CLASS_NAME);
    }
    return new RatpackServerProxy(server);
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 29 with InvocationHandler

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

the class FactoryProvider method get.

public F get() {
    InvocationHandler invocationHandler = new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] creationArgs) throws Throwable {
            // pass methods from Object.class to the proxy
            if (method.getDeclaringClass().equals(Object.class)) {
                if ("equals".equals(method.getName())) {
                    return proxy == creationArgs[0];
                } else if ("hashCode".equals(method.getName())) {
                    return System.identityHashCode(proxy);
                } else {
                    return method.invoke(this, creationArgs);
                }
            }
            AssistedConstructor<?> constructor = factoryMethodToConstructor.get(method);
            Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs);
            Object objectToReturn = constructor.newInstance(constructorArgs);
            injector.injectMembers(objectToReturn);
            return objectToReturn;
        }

        public Object[] gatherArgsForConstructor(AssistedConstructor<?> constructor, Object[] factoryArgs) {
            int numParams = constructor.getAllParameters().size();
            int argPosition = 0;
            Object[] result = new Object[numParams];
            for (int i = 0; i < numParams; i++) {
                Parameter parameter = constructor.getAllParameters().get(i);
                if (parameter.isProvidedByFactory()) {
                    result[i] = factoryArgs[argPosition];
                    argPosition++;
                } else {
                    result[i] = parameter.getValue(injector);
                }
            }
            return result;
        }
    };
    // we imprecisely treat the class literal of T as a Class<T>
    @SuppressWarnings("unchecked") Class<F> factoryRawType = (Class<F>) (Class<?>) factoryType.getRawType();
    return factoryRawType.cast(Proxy.newProxyInstance(BytecodeGen.getClassLoader(factoryRawType), new Class[] { factoryRawType }, invocationHandler));
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 30 with InvocationHandler

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

the class ShadowWebView method ensureProviderCreated.

@HiddenApi
@Implementation
public void ensureProviderCreated() {
    final ClassLoader classLoader = getClass().getClassLoader();
    Class<?> webViewProviderClass = getClassNamed("android.webkit.WebViewProvider");
    Field mProvider;
    try {
        mProvider = WebView.class.getDeclaredField("mProvider");
        mProvider.setAccessible(true);
        if (mProvider.get(realView) == null) {
            Object provider = Proxy.newProxyInstance(classLoader, new Class[] { webViewProviderClass }, new InvocationHandler() {

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().equals("getViewDelegate") || method.getName().equals("getScrollDelegate")) {
                        return Proxy.newProxyInstance(classLoader, new Class[] { getClassNamed("android.webkit.WebViewProvider$ViewDelegate"), getClassNamed("android.webkit.WebViewProvider$ScrollDelegate") }, new InvocationHandler() {

                            @Override
                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                                return nullish(method);
                            }
                        });
                    }
                    return nullish(method);
                }
            });
            mProvider.set(realView, provider);
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Field(java.lang.reflect.Field) RealObject(org.robolectric.annotation.RealObject) WebView(android.webkit.WebView) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

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